Web Server Programming
Chapter 3
Apache Webserver
The main Apache site is now rather large!
The Apache software foundation now hosts numerous collaborative software
projects in addition to its HTTP server.
The exercises using servlets and JSPs (for Chapters 7 and 8) rely
on the Tomcat server, a part of the Jakarta
project. If you move into more sophisticated Java development, you
will certainly get to use the ant utility
that helps handle complex program builds and deployments. The Apache main site
has a link to the Apache Perl site where
you can get mod-Perl. This is a version of the Perl interpreter
that can be integrated into the httpd web server in much the same
way as the PHP interpreter that is used in Chapter 6.
There are many other projects at Apache - web services, XML, a mail server and others.
You get your Apache by download from the download page. I am not providing direct links to download files. You should use the Apache download page because it points you to mirror servers (so that you download from a local copy). The page also has security warnings and advice on how to confirm that you have a valid copy (checks are done using hash-encryption functions applied to downloaded files).
The examples in the book were run with Apache 1.3.27. This is available in both Windows and Unix (Linux) options. (If you have installed Linux, you will have a version of Apache; check the version and update if necessary, there are security flaws and other problems with old versions of Apache that you may get from old CDs.) You can if you wish try the newer Apache 2 implementation; this is based on a threaded server model rather than the forking server used in the Unix Apache 1.3.xx series.
Your downloaded Apache comes with considerable amount of hypertext documentaton. This documentation is available on-line. The documentation covers installation, modules, content negotitation, CGI, server side includes, and all the rest.
The old "Apache Today" site that used to have articles about Apache appears to have been swallowed up by (or maybe just renamed to) Server Watch. Here you can find articles about Apache and other servers, and a variety of specialized tutorials on issues like performance tuning (and some introductory tutorials on installation etc if you find that you need more than the material that comes by default with Apache).
The tutorial site associated with O'Reilly publishers has an Apache section.
You will at some stage need to download PHP. Actually, there is a site where you can download a pre-configured set of Apache, PHP, and MySQL for Windows. Or you can get your PHP download and configure it into Apache (following the PHP instructions that cover both Windows and Linux 1.3.27 Apaches.)
If you are a Windows user, you will also have to pick up ActivePerl.
You will need to edit your httpd.conf file. Annotated examples are provided illustrating both a Windows httpd.conf file and a Unix file.
Once you have downloaded your Apache, you do not need to remain connected to the Internet. You can run all your tests and exercises on Apache with both the Apache web servers and your browser running on your own machine.
The directory structures that are created for Apache are similar in Unix/Linux and Windows.
There will be an install directory; this will contain an htdocs directory for
web pages, a conf directory for configuration files (including the httpd.conf
file that you edit), a cgi-bin directory for CGI programs, a logs
directory, and a bin directory with executables and extra scripts such as
that used to create files with user-names and passwords.
You will get Apache as a compressed tar archive. You will need two directories. One
is really the source directory, it is where you unpack the archive and run compilations and so forth. The
second directory is your deployment directory where the executables are installed when the build process
completes. You use the configure script in your source
directory; this allows you to specify module options, and also identify the deployment directory. The
configure script will generate a make file for you. You can then run gmake to compile
your Apache, and gmake install to install it.
You might then want to edit the httpd.conf file that is in the confsubdirectory of
your installation directory. Suppose that you have for example included mod-status, your Apache will
contain this feature but the default httpd.conf file will not allow anyone to invoke it. You can
change the httpd.conf file (in this case, its just a matter of uncommenting a few lines and adding
a couple of host names or IP addresses). Other changes, like creating directories which require
user authentication, can be left for later experimentation.
If you want to test run the shell script and Perl script in the cgi-bin directory, you will
probably need to change the file permissions on these files. You may need to add group and
global read and execute permissions if these are not already set. You should also check
that the #! line in the printenv Perl script example contains the correct path for
your Perl interpreter (if you are working on Unix or Linux, you will have Perl already
installed somewhere - try which perl to find where).
The scripts that start and stop your Apache are in the bin subdirectory of your install directory.
You use apachectl startto start your servers.
You should then be able to start a browser and aim it at http://localhost:8080. If you are running as root (unwise), your Apache will use port 80; if you are running with a different user-id, your Apache will use port 8080. (If localhost is not defined, try http://127.0.0.1:8080). This will bring up the default Apache welcome page (if you have set language preferences in your browser, you can get the welcome in Spanish, German, or a variety of other languages - this welcome page is a demonstration of negotiated "multiviews".)
The two demonstration cgi-bin programs both list details of the environment variables that apply to your Apache. If you have set permissions correctly, you should be able to invoke the Perl script by aiming your browser at http://localhost:8080/cgi-bin/printenv.
You should then stop your Apaches (apachectl stop) and continue by planning how you will test things like access and authentication controls, server side includes, and other exercises.
Your Apache download should auto-install itself; probably creating itself as C:\Program Files\Apache Group\Apache. It will create an entry in your Windows "Start" control; here you will find options for starting and stopping your Apache web server.
You will probably have to modify the httpd.conf file before anything works. The
Apache web server likes to know the name of the machine on which it is running; the
name can be defined in the httpd.conf file or can be discovered using DNS. If your
httpd.conf file does not provide a name, Apache tries to contact a DNS server. Of
course, you probably don't have a DNS server on your machine; so Apache will immediately
give up. Edit the httpd.conf file and set ServerName localhost.
(If you download a current version from Apache, this should be taken care of
automatically during the installation procedure - a dialog pops up asking for
a servername)
Use the Windows Start control (Programs/Apache Web Server/Start Apache) to start your server, then aim your browser at http://localhost (your Windows Apache will use port 80).
Once again you will see the standard Apache welcome page, again language preferences apply so you can get the page in a variety of languages. You can follow the link on the page to your local copy of the Apache documentation.
Normally, the cgi-bin directory for the Windows installation is empty - there are no predefined cgi programs for you to run.
You will eventually need to install a Perl system by download, or from a CD. I got my Perl from the ActivePerl site. It installed Perl in C:\Perl. I added the following script to my Apache's cgi-bin directory:
#!/Perl/bin/Perl5.6.1
print "Content-type: text/html\n\n";
print "<html><body>";
print "<h1>Environment variables</h1>";
while(($name,$value) = each(%ENV)) {
print "<li>$name : $value";
}
print "</ul>";
print "</body></html>"
The #! line has to identify the location of the Perl interpreter. You will need to modify the example if you have a different version of Perl or if you install it in a different directory.