(Linux) Apache Web Server Admin PDF
(Linux) Apache Web Server Admin PDF
Administration
Course Objectives
Apache Web Server Administration will teach you:
• basic and advanced configuration directives.
• how to effectively work with and monitor the Apache server.
• how to implement Apache modules.
After completing this course, you will be able to apply your Apache
administration knowledge to configure a fully functional and robust
Apache server and diagnose a variety of access and performance
problems.
For these courses, plus many more, please visit us on the Internet at
http://www.itsinc-us.com/.
WELCOME 1
WELCOME TO APACHE WEB SERVER ADMINISTRATION 1
COURSE OBJECTIVES 1
COURSE STRUCTURE 2
ABOUT INTERNATIONAL TECHNOLO GY SOLUTIONS 2
TABLE OF CONTENTS 3
CHAPTER 1: INTRODUCTION 7
CHAPTER OVERVIEW 7
CHAPTER OBJECTIVES 7
OVERVIEW 8
APACHE'S STRENGTH WORLD -WIDE 8
APACHE'S OPERATING SYSTEMS 8
FEATURES 9
COMPARISON TO OTHER SERVERS 10
CHAPTER SUMMARY 11
Chapter Overview
Before using Apache, it is sensible to review the features it offers and how
it compares to other servers. In this chapter, you'll see the benefits Apache
gives administrators, and you'll see how Apache compares to other web
servers.
Chapter Objectives
After completing this chapter, you will be able to:
• describe the Apache web server.
• list Apache's features.
• compare Apache with other Web servers.
Chapter Overview
Installing Apache can be very simple or extremely complex. The range of
configuration possibilities that Apache offers is staggering, but the default
Apache installation is sufficient for many sites. This chapter will illustrate
the installation procedure and point out many of the configuration
parameters you can use to change the standard behavior.
Chapter Objectives
After completing this chapter, you will be able to:
• describe what factors influence web server placement on a
network.
• install Apache from either tar or rpm archives.
• configure your system to start Apache at boot.
• test Apache's configuration.
Untrusted users
When you will serve pages to any untrusted users, you'll need to take
several precautions to prevent unauthorized access to your server.
The general architecture for sites with untrusted users is:
Obtaining Apache
Obtaining Apache
You can download Apache from the World Wide Web, or you can find it
on your Linux operating system CD. For Red Hat Linux users, Apache is
automatically installed with the "server" install, but you can add it
manually by selecting the "Web Server" option during a custom install.
Apache’s web site, http://httpd.apache.org/, holds the latest
version for the Apache web server. This site provides the current release,
more recent beta-test releases (if available), and anonymous ftp sites.
Compiling Apache
The Apache web site distributes the Apache source code in a compressed
"tarball" format. After unpacking the archive, you must configure and
build the software for your system. The example below shows the
recommended procedure; it requires no intervention because the server
software is highly portable:
$ tar -zxf apache*.gz
$ cd apache*
$ ./configure --prefix=PREFIX
$ make
$ make install
The distribution will put the binary (httpd) and the standard
configuration files in your system-specific directories.
Configuration files
Directory Description
.htaccess Directory-based configuratio n
files. A .htaccess file holds
directives to control access to
files within the directory in
which it is located
/etc/httpd/conf Directory for Apache Web
server configuration
/etc/httpd/conf/httpd.conf Primary apache Web server
configuration file
Application files
Directory Description
/usr/sbin Location of the Apache Web
server program file and
utilities
/usr/doc Apache Web server
documentation
/var/log/http Location of Apache log files
The listing below (httpd.conf) shows the default modules that will be
loaded. Lines starting with a "#" are comments and are ignored:
# LoadModule foo_module modules/mod_foo.so
#LoadModule mmap_static_module modules/mod_mmap_static.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule env_module modules/mod_env.so
LoadModule config_log_module modules/mod_log_config.so
LoadModule agent_log_module modules/mod_log_agent.so
LoadModule referer_log_module modules/mod_log_referer.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule status_module modules/mod_status.so
LoadModule info_module modules/mod_info.so
LoadModule includes_module modules/mod_include.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule asis_module modules/mod_asis.so
LoadModule imap_module modules/mod_imap.so
LoadModule action_module modules/mod_actions.so
#LoadModule speling_module modules/mod_speling.so
LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule access_module modules/mod_access.so
LoadModule auth_module modules/mod_auth.so
LoadModule anon_auth_module modules/mod_auth_anon.so
LoadModule db_auth_module modules/mod_auth_db.so
LoadModule digest_module modules/mod_digest.so
LoadModule proxy_module modules/libproxy.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule example_module modules/mod_example.so
#LoadModule unique_id_module modules/mod_unique_id.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule bandwidth_module modules/mod_bandwidth.so
#LoadModule put_module modules/mod_put.so
# Extra Modules
#LoadModule perl_module modules/libperl.so
#LoadModule php_module modules/mod_php.so
#LoadModule php3_module modules/libphp3.so
System V style
For Red Hat Linux, which uses a System V-style interface to start services
at boot, you can configure Apache to start at boot with:
$ chkconfig -add httpd
BSD style
With other distributions, such as Slackware, you'll need to manually add
the Apache server to the system start-up scripts. For example, assume you
installed the server in /usr/sbin/httpd, then you'd put the following
at the bottom of /etc/rc.d/rc.local:
# /etc/rc.d/rc.local
/usr/sbin/httpd &
Chapter Overview
In this chapter, you will see a large collection of Apache's more popular
configuration parameters, and how they affect the operation of an Apache-
served web site. Understanding these parameters will allow you to tune
your Apache configuration to your sites' specific requirements.
Chapter Objectives
After completing this chapter, you will be able to:
• explain the difference between simple and block directives.
• list and describe the use of common Apache directives.
• enable CGI and SSI extensions.
Simple directives
Simple directives have global scope in Apache’s httpd.conf file and
take the form of the directive name followed by options. The syntax for a
simple directive is:
Directive Option Option . . .
For example, to set the server administrator's email address, you would
have the simple ServerAdmin directive set such as below:
ServerAdmin [email protected]
Block directives
Block directives hold configuration parameters that apply to specific
components. Block directives are entered in pairs; specifically, there is a
beginning and terminating directive.
The beginning block directive takes an argument that specifies the
particular component to which the directives apply, and the terminating
directive consists of a slash and the directive name designating the block’s
end. This syntax, which is very much like HTML containers, has the
following syntax:
<BlockDirective Argument . . .>
Directive Option . .
Directive Option . .
</BlockDirective>
TIP:
The .htaccess file inherits the configuration parameters of its
parent directory and any special configuration applied in the
httpd.conf file.
<Directory /home/*/public_html>
AllowOverride None
</Directory>
TIP:
You can change the directory access control filename from
.htaccess with the AccessFileName directive. For example,
AccessFileName .access sets the filename to .access.
You can use any number below 65535, as long as no other server is using
that port. The /etc/services file lists the ports normally associated
with particular servers, and you should check this file before randomly
adding a new port.
Should you decide to modify this directive, you must specify the parent
directory that holds the configuration, log, and module files. Within this
parent directory, there should be a directory named conf that holds
configuration information, logs that holds log information, and
modules that holds module files. On most systems, the logs and
modules directories don't reside in the parent directory; instead, they're
symbolic links to other directories in the filesystem.
Apache looks for these files when a browser requests a directory and not a
specific file. The first file found in the directory that matches an entry in
the DirectoryIndex list is used. If none of the files exists and the
Indexes option is in effect for the directory, Apache generates a
directory file index; otherwise, an error message is shown.
TIP:
The lock- file must reside on a local disk;
it can't be on a remote (e.g., NFS) filesystem.
Canonical hostnames
The UseCanonicalName directive (shown below) allows your server
to enforce name consistency. When set to On, Apache will always use the
ServerName and Port directives to create an explicit URL that uniquely
refers back to your server. This name, known as the canonical name,
enforces a consistent naming, which might be important for CGI scripts
that validate by hostname.
UseCanonicalName On
Cache configuration
By default, Apache sends a Pragma: no-cache header with each
content-negotiated document. This header asks proxy servers to not cache
the document, so that future requests to the document will force content
renegotiation.
Un-commenting the CacheNegotiatedDocs directive line disables
this behavior, which will allow proxies to cache documents:
#CacheNegotiatedDocs # uncomment to enable
At startup, and when operating in standalone mode, Apache will start one
master server, then start more servers as given by the StartServers
directive. Again, for average sites, the default is reasonable:
StartServers 8
Using the values specified above, when the daemon is started, the server
processes will run, waiting for connections. As more requests arrive,
Apache will ensure that at least 5 servers are ready to request connections.
When a request has been fulfilled and no new connections arrive, Apache
will begin killing processes until the number of idle Web server processes
is less than 20.
Safety nets
Apache can limit the total number of simultaneous server processes with
the MaxClient directive. The MaxClient directive should be
sufficiently high for your site's normal load. The default of 150 is almost
always large enough for most sites:
MaxClients 150
# 2) local redirects
# redirect to local URL /missing.html
ErrorDocument 404 /missing.html
# redirect to a script or a
# document using server-side-includes.
ErrorDocument 404 /cgi-bin/missing_handler.plx
# 3) external redirects
ErrorDocument 402 \
http://www.remote.com/error.html
If you have disabled all users, you can use the enabled keyword
followed by a space-delimited username list to allow these users access.
These usernames will have directory translation performed even if a global
disable is in effect, but not if they also appear in a disabled clause.
The following directive disables all users except "john":
UserDir disabled
UserDir enabled john mike
UserDir disabled mike
Example
Suppose a browser requests the URL:
http://www.company.com/~john/
The UserDir directive affects how this URL is expanded, as shown in
the following table 1 :
Directive Location
UserDir www /home/john/www/
UserDir /usr/web /usr/web/john/
UserDir /home/*/www /home/john/www/
UserDir http://www.home.com/ http://www.home.com/john/
UserDir http://www.home.com/users/j
http://www.home.com/users/ ohn/
UserDir http://www.home.com/~*/ http://www.home.com/~john/
1 The table assumes that user directories exist under /home in the local filesystem.
CGI by directory
Traditionally, these files were placed in the cgi-bin directory and could
only be executed if they resided in that specia l directory. Typically, a
Web site will only have one CGI directory.
Red Hat Linux sets the CGI directory, by default, to
/home/httpd/cgi-bin. You can set the ScriptAlias directive to
alter this default, as shown below:
ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/
CGI by file
It is also possible to configure Apache to consider any files ending in a
particular extension as CGI programs. The AddHandler directive
allows you to map a filename extension to some behavior within Apache.
For example, the directive below maps all files that end in .cgi as CGI
programs:
AddHandler cgi-script .cgi
Chapter Introduction
When you installed Apache, you configured it to start at system boot.
Though this is the usual way of starting Apache, you might encounter
situations where you need to restart or even stop Apache. At other times,
you might need to start Apache with a different set of start-up flags. Once
you've started Apache, you'll need to routinely monitor the error and
access logs for odd behavior.
This chapter will explain the various ways to start Apache, the meanings
of Apache's command- line flags, and how to examine the Apache logs.
Chapter Objectives
After completing this chapter, you'll be able to:
• use the apachectl script to control Apache.
• use the System V style script httpd to control Apache.
• list and explain Apache's command- line parameters.
• describe Apache's logs and how to read them.
apachectl
Apache (post version 1.3) comes with a command to control the Apache
server. In the source distribution, this file is found in
src/support/apachectl, but binary distributions will install the file
in /usr/sbin/apachectl.
Configuring apachectl
At the top of the apachectl script is a configuration section, shown
below:
# the path to your PID file
PIDFILE=/usr/local/apache/logs/httpd.pid
#
# the path to your httpd binary, including
# options if necessary
HTTPD='/usr/local/apache/src/httpd'
#
# a command that outputs a formatted text
# version of the HTML at the url given on the
# command-line. Designed for lynx, however
# other programs may work.
LYNX="lynx -dump"
#
# the URL to your server's mod_status status
# page. If you do not have one, then status
# and fullstatus will not work.
STATUSURL="http://localhost/server-status"
If you built Apache from the source code and modified the default Apache
installation directories, you'll need to update this configuration section to
reflect your changes.
The first information, held within the brackets ([]), is the date and time of
the error, as reported by the system clock. The second information, also
within brackets, shows the severity of the error. The remainder is error
specific, but usually provides clues as to the error's nature.
Example error
Often times, administrators will see the following error:
[Fri Jun 16 09:54:37 2000] [error] [client
192.168.0.1] File does not exist:
/home/httpd/htdocs/favicon.ico
Formats
The access log, and in fact all logs within Apache, are governed by a
format. The format specifies what each entry in the log file should look
like. For example, the format might state if the log entryshould contain
the timestamp, and if so, where should it be placed relative to the other
information.
When you configure Apache, you can specify a different log format with
the LogFormat directive. The LogFormat directive has the following
syntax:
LogFormat format handle
Chapter Overview
Virtual hosting refers to maintaining more than one server on a machine,
differentiated by host name or IP address. For example, companies
sharing a web server want to have their own domains and allow web
server accessibility by www.company1.com and www.company2.com,
without requiring any extra path information from the user. Apache
supports several types of virtual hosting: IP address-based, name-based,
and dynamically- named.
Chapter Objectives
After completing this chapter, you will be able to:
• implement IP address-based virtual hosts.
• implement name-based virtual hosts.
• implement dynamically- named virtual hosts.
• describe limitations with virtual hosts and appropriate remedies.
TIP:
Because you're specifying configuration parameters for two separate
Apache servers, all the directives are available. You will need to tailor
these appropriately for each of the individual sites.
For example, suppose your Linux system hosts two web sites:
• www.company1.com, with an IP address of 192.168.0.1
• www.company2.com, with an IP address of 192.168.0.2
Then, the configuration file for www.company1.com would look like:
# httpd configuration for www.company1.com
User www
Group company1
Listen 192.168.0.1:80
ServerRoot /etc/httpd/company1/
DocumentRoot /home/httpd/htdocs/company1/
At system boot, start an http server using the configuratio n file for
company1, and an http server using the configuration file for company2
and you've achieved IP address virtual hosting.
TIP:
You can place all of Apache's directives within a VirtualHost
block except for: ServerType, StartServers,
MaxSpareServers, MinSpareServers,
MaxRequestsPerChild, BindAddress, Listen, PidFile,
TypesConfig, ServerRoot, and NameVirtualHost.
For example, suppose your Linux system hosts two web sites:
• www.company1.com, with an IP address of 192.168.0.1
• www.company2.com, with an IP address of 192.168.0.2
You can set these up with a single Apache server with IP address-based
virtual hosts with:
<VirtualHost 192.168.0.1>
ServerName www.company1.com
User www
Group company1
DocumentRoot /home/httpd/htdocs/company1/
ErrorLog company1/logs/error_log
CustomLog company1/logs/access_log common
</VirtualHost>
<VirtualHost 192.168.0.2>
ServerName www.company2.com
User www
Group company2
DocumentRoot /home/httpd/htdocs/company2/
ErrorLog company2/logs/error_log
CustomLog company2/logs/access_log common
</VirtualHost>
TIP:
Though you could specify the DNS name instead of the IP address in
the VirtualHost block, doing so isn't recommended. Apache has
to perform a DNS lookup before allowing access, which slows down
response time.
<VirtualHost 192.168.0.1>
ServerName www.company1.com
User www
Group company1
DocumentRoot /home/httpd/htdocs/company1/
ErrorLog logs/error_log.company1
CustomLog logs/access_log.company1 common
</VirtualHost>
<VirtualHost 192.168.0.1>
ServerName www.company2.com
User www
Group company2
DocumentRoot /home/httpd/htdocs/company2/
ErrorLog logs/error_log.company2
CustomLog logs/access_log.company2 common
</VirtualHost>
TIP:
Apache looks up the server to access from the HTTP headers. If this
information isn't available (such as with very old browsers), Apache
will use the first defined virtual host.
<Directory /home/httpd/htdocs/pers/>
Options FollowSymLinks
AllowOverride None
</Directory>
<VirtualHost 192.168.0.1>
ServerName www.corp.isp.com
CustomLog logs/corp/access_log vcommon
VirtualDocumentRoot /home/httpd/htdocs/corp/%0
VirtualScriptAlias /home/httpd/cgi-bin/%0
</VirtualHost>
<VirtualHost 192.168.0.2>
ServerName www.hom.isp.com
CustomLog logs/access_log.hom vcommon
VirtualDocumentRoot /home/httpd/htdocs/pers/%0
ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/
</VirtualHost>
Chapter Overview
Apache supports an extensive set of configuration
directives. We have previously only touched on the major ones. In
this chapter, you'll see that Apache can have conditional configuration,
attach handlers to particular types of files, and change how it renders
information.
Chapter Objectives
After completing this chapter, you will be able to:
• use conditional directives to alter Apache's configuration.
• test and set Apache environment variables.
• recognize and associate handlers with files.
• redirect content.
• enable and modify Apache's fancy indexing.
• configure Apache's content negotiation.
TIP:
Parameter names are case-sensitive.
TIP:
You can nest IfDefine directives for simple multi-parameter tests.
TIP:
SetEnvIf is case-sensitive; SetEnvIfNoCase is not.
Browser matching
A special case of the SetEnvIf directive is the BrowserMatch (and
BrowserMatchNoCase) directive. This directive only checks the
browser's type, so you can use this as a quick way to set environment
variables describing the client's browser:
# unset the javascript variable if the client's
# Internet Explorer (IE uses jscript)
BrowserMatch MSIE !javascript
Handlers
Many handlers are compiled into Apache or are available in a module.
The table below lists the handlers available either by Apache directly or
through a module:
Handler Description Module
default-handler Simply send the file as- is to core
the browser, adding HTTP
headers
send-as-is Send file as-is mod_asis
By file extension
You can add a handler based on a file's extension with the AddHandler
directive:
AddHandler cgi-script .cgi
TIP:
You can specify more than one extension and you do not need the
leading dot with the AddHandler directive. For example,
AddHandler cgi-script .cgi pl causes Apache to treat all
files ending in ".cgi" and ".pl" as CGI scripts.
By file location
You can instruct Apache to use the same handler for all files in a certain
location with the SetHandler directive:
# all files in the users' cgi-bin directories
# are treated as CGI files
<Directory /home/*/public_html/cgi-bin/>
SetHandler cgi-script
</Directory>
Example
Suppose you want to check users' CGI scripts before they're run. You can
set up a handler that is called whenever a user's CGI script runs; this
handler will check the user's code for any security breaches:
Action check-user-cgi /cgi-bin/sanitize.pl
<Directory /home/*/public_html/cgi-bin/>
SetHandler check-user-cgi
</Directory>
Simple aliases
The Alias directive allows you to associate an alias with a real file's
name, as shown below:
# alias real location
Alias /icons/ /home/httpd/icons
Whenever a browser requests the alias, Apache actually goes to the real
location, which must be in the local filesystem, and retrieves the content
from there.
When using aliases, the alias must match exactly. This means that a
trailing slash on an alias, as above with /icons/, must be present in the
request for the alias to work. In the example above, a browser requesting
/icons/ would go to the aliased location, but one requesting /icons
would not.
TIP:
You can't use the Directory or Location block directives on an
alias. You must use them on the real location.
Pattern aliases
Rather than specifying an exact alias, you can specify an alias by regular
expression. The AliasMatch directive, shown below, is more powerful
than the Alias directive alone:
# redirect any requests to the icons/ directory
# to the particular corporation's icon directory
AliasMatch (.*)/icons/ /home/httpd/corp/$1/icons
Redirection with the Redirect directive has several benefits over the
Alias directive:
1. The redirected location doesn't have to be in the local filesystem; it
can be anywhere on the web.
2. You can send a status indicator along with the redirection.
Browsers conforming to HTTP 1.1 will use this status code as an
indication of the redirection's status.
Sending a status
You can send a status along with the redirection by supplying an
additional parameter:
Redirect permanent /help.html http://www.help.org/
Redirect gone /intranet.html
TIP:
Use the RedirectMatch directive to exert more control over the
resource redirection.
Default icon
The DefaultIcon directive specifies the image to display when no
previous directive has associated an icon with a particular file:
DefaultIcon /icons/unknown.gif
Index header
The HeaderName directive specifies the file that Apache should display
prior to the fancy indexes:
HeaderName header.html
Directory description
The ReadmeName directive specifies the file that Apache should display
after the fancy indexes:
ReadmeName README
Excluding files
The IndexIgnore directive specifies files that shouldn’t be included in an
auto-generated index:
IndexIgnore .??* *~ *# HEADER* README* RCS
Encoding
The AddEncoding directive maps a file extension to a MIME (Multi-
purpose Internet Mail Extensions) encoding type:
# MIME encoding extension
AddEncoding x-zip .zip
Language
The AddLanguage directive associates a file extension with a
language code:
# code extension
AddLanguage en .en
AddLanguage de .de
AddLanguage it .it
AddLanguage ja .ja
Character sets
With different languages also comes the possibility of different character
sets. For example, a Japanese encoding will not use the Western character
set (ISO-8660-0), because it doesn't contain the Japanese alphabet.
You can map a document extension to a character set using the
AddCharset directive:
# support three Japanese character sets
AddCharset EUC-JP .euc
AddCharset ISO-2022-JP .jis
AddCharset SHIFT_JIS .sjis
Then, when a browser requests a particular encoding, Apache will look for
a file ending in the mapped extension.
For example, suppose a browser wanted index.html in the Japanese
language (language code ja) and in the ISO standard Japanese character
set (character set code ISO-2022-JP). Apache, configured as shown
above, would look for the following files in order:
1. index.html.ja.jis
2. index.html.jis.ja
3. index.html.ja
4. index.html.jis
5. index.html
TIP:
The Apache documentation for the mod_negotiation module
explains the content negotiation algorithm in more detail.
Chapter Overview
Administrators typically want to increase their servers' performance and
maximize their servers' security. Apache tailors to both these needs, as
well as to more fundamental needs, including correct operation.
Chapter Objectives
After completing this chapter, you will be able to:
• explain Apache's security and performance goals.
• describe hardware performance issues.
• tune Apache's performance.
• secure an Apache run site.
Memory
The single most important characteristic for a web server machine is
available RAM. Whenever Apache server processes swap (transfers
recently unused data from disk to memory or vice versa), users notice a
delay that exceeds their usual tolerance. 2
To help reduce the potential for swapping, set the MaxClients directive
so that your system doesn't need to swap server processes. This will
dramatically improve your system's performance.
TIP:
For Linux, you can inspect your swap space usage with the
swapon -s command.
2 See http://httpd.apache.org/docs/misc/perf-tuning.html.
TIP:
Apache is not yet seasoned for the Windows NT environment. The
programming model NT employs differs from the one Apache uses.
Hence, the performance of Apache under NT is significantly different
than that for a Unix- like system.
Run-time tuning
Tuning Apache at run-time is a matter of configuring several key
directives appropriately.
AllowOverride
When you allow directories to have overrides (those provided by the
.htaccess file), you impose a significant search burden on Apache.
Consider the following configuration:
DocumentRoot /home/httpd/htdocs
<Directory />
AllowOverride All
</Directory>
A request to the site's homepage will cause Apache to check for and
process each of the following files:
• /.htaccess
• /home/.htaccess
• /home/httpd/.htaccess/
• /home/httpd/htdocs/.htaccess
These files' contents aren't cached, so every request will cause this
processing.
Keepalives
You should keep connections open with the KeepAliveTimeout
directive. This allows the client-server connection to remain open and
reduce the penalties of accepting new connections. However, keeping
Apache processes around too long means they'll sit in a busy (idle) loop,
which just wastes resources.
The default KeepAliveTimeout of 15 seconds attempts to minimize
this effect. Ho wever, the tradeoff between network bandwidth and server
resources remains regardless of the value. You should never raise this
value above 60 seconds, as the benefits are lost.
Negotiation
Don't disable content negotiation. The benefits far outweigh the
performance hit. The one scenario where it makes sense to restrict content
negotiation comes with directory indexing. Rather than using a directory
index wildcard, explicitly name the allowed index files:
# using index as a wildcard, like below which
# matches index*, is a performance no-no
# DirectoryIndex index
Server death
The MaxRequestsPerChild directive restricts the number of request
a child server will handle. Usually this value is 0, which means that there
is no limit to the number of requests handled per child. Your
configuration should not set this to a low number, like 50, as that's far too
few, and it typically causes swapping.
For operating systems where this parameter is important, like SunOS or an
old version of Solaris, limit this to approximately 10,000. This allows the
child to process enough requests to prevent swapping and also limits the
absorption of system memory through memory leaks.
Restricting access
Apache allows you to specify hosts that can and hosts that cannot access
your web sites. The Allow and Deny directives specify the hosts that
can and cannot, respectively, access your sites. These directives typically
appear within a Directory block directive.
<Directory />
Allow from company.com friend.org
Deny from foe.com
Allow from ally.foe.com
</Directory>
When Apache checks a host for access, it scans the configuration file from
top to bottom and uses the first match encountered. In the example above,
Apache would deny ally.foe.com even though it's declared Allow.
To enforce a particular order, use the Order directive. Revisiting the
previous example:
<Directory />
Order allow, deny
Allow from company.com friend.org
Deny from foe.com
Allow from ally.foe.com
</Directory>
Because the order is declared Allow first, all the Allow directives would
be checked before the Deny directives.
Pedantic access
You can allow and deny from a special class of hosts: All. When you
use All in either the Allow or Deny directive, Apache will match all
hosts. For example:
# access only from company.com & friend.org
<Directory />
Order deny, allow
Allow from company.com friend.org
Deny from All
</Directory>
Example
Considering performance and maximum security, a typical site will have
the following configuration for user directories (notoriously the most
insecure area):
# set the security on users' directories
<Directory /home/*/public_html>
Options FollowSymLinks IncludesNOEXEC \
-FollowSymLinksIfOwnerMatch
AllowOverride AuthConfig Limit
</Directory>
TIP:
You can remove a previously set option by placing a hyphen (-) in
front of the option parameter.
suEXEC
suEXEC allows the Web server to run CGI scripts as a different user than
the Apache server. suEXEC is not configured by default, so you will have
to go back to the Apache source to enable this.
When you configure Apache, pass the Apache configuration script
configure the --enable-suexec flag and the
--suexec-safepath flag. These will apply safe settings for suEXEC
for most installations.
TIP:
After configuring but before compiling, check the suEXEC setup by
typing configure --layout and inspecting the values.
When Apache starts up, a properly configured suEXEC system will log
the following message:
[notice] suEXEC mechanism enabled
Chapter Overview
The most complicated Apache module, mod_rewrite, is also the most
powerful. With it, you can translate any URL into another, incorporating
a wide array of conditions, variables, and patterns. Administrators
wishing to take their site to the "next level" will need to make heavy use
of this module.
Chapter Objectives
After completing this chapter, you will be able to:
• explain the syntax of common Rewriting rules.
• list useful scenarios where Rewriting adds benefit.
• implement Rewriting rules for common Rewriting scenarios.
Rewriting fundamentals
The mod_rewrite module can operate in two contexts:
• per-server context, which applies to all the configuration,
overrides, aliases, and so on for an entire Apache web server.
Typically, per-server context applies to everything within the
httpd.conf file.
• per-directory context, which applies to the per-server context and
all the configuration, overrides, aliases, and so on for a directory.
Typically, per-directory context augments the per-server context
via .htaccess files.
3 There is no obvious distinction between a URL and a directory; both are ways of
expressing data's location, though URLs have a more general and robust syntax.
Trailing slashes
When you request a URL that's a directory, you mustn't forget the trailing
slash. For example, suppose you try to access:
http://www.mycompany.com/~user/subdir.
Apache tries to locate the file "subdir", which probably doesn't exist.
What you wanted the URL to say was:
http://www.mycompany.com/~user/subdir/.
To accomplish this, use the following configuration:
RewriteEngine on
Generally speaking, Apache actually tries to fix this trailing slash problem
on its own. Sometimes it fails, though (for example, when you've already
done a lot of complicated rewritings). Hence, the method above is hard-
coded for those particular cases.
Users could, of course, put a general configuration (shown below) in their
top-level .htaccess files:
RewriteEngine on
RewriteBase /~user/
APPENDICES A-1
LAB 1: INTRODUCTION A-2
LAB 2: APACHE INSTALLATION A-3
LAB 3: APACHE CONFIGURATION A-5
LAB 4: EFFECTIVELY WORKING WITH APACHE A-7
LAB 5: VIRTUAL HOSTS A-8
LAB 6: ADVANCED CONFIGURATION A-10
LAB 7: PERFORMANCE AND SECURITY A-11
LAB 8: URL REWRITING AND CUMULATIVE LAB A-13
REFERENCES A-14
3. What are two ways Apache can provide "safety nets" for runaway
or buggy Apache servers?
2. Why does Apache not start spare servers immediately, but instead
starts some, waits a second, starts more, etc?