Chapter 3. Starting, Stopping, and Restarting

 

There are two kinds of fool. One says, “This is old, and therefore good.” And one says “This is new, and therefore better.”

 
 --The Shockwave Rider—John Brunner

Now that your server is installed, you need to get it running. Don't be concerned that we've devoted an entire chapter to this. It really is simple. But, there are some additional things that you'll want to know about starting, stopping, and restarting your server.

apachectl

Most of the time, you want to use apachectl to start and stop your server. apachectl is a handy little script designed to take the grunt work out of starting, stopping, and restarting Apache.

You'll find apachectl in the bin directory of wherever you installed Perl. You might want to copy, or symlink, it into /usr/local/bin, or somewhere else in your path, because you might need it frequently, at least while you're learning about Apache.

apachectl can be run with one of eight different options.

A complete listing of the options can be obtained by running apachectl with the help option:

% apachectl help

usage: /usr/local/bin/apachectl (start|stop|restart|
        fullstatus|status|graceful|configtest|help)

start      - start httpd
stop       - stop httpd
restart    - restart httpd if running by sending a SIGHUP or start if
             not running
fullstatus - dump a full status screen; requires lynx and mod_status enabled
status     - dump a short status screen; requires lynx and mod_status enabled
graceful   - do a graceful restart by sending a SIGUSR1 or start if not running
configtest - do a configuration syntax test
help       - this screen

These options are, for the most part, self-explanatory.

status and fullstatus will be covered in more detail in Chapter 14, “Handlers and Filters,” when we discuss mod_status. configtest will be discussed in more detail when we discuss configuration directives in Chapter 4, “Configuration Directives.”

The start, stop, restart, and graceful options are of particular interest to this section of the book. The first two do exactly what you would expect. restart does what you would expect also, but it's important to know that it also rereads the configuration files, so if any changes have been made, those changes go into effect on a restart. graceful is useful because any connections to the server that are currently active will be completed before the server is restarted. This is important because it means that nobody's connection will be unceremoniously dropped in the middle of receiving a file, therefore, the perceived downtime of the server will be lessened.

For both restart and graceful, if there is a syntax error in the configuration file(s), Apache will not successfully restart. However, Apache is smart enough to check the syntax of the configuration files before shutting down, rather than waiting until it is time to start back up. So, if there is a problem with the configuration files, Apache won't even shut down in the first place. This was not true with earlier versions, where a restart would occasionally leave you with your server down, if you did not check your configuration syntax before attempting the restart.

httpd

httpd is the server binary—that is, this is the actual executable file that is run when you start up your Apache server. You'll find this file in the bin directory of your Apache directory tree. The size of this file will vary depending on what you have built into your server, but it will be around 1MB in size, and will be the largest file in that directory.[1]

apachectl is just a wrapper that feeds arguments directly to the httpd binary for common options. There are a number of other things that you can do by passing arguments directly to httpd.

Starting and Stopping with httpd

To start the Web server, without any special options, simply invoke httpd directly:

/usr/local/apache/bin/httpd

To stop the Web server, you'll need to know the PID (process ID) of the Apache parent process. You can acquire this PID from the file httpd.pid, which is located in the directory with your log files—usually /usr/local/apache/logs.

cat /usr/local/apache/logs/httpd.pid | xargs kill

Command-Line Flags

By passing additional arguments to httpd, you can have it behave in ways slightly different from what you have configured in the server configuration files.

For a complete listing of the available command-line options, invoke httpd with the -h option:

# /usr/local/apache/bin/httpd -h
Usage: /usr/local/apache/bin/httpd [-D name] [-d directory] [-f file]
                                   [-C "directive"] [-c "directive"]
                                   [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]
Options:
  -D name          : define a name for use in <IfDefine name> directives
  -d directory     : specify an alternate initial ServerRoot
  -f file          : specify an alternate ServerConfigFile
  -C "directive"   : process directive before reading config files
  -c "directive"   : process directive after  reading config files
  -v               : show version number
  -V               : show compile settings
  -h               : list available command line options (this page)
  -l               : list compiled-in modules
  -L               : list available configuration directives
  -t -D DUMP_VHOSTS: show parsed settings (currently only vhost settings)
  -t               : run syntax check for config files (with docroot check)
  -T               : run syntax check for config files (without docroot check)

We'll come back to a number of these options as we talk more about the surrounding information necessary to understand what they do, but of particular interest at this time are the following options:

  • httpd -vShows you what version of the server you are running. This is a good way to convince yourself, after a new install, that you are in fact running the new version, and not some old version that happened to be laying around in your path somewhere.

  • httpd -VTells you what compile settings were in effect when you built Apache.

  • httpd -lLists the compiled-in modules. This will give you some assurance that what you did in the configuration phase actually paid off, and you got the modules that you wanted.

Starting on System Startup

If you are going to run Apache on a production system—that is, if you want people to be able to look at your Web site all the time—then you need to make sure that Apache starts up when you reboot your computer.

This is accomplished a variety of ways on different systems, but they are all typically just a small variation on the same theme.

