Setup Local (L)AMP Stack on Mac with Homebrew

This folder will later be our document root, where we place our HTML and PHP files.$ mkdri ~/Sites Also, create a simple index.html file so we can later test if our apache configurations are working. $ echo "Hello from Sites" >~/Sites/index.htmlType $ whoami into your terminal and it will return your username, we need to now that and will use it later.A for Apache ????InstallationStop and unload the mac system build in apache$ sudo apachectl stop$ sudo launchctl unload -w /Systems/Library/LaunchDaemons/org.apache.httpd.plistInstall the httpd formula with Homebrew (V2.4.37)$ brew install httpd By the way httpd is the same as Apache2.Start background service and start Apache$ brew services start httpd $ sudo apachectl startWhen opening now http://localhost:8080/ you should see “It works!”ConfigurationOpen in your favourite editor the file /usr/local/etc/httpd/httpd.conf Find and replace configs from ➡️ to:Listen 8080 ➡️ Listen 80DocumentRoot "/usr/local/var/www" ➡️ DocumentRoot "/Users/YOUR_USERNAME/Sites"<Directory "/usr/local/var/www"> ➡️ <Directory "/Users/YOUR_USERNAME/Sites">Inside the previously edited “<Directory “/Users/YOUR_USERNAME/Sites”>”AllowOverride None ➡️ AllowOverride All#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so️ ➡️ LoadModule rewrite_module lib/httpd/modules/mod_rewrite.soUser _www ➡️ User YOUR_USERNAMEGroup _www ➡️ Group staff#ServerName www.example.com:8080 ➡️ ServerName localhost:80Yeehaw!.Apache is now installed and configured..After saving your changes in the httpd.conf file you need to restart your apache server with sudo apachectl restart..After that you can open http://localhost and should see the documents from your Sites directory. What’s next?.In my next tutorial “Create vHosts for multiple local URLs with Homebrew Apache2/httpd” you can learn more about how to create multiple local URLs with different document roots.P for PHP ????InstallationInstall the php formula with Homebrew..(V7.2.12)$ brew install phpStart PHP background service$ brew services start phpConfigurationOpen in your favourite editor the file /usr/local/etc/httpd/httpd.conf Find and replace configs from ➡️ to:Insert after the last LoadModule (probably mod_rewrite.so): LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.soDirectoryIndex index.html ➡️ DirectoryIndex index.html index.phpInsert after the previously edited “DirectoryIndex” area and before the htaccess/htpasswd “IfModule” area:<FilesMatch .php$>SetHandler application/x-httpd-php</FilesMatch>Restart your Apache server $ sudo apachectl restartCowabunga!.The latest PHP version is now installed and ready to use..Go to your Sites directory and add a info.php file with <?php phpinfo(); in it and open it in your browser via http://localhost/info.php.M for MySQL ????InstallationInstall the mysql formula with Homebrew..(V8.0.12)$ brew install mysqlStart the Homebrew MySQL daemon$ brew services start mysqlConfigurationRun $ mysql_secure_installation to start configuring MySQLNow go through the procedure and set the configs as required. I suggest this config: “Would you like to setup VALIDATE PASSWORD component?”: n“password for root”: 1234“Remove anonymous users?”: y“Disallow root login remotely?”: y“Remove test database and access to it?”: y“Reload privilege tables now?”: yHurray!.MySQL is now installed and configured..You can start, stop and restart it with the Homebrew services command like this: $ brew services start|stop|restart mysql..If you’d like to test to connect PHP with your MySQL you can do it like described on this W3Schools Site.What’s next?Links to configsAt this point, I recommend creating short links inside your Sites directory to the different config files and folders of your (L)AMP stack.Create new etc folder inside your Sites directory $ mkdir /Users/YOUR_USERNAME/Sites/etcApache $ ln -s /usr/local/etc/httpd /Users/YOUR_USERNAME/Sites/etc/httpdMySQL $ ln -s /usr/local/etc/my.cnf /Users/YOUR_USERNAME/Sites/etc/my.cnfPHP $ ln -s /usr/local/etc/php/7.2 /Users/YOUR_USERNAME/Sites/etc/php72There is moreLike mentioned after each chapter I’ve made tutorials on how to improve the current state, it will be most comfortable going through them in this order:Create vHosts for multiple local URLs with Homebrew Apache2/httpdComing soon: Install and run multiple PHP versions with HomebrewComing soon: Connect MySQL to phpMyAdmin or Sequel Pro. More details

Leave a Reply