Setting up OS X for PHP Development with MAMP

Setting up a Mac for PHP development used to involve a few visits to Marc Liyanage’s excellent site, playing with NetInfo and the command line. But now there’s MAMP, and NetInfo is gone. So here’s how to setup PHP development on Leopard. [Note: since writing this, it looks like Marc has started distributing PHP packages again.]

The free version of MAMP only allows one site at the time – that means you need to stop and start the server every time you need to work on a different site. It is still possible to set up different sites on different ports, and there’s an application called Headdress to do just that, but I haven’t looked into it yet.

  1. What Is MAMP

    OS X comes with a web server (Apache) already installed, and set up to run on port 80. By default MAMP is set up to run alongside it, on a different port (8888). I prefer switching off the default installation of Apache and and setting up MAMP to be the only Apache on my system, running on the default port. To do that, open the System Preferences, select “Sharing”, and make sure the “Web Sharing” checkbox is unchecked (it could already be).
    Tick web sharing in OS X

  2. Installing MAMP

    Download and install MAMP. This is shockingly easy: you download it, drag it to to your application folder, and… that’s it. Suddenly you got Apache, PHP 4 and 5, mySql, and even phpMyAdmin, all running.

  3. Running MAMP

    Actually, not quite running yet. You need to start the MAMP application, which you’ll find in /Applications/MAMP, and play with the preferences a bit.
    MAMP application screenshot
    As I said, I like running MAMP on the default port, port 80. To do that, click on “Preferences…” then “Set to default Apache and MySQL ports”. MAMP will ask you for your password, make the changes, and stop and restart the server.

  4. Adding MAMP to $PATH, to be able to run PEAR

    I also added the php5 folder to the PATH variable – that allows me to run PEAR and PHP. To do that, I edit .bash_profile in nano (see below), adding
    export PATH=/Applications/MAMP/bin/php5/bin:$PATH What is PATH? Every time you type something in Terminal, say ‘ls’ or ‘cat’, Terminal looks at the PATH variable to see where to look for the executables. The command above tells bash to add ‘/Applications/MAMP/bin/php5/bin’ to whatever it’s already got inside PATH. Obviously, type php4 if that’s what you are using. To test it, open a new Terminal window and type
    pear config-get php_dir – this asks PEAR to tell you where it’s executables are, if you get ‘/Applications/MAMP/bin/php5/lib/php’ you are good to go.

  5. The MAMP widget

    In the application folder you’ll also find a MAMP widget, which is quite useful. To install it, just double click on it.
    MAMP Widget
    If you intend to use that, it makes sense to leave the MAMP server running even when the MAMP application itself is not, which you can easily do in the Preferences.

  6. The MAMP Start Page

    If you click on ‘Open MAMP start page’ you’ll be taken to a page from which you can access the web root (http://localhost/, or http://localhost:8888/ if you didn’t change to default ports), phpMyAdmin, and even a phpinfo page. Brilliant.

  7. Where Is Everything On MAMP

    Everything is really easy to find: all your documents should go into /Application/MAMP/htdocs/, the logs are in /Application/MAMP/logs, and httpd.conf and php.ini are in /Application/MAMP/conf. Really. That easy.

  8. Setting Up Virtual Hosts On OSX Leopard

    Normally, to run separate sites at once, you would create a virtual host for each of them. However, that doesn’t work with the free version of MAMP, since it maps all hosts to the one site it allows, so this step it’s probably pointless – I did it anyway, just in case I decide to switch back to the default Apache installation.
    First, add the virtual hosts to the local hosts file, so your machine knows they exits. That means editing the /etc/hosts file in Terminal with the editor of your choice (remember, no more NetInfo Manager). I like using nano – it is very easy to use, it gives a list of shortcut at the bottom (^ stands for “CTRL key”).
    sudo nano /etc/hostssudo tells Terminal to run as superuser (in fact, you’ll be asked for your password). You need that because you are about to edit a system file.
    nano is the editor you are going to run,
    /etc/hosts is the file you are telling nano to edit.
    Within your editor, add a line like
    127.0.0.1 projectAfor each of the sites you want to run. There should be already one in there which says “127.0.0.1 localhost”, just copy and past it and change “localhost” to the name of your new local site.
    Remember that these sites are only visible locally, only your http://localhost/ will be visible from other machines.

  9. Setting Up Virtual Hosts on httpd.conf for MAMP

    Now that OS X knows to listen out for that local URL, you also need to tell MAMP how to handle it. Open /Application/MAMP/conf/apache/conf/httpd.conf in a text editor, and add something like this at the end of the file:

    <VirtualHost *:80>
    DocumentRoot "/Applications/MAMP/htdocs/projectA"
    ServerName projectA
    ErrorLog /Applications/MAMP/logs/projectA-error.log
    CustomLog /Applications/MAMP/logs/projectA-access.log common
    </VirtualHostgt;
    DocumentRoot tells MAMP where to find the site – it makes sense to create a folder within the htdocs folder.
    ServerName is what you are going to type in your browser, and should be the same as you entered in the vhosts file.
    ErrorLog / CustomLog : you don’t need a separate error and custom logs, but it makes sense.
    Repeat that for each host.
    The free version of MAMP points all the virtualhosts to the first one in the list – this is not too bad, that means every time you want to switch site you need to re-arrange the VirtualHost directives and restarting the server.

  10. Ready to start

    Stop and Start your server. You can do that either via the MAMP application, the widget or the command line by typing
    /Applications/MAMP/bin/apache2/bin/apachectl restart
    check it all worked by typing http://projectA/ in your browser.

  11. Get used to the fact it really is so simple and works smoothly.