Chapter 12. Apache on Microsoft Windows

 

“Better run for your life!” cried the Mad Hatter

“Alright,” said Alice…”I'm going back…

To the other side of the mirror”

 
 --Alice—The Other Side of the Mirror—Stevie Nicks

Apache 1.3a1 was released in July 1997. It was the first official version of Apache to run on Microsoft Windows. There were attempts prior to this to port Apache to Windows, some of them actually fairly successful. But this was the first official release from the Apache Group that you could download from apache.org and install on Windows.

However, throughout the 1.3 releases, the Windows documentation contained the following warning:

  • Warning: Apache on NT has not yet been optimized for performance. Apache still performs best, and is most reliable on Unix platforms.

Earlier versions also contained this more ominous warning:

  • Please note that at this time, Windows support is entirely experimental, and is recommended only for experienced users. The Apache Group does not guarantee that this software will work as documented, or even at all.

These warnings were hardly conducive to establishing trust and drawing in new users. But the fact is that Apache for Windows was done because the folks in the ASF realized that we needed Apache to run on Windows. As such, a lot of effort, even brute force when necessary, went into simply getting it working, with the understanding that after it was working, an effort would be made later to go back and do it right.

In version 2.0, Apache for Windows is done right. It takes advantage of Windows' internal API in ways that the 1.3 version never did. It is much faster, and much more stable.

The installation process is much cleaner in Apache 2.0, as it determines certain configuration directives for you based on your system configuration. For example, it will determine your hostname and make sure that you are not already running any service on port 80 that would conflict with Apache. Also, the code uses native NT networking calls for much improved performance.

Requirements

Apache for Windows is designed to work on the NT family of operating systems—Windows NT 4.0 and Windows 2000. Windows 95, 98, and ME are similar enough to Windows NT that Apache will also run there, but there are some features that don't quite work as desired. This is probably just as well, because you really should not be running production network services on an operating system that is designed to be a desktop OS, not a server OS. However, Apache runs well enough on Windows 9x that it is good for Web site testing before you move your site to a real server.

If installing on NT, it is recommended that you have Service Pack 6 installed. Service Pack 6 contains a variety of fixes to the TCP/IP stack. If running on Windows 95, you will need to install the Winsock2 upgrade in order for Apache to work at all.

Downloading

The installation files for Microsoft Windows are located on the Apache Web site at http://httpd.apache.org/dist/httpd/binaries/win32/. The distribution is available in several different packages. With the 1.3 version, Apache is available as an MSI (Microsoft Installer) package and as an InstallShield package. The MSI is the preferred distribution, as it makes it easier to track where files were placed, and thus easier to correctly uninstall the files if necessary.

If you are running Windows 95 or 98, you might need to install the Microsoft Installer, which you can get from the Microsoft Web site at http://www.microsoft.com/downloads/release.asp?ReleaseID=17343.

Installation

Because most Windows machines will probably not have a compiler installed, the Windows distribution comes as a batch of compiled binaries. Installing these binaries is very simple, and should be a familiar process to any Windows user. Simply click on the file that you downloaded and answer a few simple questions to have the necessary files installed in the appropriate places.

After the initial splash screen, you will be presented with three screens on which you will need to make configuration decisions. Figure 12.1 shows the first screen.

The Apache Installation Wizard

Figure 12.1. The Apache Installation Wizard

The first screen asks for the domain in which the server will be running (such as example.com), the hostname of the server (such as www.example.com), and the e-mail address of the server administrator (such as [email protected]). You must also decide whether the server will be installed to run as a service (running with the privileges of the system account), or whether it should install only as the current user. The former choice (running as a service) is preferable, as it enables the server to start at system start time, whereas the latter choice requires that the server be started manually by the same user who is doing the installation.

The second screen (Figure 12.2) offers you the choice to install the complete package in the default locations, or to do a custom installation where you control more of the decisions. Choosing the complete installation is recommended for your first few times through this process.

Choosing the setup type.

Figure 12.2. Choosing the setup type.

Finally, the last screen (Figure 12.3) asks you what base directory you want to put everything in. The default location is c:Program FilesApache Group, but you can put it anywhere you like.

Picking a directory for installation.

