Setting up a Web Server on Leopard (OS X 10.5) and Snow Leopard (10.6) Using MAMP – A Step by Step Guide (Revised)

November 7th, 2007

MAMP Web Server

Recently, I wrote a Step-by-Step guide for installing a basic development server on Mac OS X Leopard (10.5) using the built-in packages. It turns out that the version of PHP that is packaged with Leopard is missing many commonly used components such as the GD Library, MCrypt, and many others. Since recompiling PHP on Leopard is no small feat even for experienced developers, you may be looking for an alternative method to get your web server working.

UPDATE: Based on preliminary testing, these instructions remain up to date for Snow Leopard (10.6).

This is where MAMP comes in handy. This tutorial will walk you through the steps necessary to get MAMP set up on your site, migrate your existing data, and set up multiple virtual hosts.

Revision Notes 02/03/08: Several readers have kindly pointed out some errors in the guide that follows. I’ve updated this article to correct some (all?) of these mistakes/omissions.

MAMP

MAMP (Macintosh, Apache, MySQL, PHP) is a tool released by a German company called living-e. MAMP comes in two flavors: free and Pro. The pro version is not free (appx. $70 at the time of this writing), but offers a bunch of convenient power tools to help you configure virtual hosts, your Apache server, and your startup items.

However, with a few simple tweaks to the httpd.conf, php.ini, and hosts file, the free version of MAMP should be more than adequate for most developers.

Overview

This tutorial can be used whether you are installing a web server for the first time or are migrating from Tiger (10.4), or have set up a default installation on Leopard. Steps aimed at migrating your data and settings are labeled with an "optional" tag.

Step 1: Enable your root password

  1. Open the Directory Utility: In the Finder, navigate to the Utilities folder (tip: click on the desktop, hit Cmd+Shift+U).
  2. Click on the padlock to allow edits.
  3. Go Edit > Enable Root Password
  4. Enter and re-enter your password.

Now, you are set to access protected areas of the system via the terminal.

Step 2: Export your existing data (optional)

If you were running a web server previously, you’ll want to transfer your data and settings. The best way to get your MySQL data is to use the MySQL Administrator tool to backup all of your databases.

  1. Download and install the MySQL GUI Tools. You’ll only need the Administrator tool, but the others are good to have as well.
  2. After installing the package, open MySQL Administrator.
  3. Connect to your existing database(s).
  4. Click the "Backup" tab and run a backup routine to save your database as sql dump file on your local drive.

Alternatively, you can use phpMyAdmin to export your data if your database is under 2mb (typically). I found this method to be unusable for large databases, however.

You can also use the mysql dump command line tool. This effectively creates the same dump file as the Administrator Tool, but is not nearly as easy and foolproof to use.

Regardless of the method you choose, you should have one or more sql files ready to be imported.

Why not just copy the data files? After many, many attempts to do this, I was unable to end up with a running database, even after trying to preserve permissions.

Step 3: Save your existing settings (optional)

If you have previously set up your own server, you are likely to have settings you want to preserve. You’ll need backup copies (or access to) the following files:

  • httpd.conf
  • httpd-vhosts.conf (Apache 2)
  • httpd-userdir.conf (Apache 2)
  • php.ini
  • hosts (Leopard, Windows, or *nix; Tiger & previous OS Xes used Net Info and don’t use a hosts file)

Step 4: Shut down built-in servers

If you have installed and configured Web Sharing and a MySQL database on your machine, we’ll need to ensure these are shut down before proceeding. If you have not, then skip to the next step.

  1. In your System Preferences > Sharing control panel, uncheck Web Sharing
  2. If you have MySQL installed, in a terminal window, type the following to shut down the existing MySQL server:

    sudo /usr/local/mysql/support-files/mysql.server stop

  3. In a terminal window, type the following to remove any previous MySQL start-up items:

    sudo rm -R /Library/StartupItems/MySQLCOM

