Installation
Bash
# Ubuntu / Debian
sudo apt update && sudo apt install -y apache2
# CentOS / RHEL
sudo yum install -y httpd
# Start the service
sudo systemctl enable --now apache2 # Ubuntu
sudo systemctl enable --now httpd # CentOSDirectory Structure (Ubuntu)
CODE
/etc/apache2/
├── apache2.conf # Main configuration
├── sites-available/ # Virtual host configuration files
├── sites-enabled/ # Enabled sites (symbolic links)
├── mods-available/ # Available modules
└── mods-enabled/ # Enabled modulesBasic Virtual Host Configuration
APACHE
# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>Bash
# Enable the site
sudo a2ensite example.com.conf
sudo systemctl reload apache2
# Disable the site
sudo a2dissite 000-default.confHTTP → HTTPS Redirect
APACHE
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Header always set Strict-Transport-Security "max-age=31536000"
</VirtualHost>Module Management
Bash
# Enable modules
sudo a2enmod rewrite # URL rewriting
sudo a2enmod ssl # HTTPS
sudo a2enmod headers # Header manipulation
sudo a2enmod proxy proxy_http # Reverse proxy
# Disable a module
sudo a2dismod autoindex
# List enabled modules
apache2ctl -M.htaccess and mod_rewrite
APACHE
# /var/www/example.com/.htaccess
Options -Indexes
RewriteEngine On
# www → non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# SPA routing (React, Vue, etc.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]Reverse Proxy (Node.js App Integration)
APACHE
<VirtualHost *:443>
ServerName api.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
RequestHeader set X-Forwarded-Proto "https"
</VirtualHost>Config Validation and Reload
Bash
# Syntax check
sudo apache2ctl configtest
# or
sudo apachectl configtest
# Graceful reload (zero downtime)
sudo systemctl reload apache2Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)OpenSSL documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.