# Cheat Sheet #day3 - Apache Web Server

### Apache Web Server Cheat Sheet

#### Basic Commands

- **Start Apache:**

  ```sh
  sudo systemctl start apache2      # Debian/Ubuntu
  sudo systemctl start httpd        # CentOS/RHEL
  ```

- **Stop Apache:**

  ```sh
  sudo systemctl stop apache2       # Debian/Ubuntu
  sudo systemctl stop httpd         # CentOS/RHEL
  ```

- **Restart Apache:**

  ```sh
  sudo systemctl restart apache2    # Debian/Ubuntu
  sudo systemctl restart httpd      # CentOS/RHEL
  ```

- **Reload Configuration:**

  ```sh
  sudo systemctl reload apache2     # Debian/Ubuntu
  sudo systemctl reload httpd       # CentOS/RHEL
  ```

- **Enable Apache on Boot:**

  ```sh
  sudo systemctl enable apache2     # Debian/Ubuntu
  sudo systemctl enable httpd       # CentOS/RHEL
  ```

- **Disable Apache on Boot:**

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

#### Configuration Files

- **Main Configuration File:**

  ```sh
  /etc/apache2/apache2.conf         # Debian/Ubuntu
  /etc/httpd/conf/httpd.conf        # CentOS/RHEL
  ```

- **Virtual Hosts Configuration:**

  ```sh
  /etc/apache2/sites-available/     # Debian/Ubuntu
  /etc/httpd/conf.d/                # CentOS/RHEL
  ```

- **Enabled Sites:**

  ```sh
  /etc/apache2/sites-enabled/       # Debian/Ubuntu
  ```

- **Modules Configuration:**
  
```sh
  /etc/apache2/mods-available/      # Debian/Ubuntu
  /etc/httpd/conf.modules.d/        # CentOS/RHEL
```

#### Enabling and Disabling Sites

- **Enable a Site:**
  ```sh
  sudo a2ensite example.conf        # Debian/Ubuntu
  ```

- **Disable a Site:**

  ```sh
  sudo a2dissite example.conf       # Debian/Ubuntu
  ```

- **Activate Changes:**

  ```sh
  sudo systemctl reload apache2     # Debian/Ubuntu
  ```

#### Enabling and Disabling Modules

- **Enable a Module:**

  ```sh
  sudo a2enmod module_name          # Debian/Ubuntu
  ```

- **Disable a Module:**

  ```sh
  sudo a2dismod module_name         # Debian/Ubuntu
  ```

- **Activate Changes:**

  ```sh
  sudo systemctl restart apache2    # Debian/Ubuntu
  ```

#### Common Directives

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

  ```sh
  DocumentRoot /var/www/html
  ```

- **ServerName**: Sets the hostname and port that the server uses to identify itself.

  ```sh
  ServerName www.example.com
  ```

- **Directory**: Controls the settings for a specific directory.

  ```sh
  <Directory /var/www/html>
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted
  </Directory>
  ```

- **Listen**: Specifies the port that Apache will listen on.

  ```sh
  Listen 80
  ```

- **ErrorLog**: Sets the name of the file to which the server will log any errors it encounters.

  ```sh
  ErrorLog ${APACHE_LOG_DIR}/error.log
  ```

- **CustomLog**: Sets the filename and format of log files.

  ```sh
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  ```

#### SSL Configuration

- **Enable SSL Module:**

  ```sh
  sudo a2enmod ssl                   # Debian/Ubuntu
  ```

- **Default SSL Configuration File:**

  ```sh
  /etc/apache2/sites-available/default-ssl.conf  # Debian/Ubuntu
  /etc/httpd/conf.d/ssl.conf                      # CentOS/RHEL
  ```

- **Common SSL Directives:**

  ```sh
  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:**

  ```sh
  <Directory /var/www/html>
      Require all granted
  </Directory>
  ```

- **Allow/Deny Directives:**

  ```sh
  <Directory /var/www/html>
      Order allow,deny
      Allow from all
  </Directory>
  ```

- **IP-based Access Control:**

  ```sh
  <Directory /var/www/html>
      Require ip 192.168.1.0/24
      Require ip 10.0.0.0/16
  </Directory>
  ```

### Useful Commands and Tools

- **Test Configuration:**

  ```sh
  sudo apachectl configtest           # Debian/Ubuntu
  sudo httpd -t                       # CentOS/RHEL
  ```

- **Check Apache Status:**

  ```sh
  sudo systemctl status apache2       # Debian/Ubuntu
  sudo systemctl status httpd         # CentOS/RHEL
  ```

- **Log Files Location:**

  ```sh
  /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.