Step 5: Download and install MAMP

  1. Go to the MAMP web site and download the MAMP package.
  2. Extract and mount the dmg package. Drag MAMP (not MAMP Pro) to your Applications folder.
  3. Go to /Applications/MAMP and launch the MAMP Control Panel.
  4. Start the servers by clicking the "Start Servers" button.
  5. Do not change any of the settings at this time. Just run defaults for the moment.

Step 6: Test your installation

Click on the "Open Start Page" button. This should launch the MAMP start page.

Congratulations. You have a perfectly fine web server running on your machine. This may be fine for some people‚ in which case, you’re done‚ but web developers are going to need to tweak a few settings.

You’ll notice from your URLs that you are running on an unusual port number (8888). This is fine for the time being, but you’ll probably want to switch to the default ports to avoid having to tack on ":8888" to every local domain.

To make changes, let’s shut down the server by clicking the "Stop Servers" button.

Step 7: Switch to HTML & MySQL default ports (optional)

  1. With the MAMP Control Panel still open, click the "Preferences…" button.
  2. Click the "Ports" tab
  3. Click the "Set to default Apache and MySQL ports" button to change the Apache port to "80" and the MySQL port to "3306".
  4. Click Ok.

Step 8: Transfer your existing settings (optional)

MAMP keeps its config files in the Applications/MAMP/conf/ directory.

There are a million ways to get your settings over from your old server, including simply tweaking the settings by hand. I find the safest way to do this is to use a file comparison tool like the FileMerge tool included with XCode (included on your Leopard disc) to transfer settings surgically from your old server to your new one.

Regardless, this is the time to move your settings over if you want to customize your installation. Be careful here. This is the most common way to screw things up.

Step 9: Import your existing data (optional)

If you exported your data in Step 2, you’ll want to import them to your new MySQL server. The best way to get your MySQL data is, again, to use the MySQL Administrator tool to backup all of your databases.

  1. Connect to your existing database(s).
  2. Click the "Restore" tab. Select the file(s) saved in Step 2 to reconstruct your database(s).

Alternatively, you can use phpMyAdmin’s import tool.

Step 10: Install PEAR (optional)

PEAR is a companion to PHP that is typically installed by default along with PHP. PEAR is a set of applications, modules and pre-packaged classes that provide a wealth of functionality to your apps with minimal effort. One example covered on this blog is how to implement an elegant caching mechanism with just a few lines of code. Using PEAR is highly recommended.

If you do not already have PEAR installed, type the following in a terminal window:

curl http://pear.php.net/go-pear > go-pear.php
sudo php -q go-pear.php

This will auto-install and pre-configure PEAR for you. Accepting the defaults during the installation should be fine for most users.

Now, we need to tell MAMP’s version of PHP to look for the PEAR files. To do this, we’ll need to modify the php.ini config file installed by MAMP. First, back up the config file by typing the following in a terminal window:

sudo cd /Applications/MAMP/conf/php5/
sudo cp -p php.ini php.ini.bak

Now, edit the php.ini file, open "/Applications/MAMP/conf/php5/php.ini" in your favorite text editor.

Scroll down to the include_path directive and change the following [↵ shows line wraps -Ed.]:

FROM:

include_path = ".:/Applications/MAMP/bin/php5/lib/php"

TO:

include_path = ↵
".:/Applications/MAMP/bin/php5/lib/php:/usr/share/pear"

Step 11: Set up your first virtual host (optional)

Virtual hosts are Apache’s way of letting you serve up multiple sites on a single server. Name-based virtual hosting is a convenient way to do this. Not everyone needs to set up a virtual host. For instance, if you are just tinkering on a single site, then you can skip this step. However, if you are developing and testing multiple sites locally, you will need to do this step.
For this example, we’re going to set up a test site called "site1". With a name-based virtual host, all we’d type in is "http://site1/".

In order to make this work, we’ll need to edit the hosts file on your machine. (Note: In Tiger and previous versions of OS X, you accomplished this through the NetInfo dialog. Since the NetInfo dialog was killed in Leopard, we do this the same way you do this for *nix and Windows by editing the hosts file directly).

To edit your hosts file, type in the terminal:

