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).
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. 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 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.
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 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.
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).
Congratulations you now have a perfectly functional development environment, go write the next Facebook or Digg or some other brilliant web app.


AWESOME!
Great, pleasant, informative read. Switched on and simple!
The thing I was missing, which, no where on the WWW, except for your site did I read, RESTART APACHE!!!!
I have attempted to install phpMyAdmin on Mac OSX 10.5 for approx 3.5 hours.
Just could not get it working. kept getting #2002 - Socket error.
I changed php.ini etc etc.
After reading your article it was clear that there was a very fundamental process missing!!!
Thanks mate.
Happy New Year for 2008!
Regards.
Oh, glad it was of some help to you! =)
Thanks for the comment.
Under the apache section:
You should now see something like this on line 1:
Directly underneath line 1, you should change lines 2 and 3 to look like this:
Options All
AllowOverride All
Close and save.
I try to save the file but I’m getting a “Permission denied” when I’m trying to overwrite the old file name with itself, with the updated changes.
Sorry, I forgot you need to put sudo in front of that command that I tell you to use, since the shortname.conf file is owned by the root user. The directions should be (I already corrected this in the blog entry):
Go to the Terminal and type in everything except the bold part unless you know what goes there, hit
tabto autocomplete the missing bold part with your info before hitting return:sudo nano -w /etc/apache2/users/ariadoss.confYou 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 AllAllowOverride All
Close and save.
I am trying to get this to work, and it simply wont. I am using textwrangler to edit files, as I am not versed in terminal and it does not seem to do anything but ask for a password and stop.
I just want php and apache to work with mysql so I can play with a website (self teaching myself)
I am only trying to play with Joomla. I have edited so many files now I do not know how to get back to start.
Any ideas?
Well written article by the way!
Cheers,
Brook
Hi Brook,
If you follow my directions exactly on a fresh install of Leopard everything should work properly.
However, if you insist on using TextWrangler you’ll need to enter your administrator/root password every time it prompts you to when you try to save a file. Also, before you try my directions again (using TextWrangler, nano, or otherwise) copy all the files from
/etc/apache2/originalto the/etc/apache2directory; this will overwrite the existing files that you’ve been editing.If you don’t have the patience or the time to get Apache, PHP, and MySQL running this way you could always try XAMPP for Mac OS X, but honestly I’ve never tried it myself and have little inkling as to how stable it is. From what I hear it should be fairly easy to get setup though. You should know I in no way endorse this product and their website even states that the Mac OS X “version of XAMPP is still in the first steps of development. Use at you own risk!”
thanks for the tutorial.
I tried on a clean Leopard install but I got some odd results.
I followed everything in your tutorial and I got the apache page appearing from localhost so it was partially successful. However I cannot see sites in the folder I would expect (which is my Sites folder if I understand correctly) if I type localhost/~myUsername/mySite. I am told that I do not have permission.
There were a few odd things about following your tutorial though:
Firstly I do not have a apache2/original folder. Should I have one by default?
Next typing ’sudo cp /etc/php.inidefault php.ini’ did nothing from what I could tell. Should I get a message when I type this?
Some of the other commands did open files in the terminal window, but they appeared to be empty, and did not go to the line specified.
I also did not seem to have a Library/WebServer/Documents folder. Should this have been created automatically for me?
eek!
Hi bleep,
Yes, you should have a folder at
/etc/apache2/original/Open Finder and hold down
command shift GUnder Go to folder: type
/etc/apache2/original/and click Go.If you see httpd.conf and the extra folder open up Terminal and type:
sudo cp /etc/apache2/original/httpd.conf /etc/apache2/This will overwrite the changes you made to the file during my tutorial.
After this is done I’d suggest you try the tutorial from the beginning. Make sure you have Xcode installed first though and that you read and understand each step; I’ve seen people try to skip steps before but this really isn’t something you can just skim through.
You can make sure
sudo cp /etc/php.ini.default php.iniworked by typing this command into Terminal:locate php.iniIf the command returns
/private/etc/php.inithe earlier command worked.And, /Library/WebServer/Documents/ exists on a clean install of both Leopard and Tiger by default. If you can’t find the folder I’d suggest reinstalling your OS then following my directions or using something like MacPorts to get Apache, PHP, and MySQL up and running on your machine.
So that is why i could not find where apache was installed - The folder was hidden! Thanks very much.