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/RHELStop Apache:
sudo systemctl stop apache2 # Debian/Ubuntu sudo systemctl stop httpd # CentOS/RHELRestart Apache:
sudo systemctl restart apache2 # Debian/Ubuntu sudo systemctl restart httpd # CentOS/RHELReload Configuration:
sudo systemctl reload apache2 # Debian/Ubuntu sudo systemctl reload httpd # CentOS/RHELEnable Apache on Boot:
sudo systemctl enable apache2 # Debian/Ubuntu sudo systemctl enable httpd # CentOS/RHELDisable 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/RHELVirtual Hosts Configuration:
/etc/apache2/sites-available/ # Debian/Ubuntu /etc/httpd/conf.d/ # CentOS/RHELEnabled Sites:
/etc/apache2/sites-enabled/ # Debian/UbuntuModules 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/UbuntuDisable a Site:
sudo a2dissite example.conf # Debian/UbuntuActivate Changes:
sudo systemctl reload apache2 # Debian/Ubuntu
Enabling and Disabling Modules
Enable a Module:
sudo a2enmod module_name # Debian/UbuntuDisable a Module:
sudo a2dismod module_name # Debian/UbuntuActivate Changes:
sudo systemctl restart apache2 # Debian/Ubuntu
Common Directives
DocumentRoot: Specifies the directory out of which Apache will serve files.
DocumentRoot /var/www/htmlServerName: Sets the hostname and port that the server uses to identify itself.
ServerName www.example.comDirectory: Controls the settings for a specific directory.
<Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>Listen: Specifies the port that Apache will listen on.
Listen 80ErrorLog: Sets the name of the file to which the server will log any errors it encounters.
ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog: Sets the filename and format of log files.
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSL Configuration
Enable SSL Module:
sudo a2enmod ssl # Debian/UbuntuDefault SSL Configuration File:
/etc/apache2/sites-available/default-ssl.conf # Debian/Ubuntu /etc/httpd/conf.d/ssl.conf # CentOS/RHELCommon 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 </Directory>Allow/Deny Directives:
<Directory /var/www/html> Order allow,deny Allow from all </Directory>IP-based Access Control:
<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:
sudo apachectl configtest # Debian/Ubuntu sudo httpd -t # CentOS/RHELCheck Apache Status:
sudo systemctl status apache2 # Debian/Ubuntu sudo systemctl status httpd # CentOS/RHELLog 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.




