<<

. 47
( 69 .)



>>

Web server, visitors cannot view or download files index.html and login.jsp, and the document root
outside of this directory. contains neither of these files, Apache returns to you
one of two things: an error code or a directory listing.
The configuration on the server end also determines The choice depends on whether directory browsing is
which file the server transfers to you when you type enabled.
www.dragonflyditch.com in the URL field. Whenever




BEHIND THE SCENES


While a GET / request may at first appear to be a very While the protocol that the Web uses to move files and
modest service request, retrieval of the home page data back and forth is itself fairly simple, with only a
for most Web sites results in a number of requests as handful of commands such as GET and POST, the
each frame and each image contained in the file is communications between client and server often
subsequently requested. Until your browser obtains all include a lot of additional information. Much of this is
the parts of a page from the server, the Web page is contained in the headers that you send with your
incomplete. simple requests. Through the use of these headers,
clients and servers can compare notes on what each of
While the earliest Web servers delivered static Web them is capable of doing. For example, a Web server
pages to their clients, Web servers today often produce does not send you a file in a format that your browser
custom pages. For example, they may offer forms for cannot display. When multiple sites are supported on a
you to fill out whose contents are posted, or sent to the system, and differentiated only by their names ” such
server for additional processing, or they may call on as the difference between dragonflyditch.com and
encryption services to hide sensitive data from prying idyllmtn.com ” the server knows which site you are
eyes as the data transfers to the server for processing. requesting because the site name is included in the
headers.




229
UNIX FOR MAC



START APACHE
cannot commandeer these processes, and that these

Y
ou can start your Apache server after you install the
processes do not offer up files that only a privileged
software. The Apache installation includes a script
user should access.
that facilitates starting and shutting down the service.
When Apache is not running, you can start it by typing
By starting up multiple servers, Apache can handle
apachectl start. The Apache bin directory must be on your
considerable traffic. While all connection requests from
search path for this command to work without a pathname.
clients arrive at the same port, Apache turns over each
You will probably find Apache installed in /sw/apache, or
request to one of the idle httpd processes. When all of the
usr/local/apache, and the script is bin/apachectl, but you
processes are occupied, Apache can start others. The ratio
can determine if it is on your search path by typing which
of busy to idle processes depends on specifications in the
apachectl. If you are using the Apache server that ships with
Apache configuration file. The number of processes grows
Mac OS X, look in /usr/sbin.
and shrinks in such a way that the server is always prepared
for additional traffic, but not so over-prepared that the
When Apache starts, you can examine the processes
overhead is excessive.
running on your system; you can expect to see several
processes named httpd. In a normal configuration, one of
When you install Apache, it is generally configured to start
these processes is started by root, while another five to
automatically when the server boots. However, if you change
eight are started by a user called nobody. Nobody is a
the Apache configuration file and want the changes to take
special user with no particular privileges on a Unix system.
effect immediately, you must restart the server with the
By running Apache processes as nobody, you ensure that a
apachectl restart command.
hacker


START APACHE




– A list of the Apache
„ Type sudo ‹ If a prompt appears, type › Type ps -ax followed by a
your password, and press space. processes appears.
/sw/apache/bin/apachectl
followed by a space. Return.
ˇ Type | grep http and press
– Apache starts running.
¤ Type start and press Return.
Return.

230
14
SERVE WEB PAGES WITH APACHE



STOP APACHE
Before you attempt to stop Apache, you may first want to

Y
ou can stop your Apache server at any time. To stop
confirm that it is running. You can do this in one of two
Apache, you can use the apachectl script with a
ways. You can use the apachectl script with a status
stop argument. This stops all of the Apache processes.
argument; however, this technique only works if you also
Both the stop and start commands are arguments to the
have lynx installed. You can also run the command ps -
script that you supply to Apache to control the operation.
aux | grep httpd to view the processes that are
This script can also respond to restart and status
running. If you run this command, you must be careful not
arguments. The restart command shuts down and
to mistake your grep command for an Apache process.
restarts Apache, while the status command tells you
whether or not Apache is running.
You can also determine whether the HTTP port is listening
for client requests. If Apache is not running, no processes
When you stop Apache, you terminate the server process.
should be listening for connections on port 80. You can
The server is no longer listening on the port assigned to
perform this test with the netstat command. The
HTTP, and clients who continue requesting service receive a
command netstat -a | grep http returns a LISTEN
message indicating that their browser has encountered a
message when Apache is running. The output looks like this:
DNS error, or that it cannot find the server. This indicates
that the client is not finding a responsive process on the
tcp46 0 0 *.http *.* LISTEN
HTTP port.




STOP APACHE




