macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Build Your Own Apache Server with mod_perl and mod_ssl
Pages: 1, 2, 3, 4

Apache Startup Bundle Redux

In part one of this article, I provided instructions for creating a startup bundle so that your custom Apache could be started when Mac OS X starts. My approach to creating the bundle involved copying Apple's Apache startup bundle from /System/Library/StartupItems, altering it, and then putting it into /Library/StartupItems.



However, attentive reader Milo Polten noticed a flaw in my approach -- at least from the point of view of good Mac OS X system administration. I had suggested that all of the if statements be removed from our copy of the Mac OS X Apache startup script, since they tested the value of the $WEBSERVER variable, which is set via the "Personal Web Sharing" check box in the "Sharing" system preference. It turns out, however, that the check box is just a convenient way for users to actually change the value of the $WEBSERVER entry in the /etc/hostconfig file, and it is actually this file that offers a Darwin-like method for enabling and disabling startup scripts.

The general idea is that all Mac OS X system startup scripts are controlled by the presence of variables in the /etc/hostconfig file, and the scripts themselves are supposed to check for the proper variable setting in that file. Thus startup services can be enabled or disabled merely by editing /etc/hostconfig, rather than by moving startup bundles in and out of /System/Library/StartupItems or /Library/StartupItems.

As a result of my newly-gained knowledge, I'd like to suggest a new approach to creating an Apache startup bundle. First, copy Apple's startup bundle to a temporary location:

% cp -rf /System/Library/StartupItems/Apache \
  ~/Desktop/

Now, using your favorite editor (TextEdit will work fine), open up the ~/Desktop/Apache/Apache file. Again, change all instances of apachectl to point to our new Apache server controller, /usr/local/apache/bin/apachectl. However, do not delete the if statements. Instead, change the name of the variable to be checked from WEBSERVER to APACHESERVER. The resulting file should look like this:

#!/bin/sh

##
# Apache HTTP Server
##

. /etc/rc.common

StartService ()
{
    if [ "${APACHESERVER:=-NO-}" = "-YES-" ]; then
        ConsoleMessage "Starting Apache web server"
        /usr/local/apache/bin/apachectl start
    fi
}

StopService ()
{
    ConsoleMessage "Stopping Apache web server"
    /usr/local/apache/bin/apachectl stop
}

RestartService ()
{
    if [ "${APACHESERVER:=-NO-}" = "-YES-" ]; then
        ConsoleMessage "Restarting Apache web server"
        /usr/local/apache/bin/apachectl restart
    else
        StopService
    fi
}

RunService "$1"

If you'd like Apache to start up with ssl support, change /usr/local/apache/bin/apachectl start to /usr/local/apache/bin/apachectl startssl.

Next, add the $APACHESERVER variable to /etc/hostconfig. The simplest way to do this is to use the echo command on the command-line to append it to the the file:

% sudo echo APACHESERVER=-YES- >> /etc/hostconfig

You can also edit the file directly using TextEdit, but you must open TextEdit as the root user in order to be able to edit the file. You can use the sudo utility on the command-line to accomplish this:

% sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit \
  /etc/hostconfig

Once you've added the line "APACHESERVER=-YES-", save your changes and quit TextEdit. Now move the entire startup bundle to its new home in /Library/StartupItems and test it:

% sudo mv ~/Desktop/Apache /Library/StartupItems
% sudo /Library/StartupItems/Apache/Apache start
Starting Apache web server
/usr/local/apache/bin/apachectl start: httpd started

Point your browser to your local computer again and make sure the test page loads. If it does, you're in business, and the Apache server will be started whenever you boot into Mac OS X.

Good to Go

There you have it, everything you need to know about how to compile your own Apache Web server with mod_perl and mod_ssl. The instructions I've provided in the two parts of this article should give you the knowledge you need to compile Apache with just about any module you're likely to need in your Internet development work. Other modules you may wish to consider include mod_php for PHP language support; mod_tcl for TCL language support; and mod_dav for WebDAV (distributed authoring and versioning) support. See the Apache Module Registry for a comprehensive list of modules. And enjoy your total Internet development environment: Mac OS X.

David Wheeler is a developer at Portland, Oregon-based Values of n, where he writes the code that makes Stikkit's little yellow notes think.


Return to the Mac DevCenter.