Setting up a PHP/MySQL Local Development Environment on a Mac (doing it properly)

February 25, 2009 | Mac, PHP, Tutorials | 6 comments

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:

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.

Configure Apache

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:

/Library/WebServer/Documents

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:

http://127.0.0.1

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.

AccessFileName .htaccess

Multiple sites

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.

NameVirtualHost *:80

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.

127.0.0.1    wordpress

You don’t need to restart apache after editing the hosts file, it will all just work.

Recommended Software

Subversion
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.

IDE
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.

MySQL Tools
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.

A Donate Button!

6 Comments

  1. Garrett

    What about installing the MySQL server, any tips? I’m running into the common mysql.sock problem…

    Reply
    • Sean

      Hi Garrett,

      Thanks for getting in touch. It’s been a while since I needed to do this although I followed my own instructions with my new iMac last year and seemed to work ok. With regard to mysql.sock problem. I presume it’s because you have conflicting versions installed perhaps. I just installed it from the dmg file and made the appropriate connections in the code and it went fine. If you want to drop me a line via email or send me some more information then perhaps I can point you in the right direction.

      ta
      S

      Reply
      • Garrett

        Hi Sean,

        Thanks for your reply. Actually last night while I was working on it I figured out what I was doing wrong.

        -Needed to install mySQL with the root account.
        -Set permissions on /usr/local/mysql
        -And lastly, php was looking for the mysql.sock file in /var/mysql/mysql.sock when in fact mySQL was creating it in /tmp/mysql.sock I changed the php.ini file to look in the proper location and my woes were resolved.

        There’s a lot of good info on setting up mySQL here too: http://developer.apple.com/internet/opensource/osdb.html Hopefully that will help someone. Thanks again,

        -Garrett

        Reply
  2. Thabiso

    I’m running Eclipse on Ubuntu 10.4 and I trying to connect my script to mysql database and I have got an error that “Can not connect to mysql database”. The problem is that mysql_connect() can not see mysql.sock.
    I looked for it and I can’t find it. How do I create this mysql.sock for mysql?

    Reply
    • Sean

      Hi.. Linux isn’t really my area of expertise although it’s a common problem. I think you can do something in either the httpd.conf, my.conf or php.ini files to set the sock file. Either that or perhaps consider using a package such as a LAMP installer package.

      Reply
  3. Justin

    Thanks for the great tutorial, works great.

    Reply

Leave a Reply to Garrett Cancel reply

Your email address will not be published. Required fields are marked *

CommentLuv badge

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance Website Developer in Crewe, Cheshire. He is a Full Stack Developer but with extensive experience in Wordpress and other Frameworks. He is the Co-Founder of SitePresser, Layouts Cloud and Page Builder Cloud among other things..
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

SitePresser is the plugin that packages child themes and layout packs for sale. Works with Divi and Elementor.