‹ If there is a prompt, type ‹ Type | grep http and press
STOP THE APACHE SERVER CHECK FOR LISTENS
ON THE HTTP PORT
your password, and press Return.
„ Type sudo
„ Type netstat followed by a
Return.
– No LISTENs are reported
/sw/apache/bin/apachectl
space.
– Your Apache processes
followed by a space. for the HTTP port.
¤ Type “a followed by a
stop.
¤ Type stop and press
space.
Return.
231
UNIX FOR MAC

CONFIGURE APACHE
The default location for Web pages is the htdocs directory

Y
ou can configure Apache to run according to your
in your Apache directory ” for example, /sw/apache/
specifications. Configuration parameters for Apache
htdocs. When you install Apache, the installer puts a small
reside in a single configuration file, and you can make
set of files into this directory to act as a placeholder for
configuration changes by editing this file. This configuration
your intended content. If you start Apache and visit your
file is called httpd.conf, and it contains a great deal of
Web site without doing anything else, you see these
explanatory text and examples to simplify the process of
documents. The logs directory stores log files that contain
configuring your server. In fact, if you want to set up a Web
records reflecting each page that transfers from your site,
site very quickly, you can simply install your custom Web
including information on the client such as when they visit
pages in the default document directory and start Apache
and from what system. The configuration directory,
with the apachectl start command.
containing your all-important httpd.conf file, is called
conf ” for example, /sw/apache/conf.

APACHE DIRECTIVES


Configuration commands in Apache are called their request. If you do not set these two variables
directives. The basic form of a directive is the name of correctly, your Web server does not work as you intend.
the directive followed by the value that Apache assigns,
The ServerRoot is the directory in which the
such as User nobody. Each directive controls some
configuration and log file directories reside. If your
aspect of how your Web server runs. While there are
Apache ServerRoot is /sw/apache, your configuration
several hundred possible directives, you are not likely
files reside in /sw/apache/conf, and your log files in
to make many changes in your default configuration
/sw/apache/logs. In fact, your Apache binary is likely
file. If you are curious, you can access a complete list of
to reside there as well, in /sw/apache/bin.
Apache directives by visiting www.apache.org. The set
of available directives depends, in part, on the version
Although there are many other critical directives, most,
of Apache that you are running.
if not all of these, default to very reasonable and secure
settings. There are two forms of Apache directives:
The most critical directives are DocumentRoot and
simple directives and block directives. Simple directives
DocumentIndex. The DocumentRoot directive tells
set a value for a specific setting, such as the port the
your Apache server where to go to retrieve pages when
server runs on. Block directives group other directives
requests arrive from clients. The DocumentIndex
into a set, such as the settings you want to apply to a
directive is a list of files that tells your server what files
virtual Web server.
to look for when a client does not include a filename in



BUILT-IN APACHE SERVER


Mac OS X ships with a built-in Apache server that you that Apache uses, including the location of your default
can configure and run without having to download and configuration file. The location of this file appears in a
compile it from source code files. To start the server, line that reads “D SERVER_CONFIG_FILE=”/etc/httpd/
you can click System Preferences ➪ Sharing. When the httpd.conf”.
Sharing panel appears, you can select Personal Web
You can use the command grep ^LoadModule
Sharing, and then click Start. If you open a Web
/etc/httpd/httpd.conf to list any modules that are
browser and type http://127.0.0.1 in the URL field, a test
enabled. For example, cgi_module appears in the
page appears.
output of the grep command, indicating that CGIs are
The built-in Apache server is identified on your system enabled. It is a good practice to store CGIs that you
as /usr/sbin/httpd. If you type /usr/sbin/httpd “V, a list develop in the /Library/WebServer/CGI-Executables
appears which contains information about the settings directory.

232
14
SERVE WEB PAGES WITH APACHE
BUILT-IN APACHE SERVER


To enable PHP, you can remove the initial # symbol PHP Test File
from the following lines in your httpd.conf file: <html>
# LoadModule php4_module; # AddModule <body>
mod_php4.c; # AddType application/x- <? phpinfo()?>
httpd-php .php; and # AddType application/ </body>
x-httpd-php-source .phps. You must then stop </html>
and restart Apache. After restarting, you can create the
PHP test file and save it as /Library/WebServer/test.php. The built-in Apache server also initializes personal Web
To confirm that PHP is working, you can type http:// sites for your users. For example, the personal Web site
127.0.01/test.php into the URL field of your browser; if for the user Smith, is defined in the file /Users/smith/
PHP is working, a table appears containing information Sites/index.html. You can find the access and error logs
about PHP. for the built-in Apache server in the /var/log/httpd file.




APACHE DIRECTIVES

<<

. 47
( 69 .)



>>