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 / Fedora
sudo dnf install httpdService Management
# Debian / Ubuntu (service name: apache2)
sudo systemctl enable --now apache2 # Start and enable on boot
sudo systemctl status apache2 # Check status
sudo systemctl restart apache2 # Restart (drops connections)
sudo systemctl reload apache2 # Reload config gracefully
# RHEL / CentOS (service name: httpd)
sudo systemctl enable --now httpd
sudo systemctl status httpd
sudo systemctl restart httpd
sudo systemctl reload httpdConfiguration 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/
- Main Config:
-
RHEL / CentOS:
- Main Config:
/etc/httpd/conf/httpd.conf - Virtual Hosts:
/etc/httpd/conf.d/ - Document Root:
/var/www/html/
- Main Config:
Testing Configuration Syntax
Before reloading or restarting, always test for syntax errors.
# Debian / Ubuntu
sudo apache2ctl configtest
# RHEL / CentOS
sudo apachectl configtestEnabling / Disabling Modules and Sites (Debian/Ubuntu)
Debian/Ubuntu 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 a2enmod ssl # Enable mod_ssl for HTTPS
sudo a2dismod autoindex # Disable directory listing module
# Enable/Disable a configuration snippet
sudo a2enconf security
sudo a2disconf security
# Always reload after changing configuration
sudo systemctl reload apache2Basic Virtual Host (/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>SSL / HTTPS Virtual Host
# Enable SSL module and default SSL site (Debian/Ubuntu)
sudo a2enmod ssl
sudo a2ensite default-ssl
sudo systemctl reload apache2Custom SSL Virtual Host (/etc/apache2/sites-available/example.com-ssl.conf):
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
# Redirect HTTP to HTTPS
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>URL Rewriting (mod_rewrite)
Enable mod_rewrite first: sudo a2enmod rewrite
In Virtual Host or .htaccess
RewriteEngine On
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# Remove .html extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L].htaccess
.htaccess provides per-directory configuration overrides. Requires AllowOverride All in the Virtual Host.
# Deny access to a file
<Files "config.php">
Require all denied
</Files>
# Password protect a directory
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-userCreate the password file:
# Create a new htpasswd file with user 'admin'
sudo htpasswd -c /etc/apache2/.htpasswd admin
# Add another user to an existing file (no -c flag)
sudo htpasswd /etc/apache2/.htpasswd anotheruserLogs
# Debian / Ubuntu
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
# RHEL / CentOS
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log