March 3, 2018

An Walkthrough of the main Apache Configuration file

Nearly all of Apache’s config files are stored under three high-level directories:


You can find all my latest posts on medium.
[root@webserver httpd]# tree /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.modules.d
├── httpd.conf
└── magic
├── autoindex.conf
├── fcgid.conf
├── manual.conf           # This came from installing the httpd-manual rpm
├── ssl.conf              # This came from installing the mod_ssl rpm
├── userdir.conf
└── welcome.conf
├── 00-base.conf
├── 00-dav.conf
├── 00-lua.conf
├── 00-mpm.conf
├── 00-proxy.conf
├── 00-ssl.conf
├── 00-systemd.conf
├── 01-cgi.conf
└── 10-fcgid.conf

Other rpms can drop files into the *.d directories.

There are quite a few config files that comes with apache, but the main ones are /etc/httpd/conf/httpd.conf and /etc/httpd/conf.d/ssl.conf. Here’s what httpd.conf looks like:

[root@webserver conf]# cat /etc/httpd/conf/httpd.conf
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See  for detailed information.
# In particular, see
# for a discussion of each configuration directive.
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# server as '/www/log/access_log', where as '/log/access_log' will be
# interpreted as '/log/access_log'.

# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
# Do not add a slash at the end of the directory path.  If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used.  If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
ServerRoot "/etc/httpd"

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

