macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Build Your Own Apache Server with mod_perl
Pages: 1, 2

Testing Your New Apache Build

And now Apache is completely compiled and installed with mod_perl. A quick test confirms that the installation was successful:



% sudo /usr/local/apache/bin/apachectl configtest
Syntax OK

This quick test confirms that Apache compiled properly and loads its default configuration file without error. But it's more interesting to actually get it to serve some Web pages. First, make sure that Apple's version of Apache isn't running by disabling Personal Web Sharing in the Sharing global system preference. Then start up your newly compiled version of Apache:

% sudo /usr/local/apache/bin/apachectl start
/usr/local/apache/bin/apachectl start: httpd started

Now fire up your favorite browser, and type in your Mac's domain name. Localhost will probably work fine. If you see a page that starts with, "Hey, it worked!", then you're in business.

Testing mod_perl

As the final part of this process, we confirm to ourselves for the sake of our own sanity, that mod_perl is functioning properly, too. Fortunately this is rather simple to do as mod_perl includes a module we can easily use for this purpose. The Apache::Status module is designed to display information about the status of your Apache Web server, as well as mod_perl itself. To use it, simply edit the default Apache configuration file, /usr/local/apache/conf/httpd.conf, and add these lines to it:

PerlModule Apache::Status
<Location /perl-status>
  SetHandler  perl-script
  PerlHandler Apache::Status
</Location>

Restart Apache so that it loads the new module:

% sudo /usr/local/apache/bin/apachectl restart
/usr/local/apache/bin/apachectl restart: httpd restarted

Now hit your Web server again, this time entering the "perl-status" directory name, e.g, http://localhost/perl-status/. You should see a page appear with something like this at the top:

Embedded Perl version v5.8.0 for Apache/1.3.26 (Darwin) mod_perl/1.27 process 12365,
running since Thu Sep 19 01:05:43 2002

Apache Startup Bundle

Having a working Apache is all well and good but not worth much unless it's running. If you'd like your Mac OS X box to function as a Web server all the time, you might want to create a startup bundle for it. Apple has documented a specification for startup bundles in its Creating SystemStarter Startup Item Bundles HOWTO, but it's a simple matter to adapt Mac OS X's existing Apache startup bundle for our purposes.

Apple has created the /System/Library directory structure for use by the Mac OS X operating system, and the /Library directory structure for use by third party applications such as our new Apache server. All system startup bundles, including for Apple's build of Apache, go into the /System/Library/StartupItems directory. The startup bundles for third-party applications go into the /Library/StartupItems directory. So to adapt Apple's Apache startup bundle, we'll first copy it to a temporary location. Later, we'll move the copy to its new home:

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

This command will copy the entire Apache startup bundle directory structure to the desktop where we can easily edit it. Next, using your favorite editor (TextEdit will work fine), open up the ~/Desktop/Apache/Apache file. The parts we're interested in look like this:

StartService ()
{
    if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then
        ConsoleMessage "Starting Apache web server"
        apachectl start
    fi
}

StopService ()
{
    ConsoleMessage "Stopping Apache web server"
    apachectl stop
}

RestartService ()
{
    if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then
        ConsoleMessage "Restarting Apache web server"
        apachectl restart
    else
        StopService
    fi
}

This file is a Unix Bourne shell script and is executed whenever your Mac starts up and shuts down. There are essentially two changes we need to make to convert this script for starting our newly compiled Apache server. The first is to remove all of the if statements. These statements programmatically test for a variable that gets set by the Personal Web Sharing check box in the Sharing system preference. Since our server won't handle Personal Web Sharing, we won't be needing these tests. The second change we need to make is to change the location of the apachectl startup script. Just calling apachectl will cause Apple's Apache server to start up. To get our new one to start, we need to change the location to /usr/local/apache/bin/apachectl. The result of our changes to the startup script looks like this:

StartService ()
{
    ConsoleMessage "Starting Apache web server"
    /usr/local/apache/bin/apachectl start
}

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

RestartService ()
{
    ConsoleMessage "Restarting Apache web server"
    /usr/local/apache/bin/apachectl restart
}

Now all that's left is to 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 a page loads. If it does, you're in business, and the Apache server will be started whenever you boot into Mac OS X.

Next Up: mod_ssl

I hope that this article has served as a gentle introduction to compiling your own Apache server on Mac OS X. However, as I've suggested, there's a great deal more you can do when building Apache.

In part two of this article, I'll go back over this process, assisting you to compile support for the mod_ssl Apache module. This module allows Apache to encrypt communications with browsers using SSL, the secure sockets layer. Any time you securely connect to a Web site, for example, to complete a credit card transaction at Amazon.com, your browser communicates with the server via SSL. If you find you need to develop Web applications that use SSL, tune in to part two of this article to learn how to add SSL support to your custom build of Apache.

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 Mac DevCenter