Wednesday, 23 November 2011

Set Up Apache Web Server on OS X Lion

OS X picture

This post documents the steps for setting up Apache on OS X Lion. Now the way I setup the server is a little different, but I will try to explain why.





  1. Start Apache Web Server
    1. Choose System Preferences in the Dock.

    2. Under Internet and Wireles and choose Sharing.

    3. Check Web Sharing.

    Apache should start up. If it does not, see the Troubleshooting section.


  2. Uncheck Web Sharing. This stops the Apache server. The server should be stopped so changes can be made to the directory structure.

  3. Set up the Apache home directory.

    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 the directory you will see the permissions set to 755 with 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. Open a terminal window

    2. Switch to the root user: sudo bash

    3. In your home directory, create a directory to hold your web pages (e.g., www). Note that the permissions will be set to 775 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.

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

    5. Change to web server directory: cd /Library/WebServer

    6. Rename the Documents directory: mv Documents Documents.bk

    7. Link to the directory our created: ln -s ~/www Documents

    8. Type exit to exit from your root shell.


  4. Now enable PHP

    1. Open a terminal window

    2. Switch to the root user: sudo bash

    3. Change to the configuration directory: cd /etc/apache2

    4. Change the permissions so you can edit the configuration directory: chmod 744 httpd.conf

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

    6. Uncomment this line: LoadModule php5_module libexec/apache2/libphp5.so


  5. Restart the Web Server

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. In my case, 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 console solved the startup problems for me.

No comments:

Post a Comment