Cheat Sheet #day3 - Apache Web Server

Cheat Sheet #day3 - Apache Web Server

Apache Web Server Cheat Sheet

Basic Commands

  • Start Apache:

    sudo systemctl start apache2      # Debian/Ubuntu
    sudo systemctl start httpd        # CentOS/RHEL
  • Stop Apache:

    sudo systemctl stop apache2       # Debian/Ubuntu
    sudo systemctl stop httpd         # CentOS/RHEL
  • Restart Apache:

    sudo systemctl restart apache2    # Debian/Ubuntu
    sudo systemctl restart httpd      # CentOS/RHEL
  • Reload Configuration:

    sudo systemctl reload apache2     # Debian/Ubuntu
    sudo systemctl reload httpd       # CentOS/RHEL
  • Enable Apache on Boot:

    sudo systemctl enable apache2     # Debian/Ubuntu
    sudo systemctl enable httpd       # CentOS/RHEL
  • Disable Apache on Boot:

    sudo systemctl disable apache2    # Debian/Ubuntu
    sudo systemctl disable httpd      # CentOS/RHEL

Configuration Files

  • Main Configuration File:

    /etc/apache2/apache2.conf         # Debian/Ubuntu
    /etc/httpd/conf/httpd.conf        # CentOS/RHEL
  • Virtual Hosts Configuration:

    /etc/apache2/sites-available/     # Debian/Ubuntu
    /etc/httpd/conf.d/                # CentOS/RHEL
  • Enabled Sites:

    /etc/apache2/sites-enabled/       # Debian/Ubuntu
  • Modules Configuration:

  /etc/apache2/mods-available/      # Debian/Ubuntu
  /etc/httpd/conf.modules.d/        # CentOS/RHEL

Enabling and Disabling Sites

  • Enable a Site:

    sudo a2ensite example.conf        # Debian/Ubuntu
  • Disable a Site:

    sudo a2dissite example.conf       # Debian/Ubuntu
  • Activate Changes:

    sudo systemctl reload apache2     # Debian/Ubuntu

Enabling and Disabling Modules

  • Enable a Module:

    sudo a2enmod module_name          # Debian/Ubuntu
  • Disable a Module:

    sudo a2dismod module_name         # Debian/Ubuntu
  • Activate Changes:

    sudo systemctl restart apache2    # Debian/Ubuntu

Common Directives

  • DocumentRoot: Specifies the directory out of which Apache will serve files.

    DocumentRoot /var/www/html
  • ServerName: Sets the hostname and port that the server uses to identify itself.

  • Directory: Controls the settings for a specific directory.

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
  • Listen: Specifies the port that Apache will listen on.

    Listen 80
  • ErrorLog: Sets the name of the file to which the server will log any errors it encounters.

    ErrorLog ${APACHE_LOG_DIR}/error.log
  • CustomLog: Sets the filename and format of log files.

    CustomLog ${APACHE_LOG_DIR}/access.log combined

SSL Configuration

  • Enable SSL Module:

    sudo a2enmod ssl                   # Debian/Ubuntu
  • Default SSL Configuration File:

    /etc/apache2/sites-available/default-ssl.conf  # Debian/Ubuntu
    /etc/httpd/conf.d/ssl.conf                      # CentOS/RHEL
  • Common SSL Directives:

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/your_domain.crt
    SSLCertificateKeyFile /etc/ssl/private/your_domain.key
    SSLCertificateChainFile /etc/ssl/certs/chain.crt

Access Control

  • Require Directive:

    <Directory /var/www/html>
        Require all granted
  • Allow/Deny Directives:

    <Directory /var/www/html>
        Order allow,deny
        Allow from all
  • IP-based Access Control:

    <Directory /var/www/html>
        Require ip
        Require ip

Useful Commands and Tools

  • Test Configuration:

    sudo apachectl configtest           # Debian/Ubuntu
    sudo httpd -t                       # CentOS/RHEL
  • Check Apache Status:

    sudo systemctl status apache2       # Debian/Ubuntu
    sudo systemctl status httpd         # CentOS/RHEL
  • Log Files Location:

    /var/log/apache2/                   # Debian/Ubuntu
    /var/log/httpd/                     # CentOS/RHEL

This cheat sheet provides a quick reference to the most commonly used Apache web server commands and configurations, helping you efficiently manage your server.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!