Figure 12.3. Picking a directory for installation.

After this screen, the installer will place the files in the correct locations for you.

Installing as a Service

Apache can be run as an NT service, or on Windows 95 or 98, as something that roughly approximates the same behavior.

An NT service is the equivalent of what is called a daemon in the Unix world. It is a process that is running in the background, rather than in a foreground window or console. You can communicate with the service with either command-line functions, or with the Windows control panel services dialog.

In order to install Apache as an NT service, you will need to use the following command, typed at a DOS prompt:

apache -i -n "Apache"

The program file apache will be located in the directory where you installed Apache, and you might need to either change into that directory to run the command, or provide the full path to the file.

The -i flag indicates that Apache is being installed as a service.

The -n flag enables you to specify the name of the service as you want it to appear in the services dialog. If you do not specify a service name, the default is Apache.

The -f flag is optional, and is used only if you want to specify the use of a configuration file other than the default file.

Other flags might also be used in this command to alter the behavior of Apache when started as a service.

You can alter the arguments supplied to Apache by the service with the -k config argument. For example, if you wanted to change the location of the configuration file, you could do so with the following command:

apache -k config -n "Apache" -f "alternateconfig.conf"

This will alter the configuration in the registry so that the next time that the service is restarted, it will be started with this alternate configuration file.

If you want to uninstall the Apache service, use the -u flag:

apache -u -n "Apache"

Starting and Stopping Your Server

After Apache is installed, you will start and stop the server in a somewhat different manner than described for the Unix version. The method of doing this will differ depending on whether you have the server installed as a service, and whether you are running on Windows NT or Windows 9x.

If Apache is installed as an NT service, you can start and stop your server in one of three ways. There are two methods that can be used from the command line, and one via the Windows graphical user interface.

From the command line, you can use the apache program itself to start the service, using the -k command-line flag:

apache -n "Apache" -k start

Likewise, to stop and restart you can also use the -k flag:

apache -n "Apache" -k restart
apache -n "Apache" -k stop

Note: Apache on Windows does not come with the apachectl utility mentioned earlier in this book. (apachectl is discussed in Chapter 3, “Starting, Stopping, and Restarting.”)

You can also use the NT command-line tools provided to control services. These commands are the NET START and NET STOP commands:

NET START "Apache"

NET STOP "Apache"

Note that the quotes are only strictly required if the service name contains spaces. The NET START and NET STOP commands do not provide you with a method for passing additional arguments to the Apache server, so if you need to alter the behavior of the server from the defaults, you need to directly start and stop Apache yourself, rather than using the NT service tools.

Finally, you can use the NT services dialog. This dialog is accessible by opening the control panel and selecting the service icon. You can start and stop the service from this dialog. Windows 2000 also features a restart button that can be used to restart the service.

Note that Comanche will also run on Windows, and can be used as an administrative interface. See Chapter 5, “Configuration Utilities,” for more information about Comanche.

Starting and Stopping in Win9x

There are two main ways to start Apache on Windows 9x—as a console application or as a service. (As of version 1.3.13, there is experimental support for starting it as a service.)

Running as a Console Application

From a DOS prompt, change into the Apache directory and start up Apache:

cd "program filesapache groupapache"
apache

If you want to start Apache with a configuration file other than the one in the default location, you can specify the location of the configuration file with the -f flag:

apache -f c:apacheconfother.conf

Apache will run in this console window, and it must stay open as long as Apache is running. Closing this window will terminate the Apache server.

There is also a shortcut on the Windows Start menu labeled Start Apache As Console App that runs this same command line, starting Apache with the default configuration file. This too will launch a console (DOS prompt) that will need to stay open for as long as you want Apache to run.

Running as a Service

You can start Apache on Windows 9x as a service of sorts, in that it will run in the background rather than in a foreground window, as was the case until version 1.3.13. However, this is currently still labeled as experimental, and so is not guaranteed to work all the time, if at all.

After you have verified that Apache will run from the command line, you can use the following command to run Apache as a service from a shortcut on your desktop or start menu:

apache -n "service name" -k start

In order to stop Apache, you will need to run the following command, either from another DOS console or from a shortcut:

apache -n "service name" -k stop

Modules

