Since I’m on winter break and have some spare time, I thought it would be helpful if I posted a little tutorial on how to setup Apache 2.2.6, PHP 5.2.4, and MySQL 5.0.45 on Mac OS X 10.5 Leopard. For Snow Leopard instructions read How to set up Apache, PHP, & MySQL on Mac OS X 10.6 Snow Leopard.

I recently purchased a very sleek MacBook from Apple.com, thinking to myself, why not end the year in style? Anyway, after I got all my old applications installed and some of my files moved over from my PowerMac G5 I proceeded to enable Apache2 and PHP5. Usually, I use lighttpd instead of Apache and compile everything from source but this setup was just so simple I decided not to bother with anything painstakingly long, which building from source can very well be.

Preparation
Please be warned that I’m assuming you have a clean install of Leopard, I doubt anything you do here can cause any serious damage but just be sure you have backups of everything important. If you screw up my directions there are copies of the Apache configuration files you’ll be editing in /etc/apache2/original so don’t despair.

Download and install Xcode 3.1.x (version 3.2 or later will not work on Leopard). If you don’t want to wait for the latest version to download a copy of Xcode comes with every Mac OS X DVD in the Optional Installs folder.

Open up the Terminal application in /Applications/Utilities or if you prefer use iTerm.

I will be using the GNU nano text editor in my examples since it already comes with the Mac, but if you’d like to use TextMate or BBEdit just replace nano with mate or bbedit respectively and remove any nano specific options (e.g. +n or -w).

Whenever I ask you to type something into Terminal you have to hit return for the command to go through. Alternatively, for you lazy ones out there, you can just copy and paste these commands.

Setting up PHP
Enter the following into the command line/shell (Terminal):
sudo nano -w +114 /etc/apache2/httpd.conf
Whenever you put sudo in front of a command you will need to enter your root/administrator password when prompted to do so.

Your cursor will now be at the beginning of line 114, which should look like this:
#LoadModule php5_module libexec/apache2/libphp5.so
Uncomment that line by removing the pound/hash sign (#).

Before you close the httpd.conf file you should hit control w and search for:
DirectoryIndex index.html
Add index.php to the end of the line like so:
DirectoryIndex index.html index.php

When you are finished hit control x to exit, then type y, and hit return to save your changes.

Now, you should type the following into Terminal:
sudo cp /etc/php.ini.default /etc/php.ini

I’ll let you choose how you configure your php.ini file but I’d suggest you make sure PHP reports all errors, so in shell type:
sudo nano -w +305 /etc/php.ini
Change line 305:
error_reporting = E_ALL & ~E_NOTICE
To:
error_reporting = E_ALL
Close and save (unless you have any other changes to make of course). If you’re lazy you can just download php.ini-recommended and overwrite php.ini with that.

Setting up Apache
If you’re like me and use rewrite rules in an .htaccess file for those nice clean search engine-friendly URLs you’ll need to make sure mod_rewrite is working properly. For this you will need to know your computer’s short name, on this one it is ariadoss, so replace ariadoss with whatever yours happens to be, you can easily tell what it is from the Terminal cause it’s the name right before the dollar sign (e.g. danilo-stern-sapads-macbook:~ ariadoss$). If you still can’t figure it out it doesn’t matter since there is only one file in /etc/apache2/users, which should be yourname.conf.

Go to the Terminal and type in everything except the bold part unless you know what goes there, hit tab to autocomplete the missing bold part with your info before hitting return:
sudo nano -w /etc/apache2/users/ariadoss.conf

You should now see something like this on line 1:
<directory "/Users/ariadoss/Sites/"></directory>
Directly underneath line 1, you should change lines 2 and 3 to look like this:
Options All
AllowOverride All

Close and save.

We’re not exactly done yet but let’s go ahead and test that Apache and PHP are running.
Go to System Preferences, click on the Sharing icon, then select Web Sharing and make sure it’s checked, the light will turn green and it will say Web Sharing: On. Now open up your favorite web browser and go to:
http://localhost/

If you want to see the contents of your Sites directory go to: http://localhost/~ariadoss
Again, make sure you replace ariadoss with your short name.

Personally, I hate typing in ~ariadoss every time I need to test a site so I made some further changes to my httpd.conf file, which you probably only want to implement if there are no other users on your computer. We could setup virtual hosts to make short memorable URLs like http://ariadoss/ instead of just pointing /Users/ariadoss/Sites to http://localhost but that’s outside the scope of this tutorial. However, if this interests you read my tutorial on How to setup virtual hosts on Mac OS X 10.5 (Leopard).

So here’s what you do, back to Terminal:
sudo nano -w /etc/apache2/httpd.conf
Once open, replace the two occurrences of /Library/WebServer/Documents (on lines 163 and 190) with the full path to your own Sites directory (e.g. /Users/ariadoss/Sites). To easily find the path to your particular directory open another Terminal window (command n) and enter:
cd ~/Sites
pwd

Once you finish finding and replacing all that jazz, restart Apache using the command below:
sudo apachectl restart

You’ll want to restart the Apache service either from System Preferences or the command line whenever you make a change to a configuration file.

Okay, now I know that was a lot to absorb and we haven’t even gotten to building and installing MySQL yet, so get a drink of water, stretch, even walk around a little bit, just make sure to clear your head before proceeding to the next section. You may want to go to http://localhost/ and make sure it’s displaying the contents of your Sites directory – if not make sure you clear your cache and refresh the page before assuming you did something wrong and double check your index.html file’s contents so you know what to expect.

Compiling and Installing MySQL
At this point in time there is still no MySQL installer for Leopard so you will have to build and compile it yourself. I wouldn’t mind explaining how to do this but Dan Benjamin over at Hivelogic already did a thorough job of it:
Installing MySQL on Mac OS X

You want to follow Dan’s directions until you reach the heading titled, Baking-In the MySQL Bindings, which is really only needed if you’ll be developing with Ruby on Rails.

Update: A MySQL Installer for Mac OS X 10.5 (Leopard) has been made available; meaning you no longer have to go through the Hivelogic tutorial above.

To be able to use localhost as your database hostname instead of 127.0.0.1, open up Terminal and enter:
sudo nano -w /etc/php.ini
Search (control w) for:
mysql.default_socket =
And replace with:
mysql.default_socket = /tmp/mysql.sock
Then search for:
mysqli.default_socket =
And replace with:
mysqli.default_socket = /tmp/mysql.sock
Save and close then restart Apache using one of the two previously described methods (I prefer sudo apachectl restart).

After you install MySQL you may want to run the following command to create a password for your root user:
mysql_secure_installation

Congratulations you now have a perfectly functional development environment, go write the next Facebook or Digg or some other brilliant web app.

Last modified: March 3, 2012