The Apache HTTP web server is in many respects the defacto standard for general purpose HTTP services. Through its large number of modules, it provides flexible support for proxy servers, URL rewriting, and granular access control. Furthermore, web developers often choose Apache for its support of server-side scripting using CGI, FastCGI, and embedded interpreters. These capabilities facilitate the quick and efficient execution of dynamic code. While there are several prominent alternatives to Apache, even within the boundaries of open source, the breadth of Apache usage is unique.
The extraordinary degree of flexibility provided by Apache does not come without some cost; this mostly takes the form of a configuration structure that is sometimes confusing and often complicated. For this reason we've created this document and a number of other guides that seek to address this complexity and explore some more advanced and optional functionality of the Apache HTTP Sever.
If you're interested in just getting a running web server and installing Apache for the first time, we recommend using the appropriate "installing Apache guide" for your distribution of Linux. If you need a more full-featured LAMP stack, consider trying the appropriate LAMP guide for your distribution. This guide assumes that you have a running and up to date Linux system, have successfully installed Apache, and have logged into a shell session with root access.
The default configuration of Apache varies significantly between various distributions of Linux. Debian and Ubuntu distributions, as well as Gentoo, refer to Apache as "Apache2" and place the configuration files in the /etc/apache2/ directory. Other distributions, including Fedora, CentOS, and Arch Linux refer to Apache as "httpd" and store configuration files in /etc/httpd/. Most configuration options do not vary between operating systems, though we encourage you to become familiar with the default configuration of your Apache server. The greatest challenges in configuring Apache are in understanding the distributions' standard configurations and their quirks and differences from the Apache upstream.
To administer basic Apache functionality you can use the "init" scripts, which provide a safe and easy way to start, stop, and restart the server. The init script also allows you to reload the configuration and check on the status of the server. To access these functions, issue the appropriate command:
/etc/init.d/apache2 start /etc/init.d/apache2 stop /etc/init.d/apache2 restart /etc/init.d/apache2 reload /etc/init.d/apache2 status
If you're using a distribution that refers to Apache as httpd, then the commands are as follows:
/etc/init.d/httpd start /etc/init.d/httpd stop /etc/init.d/httpd restart /etc/init.d/httpd reload /etc/init.d/httpd status
For some distributions, the path to the script may be /etc/rc.d/init.d/ instead of /etc/init.d/.
If you're using mod_disk_cache on Debian-based distributions, the init script contains functionality to control the functionality of the htcache, with the following command:
/etc/init.d/apache2 start-htcacheclean /etc/init.d/apache2 stop-htcacheclean
There is also additional functionality provided from a command line interface. To check the syntax of your Apache configuration files without needing to restart the server and test, you can issue the following command on Debian and Ubuntu systems:
On CentOS and Fedora systems, use the following form:
Additionally, the "apache2ctl -S" or "httpd -S" commands provide a report on currently running virtual hosts, containing the port that the host is listening on, the name of the virtual host (i.e. the domain), and information about the location of the site's configuration settings including file names and line numbers.
Typically, the "master" configuration file for Apache is located in the httpd.conf file. In Debian-based distributions, this is located in the apache2.conf file, and the httpd.conf file contains user-specific configuration. In addition to the master file, a number of additional files are included by the master file. To get a list of these files, issue one of the following commands, depending on your distribution:
grep "Include" /etc/apache2/apache2.conf grep "Include" /etc/apache2/httpd.conf grep "Include" /etc/httpd/httpd.conf
Note that the order in which these files are included can affect the behavior of the web server. If later options contradict options set in earlier files, the later options override earlier options. Familiarizing yourself with the existing default configuration can be a helpful learning experience.
One of the most common use cases for the Apache web server is to use its "virtual hosting" capabilities, which allow a single instance of Apache to serve numerous websites and subdomains. Because most websites don't tend to use a significant amount of system resources, virtual hosting is often a great way to fully utilize a web server. As a result of this capability, configuration files for virtual hosts can be complex and difficult to organize. There are two major approaches to the solution for this problem.
The Debian approach of keeping a single configuration file for each virtual host can be helpful for managing a number of websites that are not interconnected, or that need to be edited by a number of distinct and non-privileged users. However, there are some situations where having too many individual virtual host files that set the same cluster of configuration options for a group of hosts can cause confusion and increase the maintenance burden.
In these cases, a single file may be the best option for keeping Apache configured properly. This is the preferred organization for virtual host configuration in some distributions, including CentOS, Fedora, and Arch Linux. While you should check the default Apache configuration for your distribution, commonly there is a conf.d/ directory where user-created configuration can be stored. If you want to combine a number of virtual host configuration files into a single file, create a vhosts.conf in the conf.d/ folder and place all of your configuration options in this file. The conf.d/ folder is located within Apache's /etc/ directory, either: /etc/apache2/conf.d/ or /etc/httpd/conf.d/ depending on your distribution.
Both configuration options are functionally equivalent, but offer easier administration in different scenarios. The configuration file organization that you choose depends on the needs of your particular deployment.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.
Last edited by Amanda Folson on Monday, August 22nd, 2011 (r2409).