Because everything is distributed as binaries, you have no compile-time options for adding and removing modules. Consequently, Apache for Windows is distributed with all the standard modules compiled as dynamic shared objects (DSOs), or, as they are called in Microsoft-land, dynamically linked libraries (DLLs). In your server configuration file, you'll need to specify which of these modules you actually want to load when your Apache server starts. This is done with the LoadModule directive, as shown here:

LoadModule speling_module modules/mod_speling.so

The LoadModule directive adds the named module to the active module list. The file path provided is relative to the ServerRoot, and because the file extension might vary from module to module, the full filename must be provided. The modules distributed with Apache have a .so file extension, while many third-party modules have .dll file extensions.

Differences Between Apache on Windows and Unix

For most purposes, Apache on Windows is exactly the same as Apache on Unix, and everything else in this book will apply. However, there are some important differences between Apache on Windows and Apache on Unix, and these are primarily in the area of configuring your server. There are, however, a few differences in other areas as well.

Threading Versus Prefork

Apache on Windows is multithreaded, as opposed to Apache on Unix, which is a single parent with multiple preforked[1] child processes. Consequently, the various directives that deal with the creation of new httpd processes are not relevant for Apache on Windows, and are replaced instead with several others that deal with the creation of threads.

The particular directives that are ignored by Apache on Windows in this context are MaxSpareServers, MinSpareServers, and StartServers, which specify the number of child processes that you have at any given time. These directives are discussed in more detail in Chapter 13, “Performance Tuning.”

In their place, we gain the ThreadsPerChild directive, which specifies how many threads your Apache process should permit to be used. Note that because threads are roughly analogous to child processes in the Unix model, this number also limits the total number of simultaneous connections that will be permitted to your site at any time.

In Apache 2.0, we also get MaxSpareThreads and MinSpareThreads, which are directly analogous to the MaxSpareServers and MinSpareServers directives and govern the lower and upper limits on the total number of idle threads at any given time. These threads are in addition to any threads that are currently serving client requests, and are kept waiting in the wings to serve any additional incoming requests. Apache will create additional threads as the idle pool gets used up, and reap unused threads that exceed the configured maximum when load trails off.

Apache 2.0 greatly reduces the number of differences between the Windows and Unix versions of the server by introducing platform-specific multi-processing modules (MPMs). These modules provide a variety of different ways to handle multiple processes serving incoming client requests. By choosing a different MPM, you can determine whether a threaded model, a pre-fork model, or some other model is used. The MPM for Windows NT, called mpm_winnt, is specifically written for Windows NT and takes advantage of specific Windows API calls that result in better performance and stability.

With Apache 1.3, this model is entirely determined by the platform on which you are running.

MaxRequestsPerChild

As mentioned previously, Apache on Windows is multithreaded. There is a single child process containing many threads, each of which serves requests.

The MaxRequestsPerChild, as the name suggests, is the maximum number of requests that a particular child process is permitted to serve before that child will be reaped. In the Windows single-child model, it is undesirable for the child process to ever be reaped, as this will take your server down. Of course, Apache will automatically launch a new child process to take the place of the one that was reaped, but this has two undesirable effects.

First, it means that you will have significant server downtime, from a few seconds to possibly a minute or more, depending on the number of modules you have loaded and the complexity of your configuration file.

Second, if you have made changes to your configuration file(s), but have not restarted your server to read in those new changes, these modifications will be read in and applied at this time. Restarting the only child process is equivalent to a complete server restart, and so the configuration files are re-read. This can have undesired or unexpected effects when a configuration changes and you had not intended to apply the changes yet.

Consequently. MaxRequestsPerChild should under no circumstances be set to anything but the default 0 on Windows. A value of 0 means that the child is never to exit, no matter how many requests it serves.

CGI Scripts

When running CGI scripts under Windows, there are two ways that you can indicate what interpreter is to process the script. (The term “script” is being used here in the traditional sense to indicate an interpreted program, rather than a compiled one.) The default behavior here tends to be rather unintuitive to users used to ordinary Windows behavior, and so generates a lot of questions on various mailing lists and newsgroups.