sudo nano /etc/hosts

Below the default entries, you’ll add the following:

# My sites
127.0.0.1 site1

The following is a screenshot of my hosts file. Yours will be similar but different:

Screenshot of my hosts file

Save your changes.

Now, we’ll need to add a corresponding virtual host. But first, Apache wants us to add our existing default directory as the very first virtual host. This is critical, so do not skip this step.

To edit your virtual hosts file, open "/Applications/MAMP/conf/apache/httpd.conf in a text editor.

Uncomment the virtual host directive:

NameVirtualHost *

Replace the two example virtual hosts with the following:

<VirtualHost *>
DocumentRoot &quot;/Applications/MAMP/Library&quot;
ServerName localhost
</VirtualHost>

Next, add your first virtual host similar to the following:

<VirtualHost *>
ServerName site1
DocumentRoot /path/to/site1
</VirtualHost>

This is a screenshot of the vhosts section of my http.conf file. Yours will be similar but different:

Screenshot of vhosts file

Who do we have to add our default directory first? According to the Apache Docs on Virtual Hosts, they recommend the following:

If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.

Note: Failing to add my default directory as the very first virtual host tripped me up for days. If your virtual hosts are defaulting to an unexpected directory, this is likely to be the culprit.

Step 12: Restart the servers

Back in the MAMP Control Panel, click the &quot;Start Servers&quot; button to restart Apache and MySQL with your new settings. Fingers crossed, everything went well, and you can now access your local test sites.

If something went wrong, retrace your steps. The most likely cause is a mis-configuration in your Apache config file. To check the syntax of your conf file, type the following into a terminal window:

cd /Applications/MAMP/bin/apache2/bin
sudo ./apachectl -t

Repair your config file as necessary.

Step 13: …

There is no step 13. I just didn’t want to end on a bad number.

Step 14: Create a startup item

Now that you have everything running, you’ll probably want to launch the web server on start up. One of the nice things about MAMP Pro is its built in ability to start itself up on login. The free version of MAMP does not have this luxury, but this is easily solved by creating a launch daemon.

Follow this tutorial to get MAMP to launch silently on startup.

Conclusion

By this point, you should have a fully functional development server that you can extend and expand as you take on new projects.

Please feel free to add comments below if you find errors or problems with the guide above.

