Sep 10, 2012

Setup Apache Server on OS X Mountain Lion (10.8)

OS X picture

This post documents the steps for setting up Apache on OS X Mountain Lion. A few things have changed between Lion and Mountain Lion and I will try to point them out.


  1. Start Apache Web Server

      The first difference from Lion of note. Web Sharing no longer appears as an option under Sharing in System Preferences. Therefore, all the required commands must be performed from the command line.

      Apache should be running by default type in http://localhost in a browser. You should get back "It works!". Note: Some browsers may require you to use http://127.0.0.1 for the address.

      If apache is not running, do the following.

    1. Open a Terminal window.
    2. Type: sudo bash
    3. Enter the password. You should now be in super user mode.
    4. Type: apachectl -v. This should give you the version of the Apache server.
    5. Type: apachectl start
    6. Apache should start up. If it does not, see the Troubleshooting section.

  2. Open a terminal session in super user mode if you have not done so already.

    1. Open a Terminal window.
    2. Type: sudo bash
    3. Enter the password. You should now be in super user mode.
  3. Type: apachectl stop to stop the apache server.
  4. Setup the apache home directory.

  5. The Apache home directory is located at /Library/WebServer/Documents. Of course you can set up your website and do your development there. However, if you perform an ls -l on /Library/WebServer you will see the permissions are set to 755 with a user of root and a group of wheel. So effectively, you have no access to the directory. So you can either make files as root, or add yourself to the wheel group. Neither is very convenient, so this is the approach I take.

    1. In your home directory, create a directory to hold your web pages (e.g., www) while you are still logged in as super user. Note that the permissions will be set to 755 with a user of root and a group of staff. Since you are a member of the staff group by default, you have read and execute rights to the directory.
    2. Add write permissions to the directory: chmod 775 www. Now this directory can be edited by both the web server and by you. Now we can have the web server point at this directory as its document directory.
    3. Change to web server directory: cd /Library/WebServer
    4. Rename the Documents directory: mv Documents Documents.bk
    5. Link to the directory our created: ln -s ~/www Documents
    6. To test your setup type: apache start. If you reload http://localhost in your browser, you should now see a directory listing of whatever is in www.

  6. Now enable PHP
    1. Open a terminal window and switch to super user if you have not done so already.
    2. Change to the configuration directory: cd /etc/apache2
    3. Change the permissions so you can edit the configuration directory: chmod 744 httpd.conf
    4. Edit the file with your editor of choice. vi httpd.conf. Note: If you do not know how to use vi, learning it is highly recommended as it is almost always available on Unix based systems.
    5. Uncomment this line: LoadModule php5_module libexec/apache2/libphp5.so

  7. Restart the Web Server: apachectl start if the server is not running. Or if the server is running: apachectl restart.

This should setup the server to work with PHP and allows you to easily edit and manipulate files.

Troubleshooting Tips

Problem: Apache will not start after upgrading from OS X Snow Leopard to Lion

Solution: The first thing to do is find out what error messages are being generated by Apache when it starts. This can be found in the Console application found in the Applications > Utilities > Console. This should list the error messages generated. Or if you prefer the command line: tail -f /private/var/log/apache2/error_log.

Apache would not start for me when I upgraded to Lion, the errors were related to directives in the Apache configuration file located at /etc/apache2/httpd.conf. Just commenting out the specific lines indicated in the error log solved the startup problems for me.

Post a Comment

Site Navigation