Setting up a PHP/MySQL Local Development Environment on a Mac (doing it properly)
This tutorial will show you how to setup a Local Development Environment on your Mac the proper way (not using MAMP or XAMPP). I will also give examples of some of the software you can use for your day to day development including an IDE, SQL Browser and Subversion client.
Since OSX Leopard (I think), Macs arrive bundled with a local Apache server and a PHP install. The only real missing thing are MySQL and turning it all on. It’s not quite as easy as that as you are about to see but follow the instructions below and you should be up and running in half an hour (depending on typing speed ;))
Why not use a 3rd party product?
You may ask why not use XAMPP or MAMP… This is entirely down to personal preference but seeing as Macs are bundled with most of the software required for local development, it seems silly to just ignore all that and duplicate all that functionality.
For those of you that would prefer the easier route then use one of the following links and download the files on their sites as instructed.
So… For those of you NOT taking the easy way out
Download the software
The first step is to download all the software you will need which in this case is simple, get the MySQL DMG Package from http://dev.mysql.com. If you prefer not to trawl their site then use the following link:
- http://dev.mysql.com/downloads/mysql/5.1.html (MySQL 5.1 DMG File which was stable at time of writing)
To save having to compile from binaries, MySQL have released a version in a Mac friendly DMG format. Just double click the file when it downloads and then it should be installed to the appropriate place.
For this section you will need to be logged in as a user with Administrative rights on the computer (or someone in the ‘sudoers’ file). At this stage it would be wise to backup the file we are about to edit.
Open your Terminal (which lives in /Applications/Utilities/) and type the following
sudo vi /private/etc/apache2/httpd.conf
You will be asked for your password again to confirm that your user can use the ‘sudo’ command and then be entered into ‘vim’. You can substitute ‘vi’ for any of the other terminal based text editors, pico, nano, etc..
Within the file you need to comment a line which will enable PHP to function on the computer. Find a large block of lines that each start ‘LoadModule’ and in the list you will see a line that looks like:
#LoadModule php5_module libexec/apache2/libphp5.so
Remove the preceeding hash (#) character.
Next we need to tell it to look for the index.php file when a root url is given and no filename (http://www.google.com as opposed to http://www.google.com/index.php). There will be a block of code that looks something like the following:
<IfModule dir_module> DirectoryIndex index.html index.htm </IfModule>
Add index.php to the list of files on the middle line so it looks like the following:
<IfModule dir_module> DirectoryIndex index.html index.htm index.php </IfModule>
Save and close the file. If you are not logged in as an administrator and didn’t use the ‘sudo’ command then you will likely see an error telling you the file is read only and cannot be saved. In this case you need to go back and do it all again using ‘sudo’.
We will need to start/restart Apache now to make sure that nothing is broken. Firstly though, run a test on the settings through Apaches internal validator:
sudo /usr/sbin/apachectl configtest
If you see no errors then run the next command which will restart Apache. If there are then revert httpd.conf back to your backup and start the process of editing it again.
sudo /usr/sbin/apachectl restart
Test it’s all working
We can now test that PHP is functionning by creating a test PHP file at the following location:
You should see a number of files and images including an index.html file. Create a new file called index.php and put the following line inside:
<?php phpinfo(); ?>
Open your browser and navigate to:
Assuming you don’t see an apache welcome page or a server error then everything is working so far. If you see the apache welcome page then it might be a priority issue in the file naming. Just go to the place where you saved your ‘index.php’ file and delete the ‘index.html’ file (and any other files if you aren’t going to use them for something). On trying again it should show your file.
That concludes the main part of the tutorial. You should now have a local Apache server that supports PHP and MySQL by default. If you are not interested in software or running multiple local sites (virtualhosts) then thanks for reading!
For the rest of you… Read on
Adding .htaccess Support
By default there isn’t a standard .htaccess file for this. It’s a simple fix however, just add the following code to your httpd.conf file and restart Apache as previously mentionned. You can then create .htaccess files in your web servable directories to do anything you need them to.
If you would like to run more than one local site then you would need to set up Virtualhost directives in your httpd.conf file. First go to the bottom of the file and add the following line.
Then you can add the following block for as many local sites as you want to run.
<VirtualHost *:80> DocumentRoot "/Library/WebServer/Documents/wordpress" ServerName wordpress </VirtualHost>
The above block will look for a site called ‘wordpress’ at the path specified (it can be anywhere on your system). You can specify several other options within the VirtualHost block and it has been thoroughly documented elsewhere. The most useful extra thing to add is another AccessfileName line in case you wanted to call it something else for example. Remember that you will need to restart Apache after you make any changes to the http.conf file before any of your changes can take effect.
You may have noticed that I called the above site ‘wordpress’, this assumes there is a local site by that name. Without adding the next part, none of it will work. You need to edit a file called ‘hosts’ which lives at ‘/private/etc/hosts’. This contains a number of lines that follow a standard format. You will need to add a new line to the file for each site you want to add. For my example I would need to add the following line.
You don’t need to restart apache after editing the hosts file, it will all just work.
Tigris.org, the people that make the popular Windows software Tortoise SVN have written a free SVN client called SCPlugin. It integrates Subversion controls into Finder making it easy to work with version controlled files without using a dedicated application. It’s also free!
I have also heard good things about Zigversion which is free except for commerical use. Worth a look if SCPlugin is a little flaky.
I personally have used numerous IDEs over the last few years but settled on Eclipse (PHP version) more recently. Like alot of ported software, not everything works on the Mac version however it nicely does the trick for me. It handles projects and basic autocomplete if you use it. Again it’s free! which helps somewhat.
As for paid software I used to use Zend on the PC however it was very slow and didnt recover well when the computer was coming out of hibernate mode. It does everything Eclipse does however and not bad for the price. The new version is based on eclipse anyway.
A friend of mine is very much in love with a new IDE written specifically for the Mac called Coda. It isn’t free but at $99 it is apparently worth the investment.
At work I use Navicat and am very pleased with it. Everything works and it’s very fast. Nothing has changed on the Mac version so it is still my favourite. It isn’t free but worth the money and they have a 30day free trial if you need convincing.
If you are after free then I would recommend MySQL Query Browser. It does what it says on the tin and most importantly allows you to throw the pile of garbage that is PHPMyAdmin in the bin if you haven’t already done so. It’s available from MySql along with some of their other useful tools like MySQL Workbench and MySQL Administrator.