Good luck.

  • http://www.jauhari.net Jauhari

    There are another solutions “XAMPP”, so which one better? MAMP or XAMPP? Please give me some review

    Thanks

  • http://www.stringersites.com sstringer

    I played with XAMPP, both the pro and free versions. They are both fine products. My personal opinion is that XAMPP Pro was overkill and unnecessary for anyone who knows their way around a Terminal window and a text editor, and I was loathe to part with my money. The free version was limited in odd ways to make you want to upgrade to Pro, I suppose. Ultimately, I just found MAMP worked more easily out of the box, so to speak, so I went with it as a temporary solution until Marc Liyanage’s PHP module is officially released, at which time I’ll stop using MAMP.

  • coffeeguy251

    I think my problem is somewhere in Step 11. I paid heed to your warning about being sure to add my default directory first in the virtual hosts file, and then added the directory I actually want to use. Whenever I go to either one, all I get is an empty directory listing, and if I try to directly access a file/page contained within either directory, I get a 404 error.

    Any thoughts?

    Thanks.

  • http://www.stringersites.com sstringer

    @ coffeeguy251

    That behavior invariably happens when the default vhosts dir isn’t set to the directory you think it’s set to. Possibilities include you’ve defined the default directory twice, or, more likely, your default vhost doesn’t match Apache’s default directory.

    Edit httpd.conf. Scroll up to about line 368 or search for “DocumentRoot”. Copy the path contained in that variable (default: “/Library/WebServer/Documents”).

    Now scroll to the bottom and make sure that the very first vhost listed after declaring “NameVirtualHost *” uses that same path from DocumentRoot.

  • stusys

    So I have everything up to the VirtualHost portion. I have tried to create a virtual host named ‘systems’

    First I added…
    127.0.0.1 localhost
    127.0.0.1 systems
    …to my /etc/hosts

    Then here is where I got a little confused.
    My install of MAMP did not have a httpd-vhosts.conf file. I initally created one. (did not work — I dont think)

    So next I have several httpd*.config files:
    httpd-std.conf
    and
    httpd.conf

    I have added the following to both files in every combination of ways.

    # NameVirtualHost *
    NameVirtualHost *:80

    # Must point to self first

    DocumentRoot “/Applications/MAMP/Library”
    ServerName localhost

    # Systems stuff

    ServerName systems
    DocumentRoot “/Applications/MAMP/htdocs/systems”


    SO, the problems I am having is that when I go to http://localhost or http://systems I just get the DocRoot index of the directories. I do not get the indexes that I have in each one’s respective directories.

    So instead of ‘http://systems/’ bringing up its index.php it just lists the directories in DocRoot.

    I am not sure what exactly I have missed/done wrong.

    (I can send screen shots etc if needed.)


    JP

  • http://www.stringersites.com sstringer

    @ JP
    Sounds like you are using a default file type for these directories that Apache doesn’t expect. To fix this, open your httpd.conf file and search for ‚ÄúDirectoryIndex‚Äù. Let‚Äôs say your site uses ‚Äúindex.php‚Äù for it‚Äôs default homepage. You‚Äôll want to add that to the DirectoryIndex line which already includes names like ‚Äúindex.htm‚Äù. Add yours, save, and restart Apache.

  • stusys

    That line is already in place. The first VirtualHost seems to be the only one working. Regardless of whether I ust http://localhost or http://systems it goes to the first DocumentRooot of “/Applications/MAMP/htdocs”.

    It is not differentiating. I want http://systems to go to
    “/Applications/MAMP/htdocs/systems”. The directory with I have my index.php

    If I set the first virtual host ie local host to “/Applications/MAMP/htdocs/systems” it works just fine. There seems to be a problem with the second.

    Idealy I should be able to add N amount of directories to my htdocs and each one could be a different site via virtual host. Right?

    # NameVirtualHost *
    NameVirtualHost *:80

    # Must point to self first

    DocumentRoot "/Applications/MAMP/htdocs"
    ServerName localhost

    # Systems stuff

    DocumentRoot "/Applications/MAMP/htdocs/systems"
    ServerName systems



    JP

  • stusys

    The < VirtualHost > < /VirtualHost > portion is not posting. Actually I am not sure if that will work.

  • troy

    In regards to launching apache and mysql from the command line, there is no problems with using the apache start and stop scripts, nor the mysql stop script (as root), but the mysql start script is missing a required parameter. mysqld (and mysqld_safe) require the –user option to be set when launching it as root.

    At first, I inserted –user=`whoami` (note the backticks) just before the & in startMysql.sh and I can then execute the start and stop scripts from terminal. It worked, but I didn’t like modifying the supplied scripts.

    I think the best wa to make this work is to add a .my.cnf file to your home directory and put the user parameter in there:

    Here’s my ~/.my.cnf file:
    [server]
    user=troy

    With that, both of the following work in terminal:
    sudo /Applications/MAMP/bin/start.sh
    sudo /Applications/MAMP/bin/stop.sh

    Since I don’t like to keep apache and mysqld running all the time, I set up an alias to the start and stop script and execute mampon and mampoff to start and stop the servers:

    alias mampon “sudo /Applications/MAMP/bin/start.sh”
    alias mampoff “sudo /Applications/MAMP/bin/stop.sh”

    -Troy

  • mmcloughlin

    Is it possible to run mamp under a standard user account? Everything runs fine under admin, but MySQL fails to start under my standard account.

    I’m new to mac, and was advised to use a standard account for everyday use for security reasons. Will this turn out to be impractical for web development?

  • http://www.stringersites.com sstringer

    @ mmcloughlin
    So, first of all, someone has steered you wrong. The Mac isn’t Windows where running under an admin account is ill advised. While I probably wouldn’t let my Mom or my kid run as an admin, daily use as an admin for someone who knows what they’re doing is perfectly safe on a Mac.

    As a practical matter, it’s difficult (if not impossible) to do web development on a machine without admin privileges, so the issue you’re having with MAMP is only one of many you’ll come across.

    Regardless, MAMP always asks for your admin/root password to start up. In truth, it’s not MAMP that’s doing this but Apache that’s doing the asking. So even under an admin account, you’re going to get prompted.

    Cheers,
    Steve

  • mmcloughlin

    @sstringer
    Thank you for your advice. I have been using a standard user account, as advised by http://www.macgeekery.com/tips/security/basic_mac_os_x_security. It seems like a generally good idea, though if it’s going to cause a lot of problems as you suggest I will consider reverting to an admin account.

    However I’ve got MAMP up and running on my standard account now, by following the general idea suggested in some posts on the MAMP official forum: http://forum.mamp.info/viewtopic.php?t=376, and http://forum.mamp.info/viewtopic.php?t=48. By logging into the administrator account (which I used to install MAMP) and giving the user account read & write access to the /Applications/MAMP folder, you can make it work for the user account.

  • jsahiri

    @sstringer
    Excellent presentation of how to use MAMP. You started by saying that you were going to deal with 2 scenarios: upgrading from an existing installation on Tiger and one where one is starting from scratch.

    Your whole presentation seems to deal with those upgrading in the sense that you talk about importing an existing database but not creating one. Do I assume that creating a DB then would be handled using the MYSQL Gui tool you recommend downloading. Please advise.
    Jsahiri.

  • http://www.stringersites.com sstringer

    @ jsahiri
    Yes, administration of your MySQL database is done through a MySQL GUI tool. A copy of phpMyAdmin is installed with MAMP. I’ve never actually set it up, though, so I’m not sure if it’s pre-configured or not. You can also download phpMyAdmin from their site:

    http://www.phpmyadmin.net

    Cheers,
    Steve

  • jsahiri

    Hi,
    Excellent presentation again. Just noticed that there is no httpd-vhosts.config in “/Applications/MAMP/conf/apache/httpd-vhosts.conf. I have the normal httpd.config. Thus can’t add virtual domains. Shall I have copied one from the apache incorporated in Leopard or just created the file myself?

  • http://www.stringersites.com sstringer

    @ jsahiri
    MAMP uses a slightly older apache configuration file format where all the vhosts info is contained down at the bottom of the main httpd.conf file, not in an external file.

  • astromac

    @sstringer
    >>As a practical matter, it’s difficult (if not impossible) to do web development on a machine without admin privileges

    I think many developer types do just fine with a standard account and actually prefer using the standard account (we tend to best practices seriously.)

    I’ve been using XAMPP for web development and using a standard account was never an issue. Using a standard account for Xcode isn’t a problem either so I’m not sure your statement holds water.

    Not being able to run using a standard account really is a shortcoming of MAMP Pro that needs to be addressed. Particularly when MAMP Pro is pay-for software. That’s not to say I don’t like MAMP Pro or that I don’t think it’s nicer than XAMPP but requiring an admin account to run is kinda bush league.

  • http://www.stringersites.com sstringer

    @astromac

    You may very well be right about the ease of using a standard account. And I agree about the fact that MAMP is not the best solution. It is, however, a good stop-gap until Marc Lyneage’s PHP package is updated and we can run PHP naturally on Leopard with a broad set of lib support.

  • sfstreet

    Someone I got stucked here.

    I installed MAMP using my OSX Leopard. Added the Directives above and OK went well.

    First, I backed up my mysql db

    While I tried to restore my mysql, I tried to use the mysql Administrator, however, seems it does not recognize the username/ password the MAMP has setup root, root.
    Its giving me an error can not connect rhru socket ‘/tmp/mysql.sock’(2) (code 202)
    However, using phpmyadim, from MAMP, there is no problem.

    Am I using a completely separate Apache, PHP and Mysql? I hope so. or, is it still pointing to the original one I installed previously prior to MAMP. I did not make any changes to the php.ini. There was nothing that says I have to.

    So, I went to phpmyadmin and tried to change the password fo rmysql. Now, after I changed it, I can not get in at all, from phpmyadmin or mysql admin.

    help..[;s/

  • veekter

    Hi I’m not sure what’s wrong but I can’t start the servers when I change the apache port to 80 and mysql to 3306 (step 7).

    Any ideas?

    I had installed xampp previously so could that be affecting it? I’ve removed the xampp installation by doing rm -rf /Applications/xampp, is there anything else I need to do?

    I’ve also checked the web sharing and that’s turned off.

  • veekter

    Actually I managed to get it working, a reboot of the system seemed to help things along.

    So you can probably ignore my comments now.

  • http://www.stringersites.com sstringer

    @veekter – good news. Glad you’re up & running.

  • http://jondear.myopenid.com/ jondear.myopenid.com/

    Hi, I have MAMP, MAMP Pro, running on Tiger on a G4, and have been running Joomla and Drupal 5 and 6 with varying degrees of success. The main recurring problem is in connection with a 2002 error, connecting to MySQL.sock,
    “MySQL said: #2002 – The server is not responding (or the local MySQL server’s socket is not correctly configured)”
    which I thought was permissions related, and which I have fixed previously by allowing read write permissions on /Applications/MAMP/tmp/mysql/mysql.sock.
    Today this has stopped working. I’m currently running Drupal 6, and have successfully upgraded previously from 6.6 to 6.7 without a hitch, and last night from 6.7 to 6.8, again successfully. Until last night I could access MySQL via PHPMyAdmin on the local mac, or via Navicat from another mac on the local network, with no problems.
    This morning on booting the mac, the 2002 error returned.
    I’ve followed your other links to the stringfoo.com site, but couldn’t find anything there which helped. Any suggestions I could follow? Help!

  • http://jondear.myopenid.com/ jondear.myopenid.com/

    Just made a little headway.

    When PHPMyAdmin is launched from MAMP Pro’s Launch PHPMyAdmin button, which uses this URL
    http://localhost:8888/MAMP/frame.php?src=/phpMyAdmin/?lang=en-iso-8859-1&language=English

    …or from its browser menu selection
    http://localhost:8888/MAMP/frame.php?src=%2FphpMyAdmin%2F%3Flang%3Den-iso-8859-1&language=English

    In each case I get the error:
    Error: MySQL said: #2002 – The server is not responding (or the local MySQL server’s socket is not correctly configured)

    But when I click Start on the browser window’s menu link,
    http://localhost:8888/MAMP/index.php?language=English

    the Webstart’s Welcome to MAMP window comes up and says everything is working, and I can access MySQL when PHPMyAdmin is launched from the link further down the page:
    http://localhost:8888/MAMP/frame.php?src=%2FphpMyAdmin%2F which is
    http://localhost:8888/MAMP/frame.php?src=/phpMyAdmin/ (without the html entities)

    …then it opens MySQL without a problem. (Navicat can also login to MySQL and all the DBs inc Drupal with no problem)

    So maybe its a problem to do with the “lang=en-iso-8859-1″ addition? I now recall that I’ve had this problem before, but never resolved the cause.

    When I look into MAMP Pro’s File/Edit Template/PHP5 php.ini option for editing php.ini, there is a section which reads:

    ; PHP’s built-in default is text/html
    default_mimetype = “text/html”
    ;default_charset = “iso-8859-1″

    Should I uncomment the iso-8859-1 option?

  • SandraMillhouse

    70$ for the Pro version might seem much but the features it offers really deserve the money. Anyone can try the free version at first and see if it's what they actually need.
    Sandra Millhouse | cheap virtual server

  • http://development.e-world.am Web Development Armenia

    Great post. I have been searching for this exact info for a while now. I will bookmark it in the public bookmarking sites to get you more traffic.

  • http://security-wire.com/01/how-to-remove-palladium-antivirus-rogue-anti-spyware.html remove Palladium Antivirus

    I really appreciate your step by step guide.