Install and configure Apache, PHP and MySQL on Mac OS X 10.8 Mountain Lion

Setting up your very own local web development workstation

Disclaimer: This is the way I do things. It may or may not help you. That being said though, I’ve had some pretty good results working this way. Most of the information for this guide I got from Neil Gee on his very helpful ‘Coolest Guides on the Planet’ website.


So you bought a new Mac. It runs Mountain Lion and you can’t wait to see how well it performs. But there’s one thing you need to do before unleashing its potential: Setting up your (AMP stack) development environment. Let’s get it out of the way quickly:

Apache

Apache comes pre-installed in OS X, so you just need to enable it. The fastest way to do that is through the Terminal application. It’s pretty straightforward. Just got to your Applications folder, open Terminal, and enter the following command to start the Apache web server:

sudo apachectl start

sudo allows any permitted user to execute a command as the superuser. It requires that users authenticate themselves with a password. If this is the first time you use sudo, it will prompt you to create a password. Remember it and keep it somewhere safe. You will keep using this password any time you need to sudo anything else.

Once you start the web server, check if it’s working as it should. Open your browser and go to:http://localhost/. You should find a greeting text: “It works!”.

The beautiful “It works!” message you should find after activating.

Document Root

The document root is the place where your websites are shared from the file system. You can usually find it with the names public_html or htdocs.

System Level Web Root

The system level web root is global for all users. The default system files are found at /Library/WebServer/Documents/. Stuff you put here can be accessed in your browser at: http://localhost/.

It’s a good idea to allow any .htaccess files used to override the default settings. You can do this by editing the httpd.conf file. You can find it at:

/etc/apache2/httpd.conf

Quick tip: These directories are not easy to find using the Finder app (oh, the irony), but you can still access them. Open Finder and type cmd+shift+G to open the ‘Go To Folder…’ box.Then type the path to the folder you want to find (in this case /etc/apache2).

Navigating system files on the Finder

Once your inside the httpd.conf file go to line 216 and set the AllowOverride setting to All. Then restart Apache.

Just change the AllowOverride and you’re good to go

User Level Web Root

The user level root can be viewed at: http://localhost/~youruser/
You’ll have to replace the ~youruser part with your own username. If you don’t know your username, just open Terminal and type:

whoami

The files for this document root should be kept in the ~/Sites folder in your home account. Apple decided to not have a Sites folder by default, so you need to make it. Again, follow these steps in Terminal:

Go to your user home directory:

cd ~

Make the Sites folder:

mkdir Sites

It’s very likely that after doing this you’ll still not be able to access the User Level Web Root http://localhost/~youruser/. To tackle this you’ll have to add in a youruser.conf file.

In Terminal type this command to go to the Apache users folder:

cd /etc/apache2/users/

Now see which users are listed by typing:

ls

There should appear at least two files:

  • Guest.conf
  • A configuration file with your username on it (in my case it would be carlos.conf)

If you don’t see the latter (very likely), create one by YOUR username with the .conf file extension:

sudo nano youruser.conf

Then add the content below swapping youruser with your own in the code below:

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

Restart Apache for the new file to be read:

sudo apachectl restart

Now check if everything’s working fine by pointing your browser to: http://localhost/~youruser/

Use Dropbox to sync to OS X apache sites folder (optional)

Up until this point you’re perfectly good to go with your Apache set-up. Just save your files either in your System Level Web Root or in your User Level Web Root and you’ll be able to access them from the browser.

Instead of using the default~/Sites folder though, I like to keep all my development files in a Dropbox folder. This way I have an instant backup of my work. It turns out doing this is pretty straightforward.

We just have to create a symbolic link (symlink) from Dropbox to the Sites folder.

First delete the ~/Sites folder from the User account. In Terminal type:

rmdir ~/Sites

Now let’s create the symlink from the Dropbox folder:

ln -s /Users/youruser/Dropbox/nameoffolder /Users/youruser

Replace youruser with your actual username.

Lastly you’ll need to change the permission of the main Dropbox folder:

chmod 755 /User/youruser/Dropbox
You should see the symlink in your user home folder

Restart Apache and check everything by pointing your browser to http://localhost/~youruser/.

PHP

Mountain Lion comes pre-loaded with a version of PHP (5.3.xxx). It just needs to be turned on by uncommenting the proper line in the httpd.conf file. In Terminal type:

sudo nano /etc/apache2/httpd.conf

Use ctrl+W to search and search for the word php. Now uncomment the line by removing the # sign.

LoadModule php5_module libexec/apache2/libphp5.so

Write out and save using the nano shortcut keys ctrl+O and ctrl+X. Now re-load Apache:

sudo apachectl restart

Test PHP by creating a file named phpinfo.php. Save it in your document root with the code below:

<?php phpinfo(); ?>

Now go to your browser and view it from: http://localhost/~youruser/phpinfo.php

Some extra configuration

Since this is a development environment, it would be a good idea to start displaying php errors. It will definitely help with the annoying debugging tasks. Go to your /etc/php.ini file(you should be able to find it by now) and go to line 538. Now set the setting to On:

display_errors = On

Html errors can also be helpful. On line 611 do:

html_errors = On

Php default error messages are pretty boring and not very helpful. Luckily Mountain Lion comes with Xdebug built in. You just have to activate it. On line 953 uncomment the proper lines:

zend_extension=”/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so”

Restart Apache, and make sure Xdebug is loading by looking at your phpinfo.php file.

I also recommend you install composer. It’s a package manager for php and a lot of frameworks are using it.

MySQL

Hold on. There’s just one final tiny step to make before you can happily go on with your life: installing MySQL. It needs to be downloaded from the MySQL site. Use the Mac OS X ver. 10.7 (x86, 64-bit), DMG Archive version. It should work fine on OS X 10.8.

Once downloaded install the 3 components in this order:

  1. mysql-5.6.xxx.pkg
  2. MySQLStartupItem.pkg
  3. MySQL.prefPane

Important: Mountain Lion comes with a new security feature known as Gatekeeper. It may present some problems when you’re installing stuff from packages downloaded directly from the web (instead of the App Store). To get around this simply right click the .pkg installer and open it from the contextual menu.

Getting around Gatekeeper is as easy as opening the file by right-clicking.

Now start the MySQL server from the System Preferences.

The full path to the MySQL commands is found here:

/usr/local/mysql/bin/mysql

If you want to use these commands without typing the full path you’ll need to add the mysql directory to your shell path. In Mountain Lion this is done from the .bash_profile file. To create or edit this file simply type from your terminal:

cd ; nano .bash_profile

Now add the path:

export PATH="/usr/local/mysql/bin:$PATH"

Write out and save using the nano shortcut keys ctrl+O and ctrl+X. Lastly you’ll have to reload the shell for the changes to take effect. Again, from you Terminal:

source ~/.bash_profile

Set the MySQL root password

It’s very important for you to create the root password for the MySQL installation. This is a security measure and keeps strangers from logging in your server and doing crazy stuff. Use this command:

/usr/local/mysql/bin/mysqladmin -u root password ‘yourpassword’

Change yourpassword to any password you like. Try to make it strong and difficult to guess (use numbers and special characters). Then write it down and keep it somewhere safe. Use the single ‘quotes’ surrounding the password.

Say goodbye to phpMyAdmin

I never really liked phpMyAdmin’s interface.It’s ugly and cluttered and really just not my cup of tee. Sequel Pro is a very nice alternative. I recommend you try it out.