Starting and Stopping the HTTP Daemon


The Apache installation process in the ports collection will start the server automatically. You can verify this by looking for httpd processes using ps and grep:

# ps -waux | grep httpd root     220  0.0  2.0  4436 2456  ??  Ss Sat02PM  0:02.79 /usr/local/sbin/httpd nobody   303  0.0  2.0  4496 2548  ??  I  Sat02PM  0:00.01 /usr/local/sbin/httpd nobody   304  0.0  2.0  4460 2452  ??  I  Sat02PM  0:00.00 /usr/local/sbin/httpd nobody   305  0.0  2.0  4460 2452  ??  I  Sat02PM  0:00.00 /usr/local/sbin/httpd nobody   306  0.0  2.0  4460 2452  ??  I  Sat02PM  0:00.00 /usr/local/sbin/httpd nobody   307  0.0  2.0  4460 2452  ??  I  Sat02PM  0:00.00 /usr/local/sbin/httpd nobody 13963  0.0  2.0  4468 2468  ??  I  Sat10PM  0:00.00 /usr/local/sbin/httpd


Note that Apache 1.3 uses a forking model, where one "master" process (the one owned by root in the output shown here) listens on port 80 for incoming requests and then forks off a copy of itself (owned by the unprivileged pseudo-user nobody) as a child process to handle each request. The master process never actually serves any requests on its own.

Note

It's very dangerous to have a web server that can serve requests as root, because that means it can execute CGI programs as root tooa situation that's one small CGI script away from an exploit that can wipe your entire hard disk clean.


Under the forking model, where dozens of httpd processes might be running simultaneously, killing and restarting every one of these processes is impractical, to say the least. There's a better way to stop and restart the server, though. All you have to do if you want to stop the server is to kill the master process. To restart the server, send the master process a HUP signal (kill -HUP 220), upon which it will kill all its child processes, restart itself, and respawn its children.

Even this process is still fairly messy, though, and it involves using tools such as ps, grep, and kill in a fairly arcane manner. Fortunately, Apache provides us with a handy tool for this purpose: apachectl, which is installed into /usr/local/sbin and is therefore part of your default program path.

Note

In Apache 2.x, the apachectl support program is merged with the httpd program itself; if you call apachectl with any of the arguments we discuss here, it is actually passing those arguments to httpd, which now handles such commands internally. Therefore, the command httpd l (which we will discuss later in this chapter) is equivalent to apachectl l, and httpd start will have the same effect as apachectl start.

Apache 2.x is threaded instead of fork-based, meaning that it spawns many fewer httpd processes than does Apache 1.3.


Starting and stopping Apache using apachectl is quite easy:

# apachectl start /usr/local/sbin/apachectl start: httpd started # apachectl stop /usr/local/sbin/apachectl stop: httpd stopped


After you've made any changes to any of the files in /usr/local/etc/apache, you must restart Apache. This is also made easy by apachectl:

# apachectl restart /usr/local/sbin/apachectl restart: httpd restarted


The apachectl restart command does the equivalent of sending a kill -HUP to the master httpd process: All child processes will be killed, even if they're in the middle of serving files to clientsand those clients will have their connection abruptly dropped. If you want to restart Apache in a less-intrusive wayfor example, if you have a high-load server whose data integrity is criticalyou might choose instead to perform a "graceful restart," which uses a SIGUSR1 signal instead of a SIGHUP signal. This less-urgent signal allows the master process to restart itself without killing its child processes, meaning that in-progress transfers will not be dropped:

# apachectl graceful /usr/local/sbin/apachectl graceful: httpd gracefully restarted


One other common use for apachectl is the configtest command. This will cause Apache (whether it's running or not) to read in the config files from /usr/local/etc/apache and report any configuration errors, just as the start command wouldexcept that it does not actually start the server. This is an excellent diagnostic tool and an implicit part of both the restart and graceful commands. apachectl uses configtest to determine whether the configuration is valid before trying to restart the server; if the configuration isn't valid, it leaves the running processes alone. This prevents a restart or graceful command from inadvertently killing your server:

# apachectl graceful /usr/local/sbin/apachectl graceful: configuration broken, ignoring restart /usr/local/sbin/apachectl graceful: (run 'apachectl configtest' for details)


Tip

If Apache isn't running, apachectl restart or apachectl graceful will start it.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net