# Dynamic Shared Object (DSO) Support
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
# Example:
# LoadModule foo_module modules/
Include conf.modules.d/*.conf

# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
User apache
Group apache

# 'Main' server configuration
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
#  definition.  These values also provide defaults for
# any  containers you may define later in the file.
# All of these directives may appear inside  containers,
# in which case these default settings will be overridden for the
# virtual host being defined.

# 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.
ServerAdmin root@localhost

# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
# If your host doesn't have a registered DNS name, enter its IP address here.

# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
#  blocks below.

    AllowOverride none
    Require all denied

# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.

# 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 "/var/www/html"

# Relax access to content within /var/www.

    AllowOverride None
    # Allow open access:
    Require all granted

# Further relax access to the default document root:

    # 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
    # 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.
    Require all granted

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.

    DirectoryIndex index.html

# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.

    Require all denied

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a 
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a 
# container, that host's errors will be logged there and not here.
ErrorLog "logs/error_log"

# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a 
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per- access logfiles, transactions will be
    # logged therein and *not* in this file.
    #CustomLog "logs/access_log" common

    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    CustomLog "logs/access_log" combined

    # Redirect: Allows you to tell clients about documents that used to
    # exist in your server's namespace, but do not anymore. The client
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo

    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a  section to allow access to
    # the filesystem path.

    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.

    AllowOverride None
    Options None
    Require all granted

    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    TypesConfig /etc/mime.types

    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #AddType application/x-gzip .tgz
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #AddHandler cgi-script .cgi

    # For type maps (negotiated resources):
    #AddHandler type-map var

    # Filters allow you to process content before it is sent to the client.
    # To parse .shtml files for server-side includes (SSI):
    # (You will also need to add "Includes" to the "Options" directive.)
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

# Specify a default charset for all content served; this enables
# interpretation of all content as UTF-8 by default.  To use the
# default browser choice (ISO-8859-1), or to allow the META tags
# in HTML content to override this choice, comment out this
# directive:
AddDefaultCharset UTF-8

    # The mod_mime_magic module allows the server to use various hints from the
    # contents of the file itself to determine its type.  The MIMEMagicFile
    # directive tells the module where the hint definitions are located.
    MIMEMagicFile conf/magic

# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/"
#ErrorDocument 402

# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall may be used to deliver
# files.  This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#EnableMMAP off
EnableSendfile on

# Supplemental configuration
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

All these settings are defined in the Apache Directives Documentation.

Notice that in Apache, settings are referred to as Directives.

You should read this document in conjunction with the Directive Quick Reference Guide, and Dictionary dictionary.

You can also access them locally like this (if you have installed httpd-manual rpm):

$ elinks http://localhost/manual/mod/directives.html
$ elinks http://localhost/manual/mod/directive-dict.html
$ elinks http://localhost/manual/mod/quickreference.html

Let’s take a look at some of these setting in turn:

The ‘ServerRoot’ setting

The ServerRoot sets the location where all of Apache’s configurations and logs are stored. Some of the files, e.g. logs are not actually sotred in this directory, instead there are symbolic links that are pointing to the actual locations, e.g.:

[root@webserver httpd]# ll /etc/httpd
total 0
drwxr-xr-x. 2 root root  37 Mar  4 15:56 conf
drwxr-xr-x. 2 root root 135 Mar  4 15:56 conf.d
drwxr-xr-x. 2 root root 186 Mar  3 22:01 conf.modules.d
lrwxrwxrwx. 1 root root  19 Mar  3 22:01 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root  29 Mar  3 22:01 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 Mar  3 22:01 run -> /run/httpd

The ‘Listen’ Directive

What Listen setting sets the port that the httpd deamon should be listening on. Here are some things to keep in mind when changing this setting to a non standard port:

  • Use an available port – Don’t change this to a port that’s already in use by another daemon
  • Open up necessory ports in firewalld
  • Update SELinux settings to support the non standard port
  • Need to specify the port in web browsers – Firefox and Chrome will automatically assume port 80/443 when accessing urls. So need to override this by explicitly specify port number in the url, e.g.

If you web server has multiple network interfaces, e.g. enp0s8, enp0s9,..etc, and you want to restrict web traffic from one interface, then you can do this by setting ip address in the listen directive, e.g.:


Note: you can find another default listen directive for https, specified in:

$ cat /etc/httpd/conf.d/ssl.conf | grep Listen
Listen 443 https

The ‘Include’ setting

The Include loads in configurations from other locations. At the moment this setting is set to a relative path, conf.modules.d/*.conf. As a result Apache will assume that the corresponding absolute path is {ServerRoot}/conf.modules.d/*.conf, i.e. /etc/httpd/conf.modules.d/*.conf.

If the Include Directive wildcard expression fails to match, then Apache will error out. If you want to suppress that error then you should IncludeOptional directive instead, covered further on.

Speaking of /etc/httpd/conf.modules.d/*.conf, this contains config files which tells Apache which modules should be running when the the httpd daemon is running. You can confirm the correct modules are loaded by running:

$ apachectl -M

Also you can view a full list of all available modules here:

[root@webserver modules]# ll /usr/lib64/httpd/modules | head
total 2788
-rwxr-xr-x. 1 root root  11208 Oct 19 20:40
-rwxr-xr-x. 1 root root  11168 Oct 19 20:40
-rwxr-xr-x. 1 root root  15360 Oct 19 20:40
-rwxr-xr-x. 1 root root  11136 Oct 19 20:40
-rwxr-xr-x. 1 root root  11088 Oct 19 20:40
-rwxr-xr-x. 1 root root  15328 Oct 19 20:40
-rwxr-xr-x. 1 root root  36080 Oct 19 20:40

If there are any modules that you want to load in then you need to reference in the /etc/httpd/conf.modules.d directory.

The ‘User’ and ‘Group’ settings

When you start the httpd deamon, it ends up starting a single parent process followed by multiple child processes. For example:

[root@webserver httpd]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2018-03-04 16:43:50 UTC; 4min 52s ago
     Docs: man:httpd(8)
  Process: 5547 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 5552 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─5552 /usr/sbin/httpd -DFOREGROUND
           ├─5553 /usr/sbin/httpd -DFOREGROUND
           ├─5554 /usr/sbin/httpd -DFOREGROUND
           ├─5555 /usr/sbin/httpd -DFOREGROUND
           ├─5556 /usr/sbin/httpd -DFOREGROUND
           ├─5557 /usr/sbin/httpd -DFOREGROUND
           └─5558 /usr/sbin/httpd -DFOREGROUND

Mar 04 16:43:50 webserver.local systemd[1]: Starting The Apache HTTP Server...
Mar 04 16:43:50 webserver.local systemd[1]: Started The Apache HTTP Server.

Here it shows that the parent process’s pid is ‘5552’. To see what the child processes are we do:

[root@webserver httpd]# pstree -p -a 5552
httpd,5552 -DFOREGROUND
  ├─httpd,5553 -DFOREGROUND
  ├─httpd,5554 -DFOREGROUND
  ├─httpd,5555 -DFOREGROUND
  ├─httpd,5556 -DFOREGROUND
  ├─httpd,5557 -DFOREGROUND
  └─httpd,5558 -DFOREGROUND

here we can see the each child process’s (p)ids command-line (a)rguements.

This setting specifies which username/group that the child processs will be run under. The parent process itself has to be owned by the user that started the httpd daemon, i.e. the root user.

[root@webserver httpd]# ps -ef f | grep httpd | grep -v grep
root      5552     1  0 16:43 ?        Ss     0:00 /usr/sbin/httpd -DFOREGROUND
vagrant   5553  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND
vagrant   5554  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND
vagrant   5555  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND
vagrant   5556  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND
vagrant   5557  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND
vagrant   5558  5552  0 16:43 ?        S      0:00  \_ /usr/sbin/httpd -DFOREGROUND

The root user is the only user that has the authority to start a deamon that listens on a port number between 1-1000.

The ‘Directory’ block Directive

Here’s what the Directory directive block setting looks like:

    AllowOverride none
    Require all denied

Here the Directory tags specifies the scope at which the containing settings should be applied to. In this the scope is the very top level ‘/’ and recursively down all child folders. This is a really broad setting, which in effect covers the whole CentOS machine. You can declare However further down the same file, we have:

    AllowOverride None
    # Allow open access:
    Require all granted

This time the scope is more targeted, /var/www. Basically what’s happening here is that the settings that resides in a more targeted blocks overrides (where there are conflicts) the broader directory block’s settings. Let’s look at the specs inside the directory block:

  • AllowOverride – This controls what features of the .htaccess file to allow (if any).
  • Require – Controls access to resources

Here’s another example:

    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

Here we have the Options directive, which let’s you enable a number of features for the given directory. In this case we have:

  • Indexes – If a user requests for the index.html file, and that doesn’t exist then Apache will instead display a plain table (that looks a bit like this) listing all the available files to choose from.
  • FollowSymLinks – This basically let’s you place symlinks in the given directory to point to files other location, such as in the /etc folder. Note, you don’t need to do any additional SELinux work to get this working.

The ‘DocumentRoot’ setting

This is where the actual web content will reside, e.g. *.html, *.css, *.php files …..etc. By default this is set to /var/www/html. One thing to bear in mind is that if you want to change this, then you need to configure SELinux to support whatever the new location is.

The ‘IfModule’ block Directive

This IfModule block comes into play if the specified module is present. Here’s an example:

    DirectoryIndex index.html

This checks to see if the dir_module is present, you can check this manually using the apachectl command:

[root@webserver httpd]# apachectl -M | grep dir
 dir_module (shared)
 userdir_module (shared)

Here we can see dir_module is enabled. This means that the DirectoryIndex setting will come into effect.

This basically means that if the url doesn’t specify a particular file, then Apache will default to serving up index.html as the default, i.e. if you request for then Apache will assume and serve

The ‘Files’ block Directive

The Files directive is similar to the Directive but controls settings at the file level, here’s an example:

    Require all denied

In this case, this will prevent website visitors from accessing files such as .htaccess, irrespective of which directory it’s in.

The ErrorLog directive

The ErrorLog directive let’s you specify set where you want your error logs to be written to. In this case it is:

ErrorLog "logs/error_log"

Since this is specified as a relative path, Apache will assume it is relative to the ServerRoot location, i.e. Apache will assume the above corresponds to /etc/httpd/logs/error_log.

However since /etc/httpd/logs is a symbolic link:

[root@webserver httpd]# ll /etc/httpd/logs
lrwxrwxrwx. 1 root root 19 Mar  3 22:01 /etc/httpd/logs -> ../../var/log/httpd

It means that the actual location where the logs are stored is:

[root@webserver httpd]# file /var/log/httpd/error_log
/var/log/httpd/error_log: ASCII text

The LogLevel directive

The LogLevel directive sets the verbosity of the logging.

The LogFormat directive

The LogFormat directive let’s you specify the format of your log entries. Optionally you can then give this format a nickname, which you can then reference, for example:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

Here we assigned the this format the nickname ‘combined’. We can then use this nickname to reference this format in other config settings, for example in the CustomLog directive.

The CustomLog directive

The CustomLog directive let’s you specify a custom location for sending access related log entries to. Here’s an example:

CustomLog "logs/access_log" combined

Again this is a relative, which Apache resolves (using the ServerRoot setting) to


Which in turn ultimately points to /var/log/httpd/access_log

The ErrorLog directive

The ErrorLog directive is similar to the CustomLog directive, but this time for Error logs.

The IncludeOptional directive

The IncludeOptional directive essentially pulls in other configs. These settings can be (and is often) used to override the configurations in this file.

IncludeOptional conf.d/*.conf

Again this is a relative path, which Apache will assume it is relative to the ServerRoot location, i.e. Apache will assume the above corresponds to all /etc/httpd/conf.d/*.conf files.

Overrides, overrides, and more Overrides

In Apache, the configurations can be stored in multiple places. In fact in Apache duplicate directives are specified with the intention of more targetted directives overriding the duplciates. The penultimate column in the Apache Quick Reference guide shows where certain directives can be declared. Here’s the order of precedence:

  1. .htaccess – Directive setting here overrides everything (as long as the .htaccess feature has been enabled)
  2. Directory – settings specified in the Directory block will take precedence the more targetted it is.
    Also those that are specified within vhost config files takes precedence over those at the top level server config file.
  3. vhosts – directive specified in vhosts takes precedance over server configs
  4. server config – takes lowest priority over duplicate directives specified elsewhere