Installing LAMP stack on OSX Mavericks with Homebrew

2014/06/14

Last week, as a turn of event, my 4-year-old mac did not recognize my hard drive and failed to boot. I instantly assumed that the hard drive was dead and bought a replacement SSD, only to find out it was the bad SATA cable that caused the problem. Got a new SATA cable from ebay and an upgrade to SSD - thanks to my hasty assumption. Now I have the old HDD sitting around for nothing, plus a Raspberry Pi rotting on the shelf. Would be a fun summer project to make a Time Machine Server - going to post about the project when it’s done!

With new SSD come new OSX installation, and new develoment evironment setup. There are many articles on the net that show how to install LAMP stack on Marvericks. Personally, I love to use Homebrew as the package manager for my mac, but tutorials using Homebrew I found on the net are outdated. So here’s an updated guide to setup latest PHP55 and MySqL on your OSX Mavericks. This guide is based on a post on frdmn’s notes, which is a tutorial about setting up local development environment with Nginx instead of Apache.

Mavericks comes with Apache and PHP bundled out of the box, so we can ignore installing Apache. The PHP version that comes bundled with Mavericks, however, is 5.4, so we are going to upgrade PHP to 5.5.

Installing Xcode

First, if you don’t have Xcode installed yet, go to the Mac App Store and download Xcode. After downloading the app, run it and make sure you agree to the User Agreement.

After downloading Xcode and agreeing to the license, open the terminal app and run:

xcode-select --install

Follow the dialog and install the Xcode command line tool.

After the installation, open Xcode, go to Xcode Preferences > Locations tab, and make sure you select the latest command line tool version. In my case, it’s 5.1.1

x-code image

Save all the settings and close Xcode.

Installing Homebrew

If you’re new to Homebrew, here’s a quote from frdmn’s article:

Now we need to install Homebrew, which is a package manager for OS X. You probably ever heard about apt-get or aptitude on Linux distributions to install packages or depencies for a specific application. brew works the same, just on Mac operating systems. It will also make sure that you will get the latest updates of the installed packages as well, so you don’t need to worry about outdated versions or vulnerable security flaws and exploits either.

First, we need to install XQuartz, which is used to build Homebrew packages. Download and install the latest XQuartz version here. At the moment of writing this post, 2.7.6 is the latest version.

After installing XQuartz, if you are asked to logout and login, do it before moving on to the next step.

Now comes the Brew Master! Run this command to download Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After Homebrew has been downloaded and installed, run “brew doctor” to check for any conflicts or problems

brew doctor

If things go well, “brew doctor” will notify

Your system is ready to brew.

If you receive warning or errors from the Brew Doctor, you have not been using your “Apple” right :D. Make sure the warnings are resolved before continuing.

Next, update and upgrade Homebrew’s formulars:

brew update
brew upgrade

Your Mac is now ready to Brew.

Install PHP5.5 with mcrypt

We are going to install the latest php5.5 version from Homebrew, together with the mcrypt library (which is required if you are developing Magento sites), otherwise, mcrypt library is optional.

Tap the necessary Homebrew formulas for our PHP installation:

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

NOTICE: in the following step, if you already have a mysql server installation, remove –with-mysql option from the installation command. Otherwise, if you want to install a fresh mysql server, make sure the old one is removed and reboot your Mac before moving on.

At this step, there are two options:

Install php55 with mysql and mcrypt:

brew install --with-mcrypt --with-mysql php55

OR install php55 and mysql without mcrypt:

brew install --with-mysql php55

This step will take a while to finish.

Aaaand we’re back.

Now, open the command line and run:

php -v

This will check the version of you PHP CLI, and you will most likely see that php version is still 5.4, or an older version that you have previously on your mac. This is because we have not setup $PATH for the new PHP CLI library. To do that, we need to update the $PATH environment in our .bash_profile. Run:

echo 'export PATH="$(brew --prefix josegonzalez/php/php55)/bin:$PATH"' >> ~/.bash_profile

After updating the ~/.bash_profile, you have to exit the terminal and open it again to make sure the terminal uses your updated ~/.bash_profile. Now run again:

php -v

And now we should be able to see the latest PHP CLI version being used:

PHP 5.5.13 (cli) (built: Jun 14 2014 16:40:32)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

Again, at this moment, the latest homebrew php version is 5.5.13 - so it may be a different version for you. Make sure you take note of your PHP version. This is very important in the next steps.

Setup auto start:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/php55/5.5.13/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/

Remember to replace 5.5.13 with your latest PHP version if it is different. Otherwise, you may get a Directory not found error.