In /etc/rc.d you will find a collection of scripts, typically with filenames starting with . (such as rc.inet1, rc.firewall, and rc.local) which run when your system starts up. The exact layout of this directory varies. Sometimes these scripts are located in subdirectories. Sometimes there are several subdirectories, corresponding to the various runlevels[2], containing the scripts, or symlinks to the scripts, and sometimes the scripts are all directly in /etc/rc.d.[3]

When you know how your system does things, you should create a startup script (called rc.httpd, for example) which contains a call to either apachectl or httpd, as you prefer, telling it to start your server. Alternately, many systems have a script called rc.local, which is specifically for you to put in your customizations to the startup process; this enables you to keep all your startup alterations in the same place.

So, for example, you can add the following line to either your rc.local, or to a separate file called rc.httpd:

/usr/local/httpd/bin/apachectl start

Or, if you want to start Apache with a configuration file other than the one in the default location, you could use the following line instead:

/usr/local/httpd/bin/httpd -f /etc/httpd/host2.conf

Microsoft Windows

As should be expected, Microsoft Windows does things a little differently. Apache on Windows offers a few other ways to manage things, more in line with the expected Windows way of doing things. For example, you can install Apache as an NT service, or start and stop it from the Start menu. However, there are also several ways to start and stop Apache from the command line as well.

Starting from the Command Line

The various command-line options previously listed for httpd also work on Windows, except that the executable on Windows is called apache, and is (by default) located in c:program filesapache group

cd "program filesapache group"
apache

After doing this, you might need to press control-C to regain control of your command prompt. Apache will continue running. Make sure that you can start Apache in this manner and that it is serving pages correctly before you proceed to the next step, to ensure that things are mostly set up OK.

Installing as a Service

As previously described, if you are running a production server, you'll want to install Apache as a service. This is done with the following commands.

To install Apache as a service with no special options, do the following:

apache -k install -n "service name"

If the "service name" option is omitted, the service name "Apache" is used as the default name of the service.

If you want to install the service to use a particular configuration file, different from the default location for configuration files that you specified during the installation process, you can specify a different configuration file when you install the service. This way, you can have multiple Apache services, with different configurations.

apache -k install -n "service name" -f "path	oalternateconf"

To remove the Apache service after it is installed, you can use the following command:

apache -k uninstall -n "service name"

Starting and Stopping Your Apache Service

After you have Apache installed as an NT service, you can start and stop it a number of different ways.

apache -n "service name"

You can call the Apache binary directly from the command line if you like, passing it arguments as we discussed previously with httpd. To start, restart, or stop the Apache service, you would use the following three commands, respectively:

apache -n "service name" start
apache -n "service name" restart
apache -n "service name" shutdown

Where, in each case, "service name" is whatever you called the service when you installed it.

NT NET Command

Alternatively, you can use the NT NET command, which can start and stop any NT service:

NET START "service name"
NET STOP "service name"

Services Control Panel

Finally, there's the NT GUI (graphical user interface) way to do things. NT provides you with a Services dialog, which lists all of the NT services, and enables you to start and stop each one. Additionally, it enables you to set properties on each service, such as whether they start automatically on server boot.

The Services applet is located in the Control Panel.

Console Application

Under Windows 9x (Windows 95, Windows 98, and Windows ME), Apache does not, by default, run as a service. Windows 9x, not being server operating systems, do not have the concept of a service.[4]

Consequently, under Windows 9x, Apache runs as a console application. Which means, as it sounds, that Apache runs in a console (DOS window), and that console stays open for the entirety of the time that Apache is running. This is a little less than convenient, but then you really should not be using Windows 9x as a server platform, so hopefully this will only inconvenience you during testing.

If you are running Apache as a console application, you will launch it from the Start menu, in the Apache submenu. This will open up a console in which you will see an indication that Apache is running. To shutdown Apache, or restart it, the recommended method is to open another console, and type the commands

apache -k shutdown

to shutdown the process, and

apache -k restart

to restart it.

Doing this with the -k option, rather than just pressing control-C in the console window, or just closing the console window, is preferable, because it enables Apache to shutdown cleanly, rather than abruptly disconnecting from any open connections. However, because you are hopefully not running this in a server environment, it probably does not matter much.

Summary

Apache provides a variety of ways to start, stop, and restart your server. This makes it easy to automate sthese processes, and ensure that your server is always running when it needs to be.



[1] Note that if you installed Apache from a binary distribution, or built Apache with a file and directory layout different from the standard, httpd might be located elsewhere. See Chapter 2, “Acquiring and Installing Your Apache Server,” for more information.

[2] A Unix machine can start up in one of several runlevels, or modes of operation, such as single-user mode or multiuser mode. The details of this are not particularly important for this discussion. Consult your OS documentation for further information.

[3] And, of course, other systems put it yet other places. Some systems, such as HP/UX, SuSE, Digital Unix, and others, like to put these startup scripts in /sbin/init.d or /sbin/rc[0-6].d. It can be a little hard to keep up with. Contact your local guru if you don't know how your system does things.

[4] Yes, there are third-party applications that enable you run things as services. They are, however, not part of the base operating system.

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

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