These options are specific to Windows servers.-i: Install Apache As Service
The -i option is a short form of -k install and has the same effect. The following two commands are equivalent:
-k: Issue Apache Service Command
The -k option is used to run Apache as a service on Windows and takes a command as an argument. The -k install and -k uninstall commands install and uninstall Apache as a service, respectively. They don’t, however, actually cause Apache to start or stop; the -i and -u options are shorthand abbreviations for these commands. For example, to install Apache under its default service name of Apache, use this:
To specify a different service name, use -n in addition to -k. Other Apache options may also be specified to configure the service, as, for example, an alternative document root:
To modify the configuration of an existing service, use -k config instead:
The -k start command actually starts Apache running as a service. Once running, the -k restart and -k stop commands restart and stop the running server, respectively. -k shutdown is a synonym for -k stop:
To install and run Apache with a specific service name, -k can be combined with the -n option:
-N: Specify Apache Service Name
If Apache is being installed, uninstalled, or otherwise controlled as a service, -n specifies the service name. If Apache was started as a service using the default service name, then -n isn’t necessary. It has meaning only if used in combination with the -i, -u, or -k options, as shown previously and next. (On NetWare, -n renames the console screen instead.)-u: Uninstall Apache As Service
The -u option is a short form of -k uninstall and has the same effect. The following two commands are equivalent:
-w: Keep Console Window Open
If Apache isn’t started from a command line, then this option preserves the initial console window for the given number of seconds, allowing you to see any important startup messages before the window closes. Normally, the transient console window closes almost immediately. To configure an existing service to keep it open for 10 seconds, use this:
See the -E option for an alternative way to preserve and view startup log messages separately from the error log.-W: Specify Preceding Service
In conjunction with -k config, -k install, or -i, the -W option can be used to specify the name of the service that Apache is to be placed after on server startup. This determines the order in which it starts relative to other services, for example:
It’s possible to issue this command twice, from the bin directories of two different Apache installations, giving a different service name for each (for example, Apache2a and Apache2b).Restarting the Server
There are several reasons you might want to restart Apache rather than shut it down, for instance, to read in an altered configuration if you have changed one or more configuration files.
Restarting is a gentler process than a full shutdown because it allows Apache to come back up as quickly as possible. In addition, you can do what is known as a graceful restart, which allows clients to complete their current request before being disconnected.
Restarting the Server Normally
The simplest way to restart an Apache 2 or Windows Apache 1.3 server is with this command:
Alternatively, any Apache server can be restarted using this command:
You can use the apachectl script to do the same thing as the first example:
This is actually identical to the kill command but is easier to remember. Naturally, you’ll need to have apachectl installed in order to use it. The advantage of the kill command is that it will always work regardless of the server version and whether apachectl is installed. In either case, the error log should then contain the following two entries regarding this process:
Restarting the server in this way takes it down and brings it back up again in the minimum possible time. As a result, it causes connections that are currently servicing client requests to terminate prematurely and also clears the queue of pending connection requests should any be waiting. As a way around this problem, Apache also supports the concept of a graceful restart.
Restarting the Server Gracefully
In a graceful restart, existing client connections aren’t interrupted as they are in a normal restart. The configuration file is reread, and all child processes or threads not currently engaged in servicing a request are terminated immediately and replaced by ones using the new configuration. Processes engaged in a client request are instructed to terminate themselves only after completing the current request. Only then will new ones replace them. The ports on which Apache is listening will not be shut down during this process, preserving the queue of connection requests. The exception to this is if a port number was changed in or removed from the configuration file—then the queue is dropped, as you would expect.
Graceful restarts make use of the USR1 signal, but, other than this, they’re identical to the other restart and stop methods. To do a graceful restart the old-fashioned way, use this:
Apache 2 and Windows Apache 1.3 servers will also understand this:
The following two entries should then appear in the error log:
Wherever possible, a graceful restart should be used in preference to a normal restart, particularly in the case of a production Web server that’s potentially receiving a new request at any given moment.
A graceful restart is also useful for scripts performing log file rotation. In a graceful restart, child processes still engaged in an existing connection will continue to log to the original log files until they complete the request rather than terminate prematurely. Because the log files may still be in use for a short time after the restart, a rotation script should wait until all existing processes have completed before rotating the log files. It can do this either by monitoring the process IDs of the running server or simply waiting a “reasonable” period of time before commencing—a minute is generally more than adequate. See the discussion of log rotation in Chapter 9 for more about this.Stopping the Server
Stopping the server follows a similar pattern to restarting it using the old style:
or the new style:
Any of these commands will cause Apache to shut down all its processes (under Unix, as Windows has only one), close all open log files, and terminate cleanly with an error log message:
Note that if the server is shut down at a point of high activity, it may take a while for all the running processes to terminate. One upshot of this is that attempting to start the server back up again immediately may fail because the old instance is still not completely terminated (the continued existence of the httpd.pid file is an indication of this). The solution to this problem is, of course, to restart the server instead:
Starting the Server Automatically
Just as with any other network service, Apache should be started automatically when the server is booted. This is especially important if you want the server to be capable of being shut down and started up again without our manual intervention, which is usually the case for a production system. The exception is a development server, where the Apache configuration may not yet be stable or secure enough to run full-time.
Configuring Apache As a Unix Service
On Unix systems, the standard place for stand-alone boot-time scripts to run from is /etc/rc.d/rc
Given the existence of rc.local, add the following lines to the bottom to automatically start Apache:
or, using apachectl:
Alternatively, an Apache start/stop script—for example, apachectl—can be installed into the standard run-level directories /etc/rc.d/rc.<n>
First, copy or (preferably) link to apachectl from the init.d directory, for example:
Creating a link will ensure that if you upgrade Apache, the apachectl that came with it is automatically used rather than an older and potentially incompatible version.
Next, create start links that point to the new init.d entry, according to when you want Apache to start. The run level for networking is 3, and for graphical desktops 5, so you need to add a link from both rc3.d and rc5.d (if you don’t intend to run a graphical interface, you can ignore rc5.d), like this:
Startup scripts are always named with an S followed by a number, which serves to determine the order in which the operating system will run them. In this example, I’ve picked 30 to ensure it starts after other essential services, such as the network. If you have Apache automatically making database connections, then you’ll also want to make sure the database starts up first. If in doubt, start Apache toward the end of the initialization to ensure all prerequisite services are started first. Apache failing to start because a prerequisite service did not start before it can be very difficult to troubleshoot, especially for someone not fully familiar with the nuances of Unix.
Now, create stop links that point to the new init.d entry, according to when you want Apache to stop. This is generally the opposite of the start order—you want Apache to stop first, then the network, to give Apache a chance to clean itself up and exit rather than pull the carpet out from under it:
With this in place, Apache should now start and stop automatically with the server.
Prebuilt packages for Apache generally come with suitable scripts to start and stop Apache. In many cases, they’re just a wrapper for apachectl; in others, they perform various kinds of integration with the rest of the operating system. If one is available, it’s generally better to use it. For the more adventurous administrator, it’s also quite possible to download a prebuilt Apache package, extract the scripts from it and throw the rest away, build Apache from scratch, and install the scripts by hand.
Configuring Apache As a Windows Service
Running Apache as a Windows service is not greatly different from the stand-alone approach described previously. However, it allows Apache to be administered more easily through the operating system Services window. You can also use the Apache Monitor, by default installed into the system tray, to control the running server and access the Services window.Starting Apache as a Service from the Start Menu (Windows NT, 2000, and XP Only)
The simplest way to start Apache as a service is to install it as a service to begin with. It can also be installed, uninstalled, and reinstalled by using the -n and -k command line options, which allows you to change the service name. Different command line options (notably -f and -D) can also be specified with different service names to create multiple configurations, each running as a different service but using the same installed software.
Apache may now be started and stopped at any time by opening Control Panel -> Administrative Tools ->ervices Window and selecting the Apache (or Apache2) service—clicking the Start button starts the service, clicking the Stop button stops the service, and clicking the Restart button restarts the service. Figure 2-1 shows Apache installed and running as a service with the name Apache2 in the Services window.
Note that the Startup Type column indicates that the Apache2 service currently has to be started manually. To start the Apache2 service automatically, follow these steps:
Once this is done, Apache will now start up automatically, and the Services window should reflect the changed value of the Startup Type option (see Figure 2-3).
Starting Apache As a Service from a Console Window
From a console window, you may start Apache as a service from a command prompt by entering this:
Likewise, to stop a running service, use this:
To install Apache as a service, use the following command:
This doesn’t actually start the server—it only installs it as a Windows service so that Windows knows how to interact with it.
You can also specify any number of general Apache options, which are recorded into the registry and used when the service is started. This allows you to specify the server root, configuration file, defines, and so on:
You may install several different Apache services by specifying a different service name for each invocation of the above command. However, each configuration must use a different set of Listen directives, or the services will clash when they try to bind to the same network address and port.
Once Apache has been installed as a service, you can start it from the Services window or from the command line using this:
One command must be issued for each service installed. In combination with the -f, -D, -c, and -C options, this can be used this to run different servers with different capabilities, each serving its own Web sites. Note that at the minimum you must specify different ports for each service to listen to; otherwise, they will conflict, and only one will be able to start.
Conversely, if you used the default service name (which differs depending on whether version 1.3 or 2 is installed), you can omit the -n option entirely:
If both versions are installed, this command is ambiguous, so to be safe, it’s better to specify the service name even if it’s the default.
Once Apache is running as a service, it can be stopped again from the Services window or by entering this:
Similarly, to restart it, use this:
or, to remove Apache as a service, use this:
You can also reconfigure the service using the -k config variant, after which you may restart the server to have it pick up the new configuration:
Most of Windows NT/2000/XP services log errors to Windows’ own system logs. Because Apache has more demanding and specialized logging requirements, it continues logging its errors to the error.log file in the logs folder, as described previously.
blog comments powered by Disqus