Enable mod_headers and Verify That mod_expires is Working in Apache
This guide walks through both Apache modules together: enable mod_headers (which sets HTTP response headers) and verify mod_expires (which controls browser caching). For the per-module deep dives, see How to Install and Enable mod_headers in Apache, How to Enable and Configure mod_headers in Apache, Understanding mod_headers in Apache, How to Enable and Configure mod_expires in Apache for Caching, and the parallel Vary: Accept-Encoding Header in Apache.
apachectl -M | grep headers
Expected output:
headers_module (shared)
If mod_headers is not enabled, proceed with enabling it.
Enable mod_headers
For Debian/Ubuntu
sudo a2enmod headers
sudo systemctl restart apache2
For CentOS/RHEL
Ensure mod_headers is loaded in Apache's configuration file:
sudo nano /etc/httpd/conf/httpd.conf
Find or add:
LoadModule headers_module modules/mod_headers.so
Restart Apache:
sudo systemctl restart httpd
Test if mod_headers is working by running:
curl -I http://yourdomain.com
Expected output (example):
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Verify That mod_expires is Working
Check if mod_expires is Enabled
Run:
apachectl -M | grep expires
Expected output:
expires_module (shared)
If mod_expires is not enabled, enable it.
For Debian/Ubuntu
sudo a2enmod expires
sudo systemctl restart apache2
For CentOS/RHEL
Ensure mod_expires is loaded in Apache's configuration file:
sudo nano /etc/httpd/conf/httpd.conf
Find or add:
LoadModule expires_module modules/mod_expires.so
Restart Apache:
sudo systemctl restart httpd
Check HTTP Headers for mod_expires
Run:
curl -I http://yourdomain.com/path-to-image.jpg
Expected output:
Cache-Control: max-age=15552000
Expires: Tue, 10 Sep 2024 12:00:00 GMT
If the headers are missing, check Apache logs:
sudo tail -f /var/log/apache2/error.log
or for CentOS:
sudo tail -f /var/log/httpd/error_log
Summary of Fixes
| Issue | Fix |
|---|---|
| mod_headers not enabled | sudo a2enmod headers (Debian) or LoadModule headers_module (CentOS) |
| mod_expires not enabled | sudo a2enmod expires (Debian) or LoadModule expires_module (CentOS) |
| Headers not appearing in response | Add rules in Apache config or .htaccess |
| Changes not applying | Restart Apache: sudo systemctl restart apache2 |
| Debug headers | Use curl -I http://yourdomain.com |
Now both mod_headers and mod_expires are enabled and verified!
