Directory structure of Apache web server: Understanding the big picture

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Apache is one of the most popular web servers in the world, known for its flexibility, power, and widespread support. A clear understanding of its directory structure can be immensely beneficial for system administrators, developers, and anyone responsible for deploying or configuring web services. This guide will offer an understanding of the core components of Apache’s directory structure, with a look from basic arrangements to advanced layouts.

Default Apache Directory Structure

After installing Apache, on most Unix-like systems the default directory structure will look something like this:

/etc/apache2/
|-- apache2.conf
|-- conf-available/
|-- conf-enabled/
|-- mods-available/
|-- mods-enabled/
|-- sites-available/
|-- sites-enabled/
|-- envvars
|-- magic
|-- ports.conf
| -- sites-available/
    |-- default
| -- sites-enabled/
    |-- 000-default.conf

This is the standard file layout for Apache on a Debian/Ubuntu system; other distributions might have variations in the exact structure or path.

Let’s break down the key directories and files:

  • /etc/apache2/ – Main configuration directory.
  • apache2.conf – Global configuration file.
  • conf-available/ & conf-enabled/ – Stores additional configuration files.
  • mods-available/ & mods-enabled/ – Houses modules that can be enabled or disabled.
  • sites-available/ & sites-enabled/ – Contains configuration for individual websites.
  • envvars – File that sets environment variables for the server.
  • magic – File used for determining MIME type of files.
  • ports.conf – Configures listening ports.

Core Configuration Files and Directories

apache2.conf: This is Apache’s main configuration file. It sets the defaults for the web service and is often accompanied by other config snippets that handle specific tasks.

conf-available/ & -enabled: Admins or package maintainers can provide additional configs here which site maintainers can enable as necessary. Enabling them is typically done via symlinks.

mods-available/ & -enabled: Similar to ‘conf’, but for mod files. You enable and disable modules with the ‘a2enmod’/’a2dismod’ commands.

sites-available/ & -enabled: This is where you place your ‘virtual host’ files, which define different websites hosted on the server. These are enabled with the ‘a2ensite’/’a2dissite’ commands.

Example of enabling a config:

sudo a2enmod rewrite
sudo systemctl restart apache2

Adding a New Site

To host a new site, you would act on the ‘sites-available’ directory. Here’s a basic flow of how to set up a new site:

  1. Create a new config file in /etc/apache2/sites-available/, for example ‘my-site.conf’.
  2. Define your block within this config file.
  3. Place your website’s files in the correct DocumentRoot directory.
  4. Enable the site with the ‘a2ensite my-site’ command.
  5. Reload Apache with ‘sudo systemctl reload apache2’ for changes to take effect.

Example config file:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName my-site.com
ServerAlias www.my-site.com
DocumentRoot /var/www/my-site
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Remember: All sites must listen on a port number. This configuration listens on port 80, the default port for HTTP.

Advanced Configurations

In a more advanced scenario, you might be configuring SSL, creating rewrites, setting up directory protections, or managing performance settings. Here’s how you might configure SSL for your site:

<VirtualHost *:443>
ServerName my-site.com
SSLEngine on
SSLCertificateFile '/path/to/cert.crt'
SSLCertificateKeyFile '/path/to/private.key'
DocumentRoot /var/www/my-site
Include /etc/apache2/conf-available/ssl-params.conf
</VirtualHost>

To improve the performance of your Apache server you could edit the global configuration to tweak the ‘mpm_prefork’ module, like so:

<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 3000
</IfModule>

Logs

Most Apache installations will log to /var/log/apache2/ by default. Within, you will find ‘access.log’ and ‘error.log’ which will be referenced in your VirtualHost definitions for individual sites. Proper logging is crucial for monitoring and debugging purposes.

Conclusion

In this guide, we walked through the basics to more advanced areas of Apache’s directory structure. Understanding this structure forms the foundation of managing and optimizing your web server. With this knowledge, engaging with Apache configurations should become less daunting and more intuitive.