Search:

Configuring a local Apache server under OS X 10.7 (Lion)

As I in november 2011 retired our 2003 vintage Apple PowerBook G4 running OS X 10.3 (Panther) for an Apple MacBook Air, this triggered a rewrite of the previous document to account for configuration of Apache under OS X 10.7 (Lion). Though still straightforward, a few things have changed over the years, in particular the location of the configuration files.

When editing PHP scripts, it is rather convenient to be able to "dry run" the code locally, without having to upload them onto your web host each time. On Apple OS X, the configuration of a local Apache server is rather simple, as the system is delivered with a pre-installed server which only has to be configured for your local needs.

In order to configure the local Apache server on OS X (in this example assuming that your user name on the system is 'hakkasberra', simply apply the following:

  1. Open System Preferences: System Preferences -> Sharing -> Services -> Personal Web Sharing. If Web Sharing is already running, then turn this off by simply unchecking the Web Sharing checkbox. (Notice that the "Stop" button is no longer present in 10.7.)
    Stop the local apache server
  2. In a terminal window (Xterm), move to the directory containing the configuration files for the Hypertext Transfer Protocol Daemon (or simply "httpd", i.e. web server) and open httpd.conf in your favourite editor (say Emacs). In all cases, the operations to follow should require root privileges if the server is set up correctly, otherwise things are deadly wrong when it comes to the security of your system. Notice that in OS X 10.7 (as compared to 10.3), the location of the configuration file is no longer /etc/httpd/, but rather /private/etc/apache2/. In OS X 10.7, the httpd.conf file has for some reason been set read-only by default, even for root. In order to make this writeable (only for root, with resulting access privileges -rw-r--r--,
    
    tesla:~ hakkasberra$ cd /private/etc/apache2/
    tesla:apache2 hakkasberra$ sudo chmod +w httpd.conf
    tesla:apache2 hakkasberra$ sudo emacs httpd.conf
    
    
  3. In httpd.conf, uncomment the following two lines:
    
    LoadModule php5_module libexec/apache2/libphp5.so
    
    
  4. Make sure that 'AddType application/x-httpd-php .php' and 'AddType application/x-httpd-php-source .phps' (that is to say, the lines which determine which file suffixes will be recognized as PHP scripts by the Apache server) are uncommented in the following block:
    
    <IfModule php5_module>
            AddType application/x-httpd-php .php
            AddType application/x-httpd-php-source .phps
            <IfModule dir_module>
                    DirectoryIndex index.html index.php
            </IfModule>
    </IfModule>
    
    
  5. Comment out the lines related to DocumentRoot and change to the default file repository you wish to use instead (for the user "hakkasberra" we use /Users/hakkasberra/Sites):
    
    #
    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    #
    # DocumentRoot "/Library/WebServer/Documents"
    DocumentRoot "/Users/hakkasberra/Sites"
    
    #
    # This should be changed to whatever you set DocumentRoot to.
    #
    # <Directory "/Library/WebServer/Documents">
    <Directory "/Users/hakkasberra/Sites">
    
    
  6. Save the httpd.conf file (in Emacs by Ctrl-x Ctrl-s) and kill the editor (in Emacs by Ctrl-x Ctrl-c).
  7. Restart the Apache server: Start System Preferences -> Sharing -> Services -> Personal Web Sharing, followed by checking the Web Sharing checkbox.
    Starting the local apache server
  8. Open a browser and open http://localhost/ to view the (X)HTML delivered by the local PHP parser, reading the contents located at /Users/hakkasberra/Sites/ on your disk.

Done!

Return to previous page

Leave a message

Your name:

Your email: (required)

Message:

Generated by ::emailform::

Last modified Wednesday 18 Jan 2012