Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#apache-cheatsheet

Apache HTTP Server Cheatsheet

Apache HTTP Server (httpd or apache2) is one of the most widely used web servers in the world.

Installation

# Debian / Ubuntu
sudo apt install apache2
 
# RHEL / CentOS
sudo dnf install httpd

Service Management

# Debian / Ubuntu (service is named 'apache2')
sudo systemctl enable --now apache2
sudo systemctl status apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
 
# RHEL / CentOS (service is named 'httpd')
sudo systemctl enable --now httpd
sudo systemctl status httpd

Configuration File Locations

  • Debian / Ubuntu:

    • Main Config: /etc/apache2/apache2.conf
    • Virtual Hosts: /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/
    • Modules: /etc/apache2/mods-available/
    • Document Root: /var/www/html/
  • RHEL / CentOS:

    • Main Config: /etc/httpd/conf/httpd.conf
    • Virtual Hosts: /etc/httpd/conf.d/
    • Document Root: /var/www/html/

Enabling / Disabling Modules and Sites (Ubuntu/Debian)

Ubuntu/Debian uses helper scripts to manage configurations.

# Enable/Disable a site (Virtual Host)
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
 
# Enable/Disable an Apache module
sudo a2enmod rewrite    # Enable mod_rewrite for URL rewriting
sudo a2dismod autoindex
 
# Enable/Disable a configuration snippet
sudo a2enconf security
sudo a2disconf security
 
# Always reload after changing configuration
sudo systemctl reload apache2

Testing Configuration Syntax

Before reloading or restarting, always test your configuration for syntax errors.

# Debian / Ubuntu
sudo apache2ctl configtest
 
# RHEL / CentOS
sudo apachectl configtest

Basic Virtual Host Example (/etc/apache2/sites-available/example.com.conf)

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
 
    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>