Installing mcrypt library

As mentioned above, I am going to install the mcrypt library for php - which is required by Magento. Ignore this step if you don’t want to install it.

Install the php55-mcrypt library and autoconf

brew install autoconf php55-mcrypt

Restart Apache:

sudo apachectl restart

Installing MySQL

Install MySQL:

brew install mysql

Setup auto start:

cp /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

This will make sure your MySQL server always starts when you boot up your mac.

Now, start MySQL server:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Alternatively, if you don’t want to use launchctl, use these commands to start / stop mysql server:

mysql.server stop
mysql.server start

Setup MySQL installation:

mysql_secure_installation

If you are asked to enter the current password for root user, just press Enter since we have not set any password for root user, yet. Then, you will be asked if you want to set a password for root user, choose Y, and then type in a password for root user. For the rest of the steps, just close your eyes and choose Y.

Check if your mysql is setup correctly:

mysql -u root -p

Then type in the root user’s password you have just set in the previous step. If everything goes correctly, you will be inside the mysql> console, which looks something like this:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Exit the MySQL console by running:

\q

Configure Apache

So far so good. Now the last step: configure Apache server to run your newly installed PHP version.

With Mavericks installed, you should already have apache installed in /private/etc/apache2. Edit the apache httpd.conf file:

sudo vim /private/etc/apache2/httpd.conf

Look for the line

#LoadModule php5_module libexec/apache2/libphp5.so

Uncomment it by removing #, and replace the libphp5.so directory to our new PHP version. The updated line should look like this:

LoadModule php5_module /usr/local/Cellar/php55/5.5.13/libexec/apache2/libphp5.so

Again, pay special attention to your newly installed PHP version, mine is 5.5.13, but it may be a later version for you. If it’s not 5.5.13, replace 5.5.13 to your own version.

Next, also in httpd.conf file, look for these lines:

Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all

Change AllowOverride None to AllowOverride All

This is the updated block:

Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all

Save the file and close it.

Create a phpinfo.php file to check if Apache is running the latest PHP installation from Homebrew:

sudo vim /Library/WebServer/Documents/phpinfo.php

Use phpinfo() to output the php information being used by apache:

Restart apache server:

sudo apachectl restart

Open your web browser and go to http://localhost/phpinfo.php

You should now be able to see what version of PHP that Apache is using:

php version

Almost there! Just some more configurations to do:

Creating your user profile:

sudo vim /private/etc/apache2/users/your_mac_user_name.conf

Replace “your_mac_user_name” with your own. Now add these settings into the file:

<Directory "/Users/your_mac_user_name/Sites">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

This tells apache that your user’s web directory is at the Sites folder of your user home directory, and the settings will be used for all of your web projects located here. Now we need to create the Sites folder:

mkdir /Users/your_mac_user_name/Sites

All of your web projects will be in the /Users/your_mac_user_name/Sites folder, let’s create a new site!

Creating a new apache virtual host:

Open httpd.conf:

sudo vim /private/etc/apache2/httpd.conf

Look for this line:

#Include /private/etc/apache2/extra/httpd-vhosts.conf

Uncomment the “Include” line. The result should be:

Include /private/etc/apache2/extra/httpd-vhosts.conf

Save and close httpd.conf

Open file httpd-vhosts.conf:

sudo vim /private/etc/apache2/extra/httpd-vhosts.conf

Add a new virtual host at the bottom:

DocumentRoot "/Users/your_mac_user_name/Sites/welcome"
ServerName welcome.local
ErrorLog "/private/var/log/apache2/welcome.local-error_log"
CustomLog "/private/var/log/apache2/welcom.local-access_log" common

This creates a new virtual host welcome.local that is located in the welcome folder within your Sites directory. However, we still need to edit the hosts file to point welcome.local to our local server. Save and close httpd-vhosts.conf file.

Edit the hosts file:

sudo vim /private/etc/hosts

Add this line below “127.0.0.1 localhost”:

127.0.0.1       welcome.local

Save and close the file.

Create a new welcome directory within the Sites folder:

mkdir /Users/your_mac_user_name/Sites/welcome

Create an index.php page:

sudo vim /Users/your_mac_user_name/Sites/welcome/index.php

Add the welcome message and display phpinfo:

This will display the welcome message and your php information below. Now restart apache server:

sudo apachectl restart

Open your browser and go to: http://welcome.local

The final result should look like this:

little lamp

If you installed mcrypt correctly, mcrypt library information should be shown in phpinfo() as well

php mcrypt