I think Mac based PHP developers most commonly install an application like MAMP to handle the perceived hard work when it comes to setting up their working environment. OS X does come preinstalled with most of what you need to develop PHP websites though and it’s pretty easy to get up and running. Why install extra 3rd party software, taking up space and system resources, when Apple has done it already?
This guide starts with a clean installation of Mac OS X 10.6.4 (Snow Leopard) and walks through the steps of setting up your dev server. It finishes with a run through of an application called VirtualHostX which is by no means necessary but I think it helps give a smooth development experience.
Start with Apache
Obviously Apache is the cornerstone to the whole idea so it seems like a logical place to start this guide.
OS X uses Apache for it’s built-in web sharing feature so enabling it couldn’t be easier. Go to “System Preferences > Sharing” then turn on “Web Sharing”.
You’ll be given an IP address that others can use to access your server but of course you can do so by visiting http://localhost/ from your own machine. Try that now and you’ll be greeted by Apache’s familiar, if underwhelming, default page.
Apache’s default location (where’s you’ll find the page shown above) is /Library/WebServer/Documents and to get to your own Sites folder you’ll need to open http://localhost/~[username]/. If you’re the only person using your Mac and you’d find it more convenient if http://localhost/ went directly to your own Sites folder, you can make a simple change to Apache. Run the following command in the system terminal:
sudo pico /etc/apache2/httpd.conf
Then find the DocumentRoot directive and change it to your Sites directory, i.e. DocumentRoot “/Users/[username]/Sites”. Note that throughout this guide I am assuming you store your websites your account’s “Sites” directory, if that’s not the case simply amend any paths you see.
Now restart the server (by disabling then re-enabling web sharing in the prefs pane) and refresh the localhost page. You should be greeted with your own home page now.
Enabling PHP support
Just like Apache, PHP is included with OS X although it’s not enabled by default. To get it working edit the httpd.conf file by running this line in the system terminal:
sudo pico /etc/apache2/httpd.conf
Find the line “LoadModule php5_module libexec/apache2/libphp5.so” and uncomment it then save the file and exit the editor.
Make a PHP configuration file by running this in the system terminal:
sudo cp /etc/php.ini.default /etc/php.ini sudo pico /etc/php.ini
You should configure PHP for your own preferences but if you’re not familiar with the php.ini file just get yourself up and running by finding the directive “short_open_tag” and changing it’s value to “On”, and then uncomment the directive ”date.timezone” and give it a suitable timezone – for me that would be “Europe/London” but there’s a full list at http://php.net/date.timezone. As this is a development server you may also want to turn on “display_errors” to aid debugging. Save the file and exit the editor once you’re done editing.
Now restart the server again using the sharing prefs pane.
To test PHP make a new file in your Sites folder called info.php and include the following code:
<? phpinfo(); ?>
Open that file in your web browser and you will get an overview of PHP’s capabilities. If not double-check that the correct line has been uncommented in httpd.conf and that you have restarted Apache since enabling PHP.
All good so far! Now let’s add database support.
Installing MySQL
OS X doesn’t come with MySQL so it’s the one component you’ll need to install for yourself. Fortunately MySQL has an easy install process so run along to http://www.mysql.com/downloads/mysql/ and download the DMG archive for your system.
You’ll find that they give you the main MySQL server, a prefs pane item and a startup item. I’d recommend install all three of these components. The following image shows their prefs pane.
By default MySQL won’t set a top-level password but that’s something you’ll want to do for increased security. On a development machine it may not seem necessary but it’s good practice so after everything’s installed run the following at the system terminal:
/usr/local/mysql/bin/mysqladmin -u root password [newpassword]
For some reason the installer doesn’t create a my.cnf configuration file either, which you’ll need if you ever want to customise the installation.
sudo cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
You might like to restrict the installation a little more but I’ll leave that admin up to you. This guide is really just about getting everything working.
By now MySQL is fully functional but PHP isn’t aware of it so we need to update a few lines in the php.ini configuration file. Using the system terminal:
sudo pico /etc/php.ini
Now uncomment the directives “extension=php_mysql.dll” and “extension=php_mysqli.dll” and amend the predefined MySQL socket paths to “mysql.default_socket = /tmp/mysql.sock” and “mysqli.default_socket = /tmp/mysql.sock“.
Restart the server again (using the sharing prefs pane) and if you refresh your info.php page you should see that MySQL has been enabled.
You’ve now got a fully functional Apache + PHP + MySQL development server on your Mac and all you’ve had to install is MySQL. You can stop here if that’s all you wanted to achieve but I’m going to carry on and introduce VirtualHostX to try and make life a little easier.
Using VirtualHostX
What VirtualHostX essentially does is automatically configure your system’s hosts file and Apache’s vhost file so that you can access your development websites using a simple url rather than localhost followed by one (or often more) nested directories.
VirtualHostX only costs about £16.50 ($25 at the time of writing) and the convenience it delivers makes that money well spent. You can run the trial version if you’re unsure whether or not you need it.
http://clickontyler.com/virtualhostx/
So, download and install VirtualHostX now. When you run it for the first time it brings up the setup wizard.
The wizard will first create a backup of the operating system’s default Apache configuration, so you can always roll back if something breaks, and then it makes a minor change that tells Apache you want it to run virtual hosts. This is a perfectly safe change and is the way Apache would normally be run.
Before we go on to setup a vhost you need to have a target website on your computer. This can be any website, located anywhere on your Mac, but I like to keep all my websites within my Sites directory and for this demo I’m going to make SQL Buddy accessible from the convenient domain “dbadmin”.
Download SQL Buddy from the above link, extract it to a new folder called “sqlbuddy” within your Sites directory, and add your MySQL root password (defined earlier) to the SQL Buddy config.php script. With that the website will be accessible at the address http://localhost/~[username]/sqlbuddy/ but that’s a bit messy so open VirtualHostX and add a new host as per the following image.
Press “Apply Changes” once done and then you’ll able to quickly access your database administration by going to http://dbadmin/ – that’s tidier isn’t it?
I generally use VirtualHostX to setup development website’s using the real domain but prefixing it with “dev” rather than “www” so I don’t have to remember directory paths from my web root, or handle them within the website’s scripts.
That’s it!
I’ve probably made this guide a little more wordy than necessary but hopefully you’ll see that setting up a development server in OS X 10.6 is really simple – really just a case of installing the MySQL package and editing two system files.
It’s worth pointing out that, with all the latest OS X updates at the time of writing this guide, the built-in Apache is version 2.2.14 and it is preconfigured with the popular rewrite module. PHP is version 5.3.1 and it supports the most commonly used modules plus “pear” is also supported from the system terminal.









Nice tutorial. I’m still not buying a mac though!