Microsoft Windows determines what type of file it is dealing with by the file extension— characters that appear at the end of the filename after a period. Thus, a file named index.html, which has a file extension of html, is known to be a HyperText file because of the extension. This information—the mapping between file extension and file type—is stored in the registry, which is a database of configuration variables stored in the Windows environment.

Unix variants, on the other hand, determine file type based on a variety of different criteria, which usually involves looking at the file contents and applying a series of rules to figure out what sort of file it is. For example, a file containing a GIF 89 header in the first part of the file is sure to be a GIF image file.

For our particular purposes, of utmost interest is figuring out what sort of file we are dealing with when asked to execute a script. The typical Unix way of doing things is to look in the first line of the file for a reference to the executable program that will be responsible for interpreting and executing the contents of the file. For example, if the first line of the file looked like

#!/usr/bin/perl

then the shell would know that the contents of the file are to be fed to the program found at the file path /usr/bin/perl for execution. The #! in this line, which is typically called the shebang, indicates to the shell that what follows is just such an instruction.

Apache employs a hybrid of these two approaches, using a variety of directives to map particular file extensions to particular MIME types (see Chapter 8, “MIME and File Types,” for more details), but, in the case of CGI programs, employing the shebang line to tell it how to execute a particular CGI script.

On Microsoft Windows this is not a common practice, so a configuration directive is supplied to alleviate this confusion.

The ScriptInterpreterSource directive tells Apache whether it should look in the registry for a mapping from file extension to interpreter, or whether it should look in the first line of the file for a shebang line to determine this information.

Setting the value of the ScriptInterpreterSource directive to registry causes Apache to use the former method, while setting it to script tells it to use the latter method. The following examples show what each of these directives would look like. These directives go into your main server configuration file, or in a .htaccess file. It can be set on a per-directory basis by putting it into a <Directory> section.

ScriptInterpreterSource Values
ScriptInterpreterSource registry Look in the registry for file type mapping
ScriptInterpreterSource script Look in the script for a shebang line

.htaccess Files

.htaccess files enable you to specify per-directory configuration options in a file located in the particular directory. By default, the name of these configuration files is .htaccess. However, you cannot create files with a dot as the first character of the filename in Windows.

The name of the file in which Apache will look for per-directory configuration changes is set by the AccessFileName directive, which can be set to something more friendly to the Windows[2] file system. The configuration file distributed with Apache for Windows has this directive set to htaccess—that is, the leading dot is omitted.

File Paths

File paths are specified differently from one operating system to another. In Microsoft Windows operating systems, you must specify what drive letter you are working on, and paths are specified with backslashes rather than forward slashes. On Unix operating systems, no drive letters are specified—everything is usually on one logical volume—and the past separator is a forward slash. This leads to differences in configuration files when file paths need to be specified.

When specifying file paths in configuration files, you can use either forward slashes, which are used in Unix and are used for examples in the documentation, or you can use the backslashes that are used by Windows. But you should be consistent, and forward slashes are preferred. The reason for this is that the documentation consistently uses the forward slash in those parts of the documentation that are not platform-specific. In order to be able to directly use the examples without modification and to avoid confusion, you will want to use forward slashes in your configuration files.

The ServerRoot directive must be specified with the drive letter so that Apache knows what drive to look on for the files. This will look something like the following:

ServerRoot c:/apache

If you are specifying other directives on the same drive as the ServerRoot, you do not need to specify the drive letter. Otherwise, you will need to provide the whole path.

If you need to specify a file path that has spaces in it, you will need to have the entire file path in quotes; otherwise, Apache will think that the file path ends at the first space:

Alias /pictures/ "c:/photo album/images"

Summary

Apache on Windows is very similar to Apache on Unix in most respects. After you have it installed and running, administering it will be almost identical to what you would need to do on Unix.

With Apache 2.0, efforts have been made to make Apache on all platforms almost identical, with a pluggable MPM to determine how the particular OS best handles multiprocessing—either via forking or threading, or some combination thereof. Management of the rest of the server is similar, with only this one layer differing in order to communicate with the underlying operating system.



[1] Preforked means that the processes are launched before they are needed, so that there are always several waiting in the wings for incoming requests.

[2] This is not entirely true—there are ways to do it—but it's not a particularly good idea.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset