Linux Server How To

How To Setup, Configure, Manage and Secure a Linux Server


Linux Server How To - Basic Apache2 Configuration


Configuring Apache2

Configuring Apache2 is performed through a series of text configuration files and is as simple as editing these files with a text editor such as Vi or Pico and restarting the web server so that the changes take effect. The configuration files can be quite extensive and on many installations are broken into smaller files that control a specific aspect of the web server such as virtual hosts and ssl for example. So you may have a main configuration file, typically called httpd.conf or possibly apache2.conf and a series of smaller ancillary configuration files located in that directory or in a subdirectory which is generally called extra. These additonal configuration files are tied in so to speak with the main configuration file with an include statement that instructs Apache2 to include the contents of that file when loading its configuration. An example of this is shown below, where we can see the virtual hosts include file commented out by a #, which we would remove if we chose to include the virtual hosts configuration file in the Apache2 configuration of our Linux server.

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

The actual location of your Apache2 configuration files on your Linux server will vary slightly from distribution to distribution and will also be dependant on whether you chose to use the Apache2 that shipped with your Linux installation and if you used a package manager like apt-get or rpm or installed from source. If you compiled your install of Apache2 from source the configuration files will be found in a conf directory in the server root, so for example if you elected to install Apache2 in /usr/local/apache2 your configuration files will be found in /usr/local/apache2/conf. If your using an install of Apache2 that was installed when you installed your Linux server or through a package management system you will likely find your Apache2 configuration files in /etc, probably in a directory called /etc/httpd or /etc/apache2.

Common Configuration Changes with Apache2

Whether you have installed Apache2 using apt-get or compiled Apache2 from source on your Linux server the web server will run as it is with no configuration changes to display a single website. There are a few options you may wish to change to customise your Apache2 install somewhat.

Server Admin Email Address

If your Apache2 web server encounters a problem it may generate an error messages and invite the visitor to report the problem to the server administrator via email. The ServerAdmin directive allows the the server administrator to specify what email address to display.

# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin you@example.com

Listen Address

By default the Apache2 web server will listen for http requests on all IP addresses configured on your Linux server. There are a few reasons you may not find this behaviour acceptable, for example the Linux server may host a ticketing or customer management system that is only to be used on the LAN IP address but it may also act as a gateway and have a WAN IP address too. The Listen directive allows you to specify what IP address your Apache server will accept a request for a web page on.

# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

Change the Document Root

The document root is the folder on your Linux servers file system that contains the contents of the web page that you would like to display. The default document root is /usr/local/apache2/htdocs in our compiled install of Apache2 on our slackware Linux server and /var/www on our Ubuntu Linux server that had Apache2 installed using apt-get. You may wish to change the document root to something more accessible or convenient, like the home directory of your web designer for example. The document root is changed in the httpd.conf or apache2.conf configuration file. In the example snippets of this file below we have commented out the original lines and replaced them with new ones which are shown in bold. The first thing to change is the DocumentRoot directive.

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
#DocumentRoot "/usr/local/apache2/htdocs"
DocumentRoot "/home/webdesigner"

Apache2 is configured in a very secure manner by default and will not display a web page from our web designers home directory without further information regarding how to treat the contents of this folder. The original configuration for the default document root is fine, we just need it to know what the new document root is.

# This should be changed to whatever you set DocumentRoot to.
#
#‹Directory “/usr/local/apache2/htdocs”›
‹Directory “/home/webdesigner”›
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information. #
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

‹/Directory›