Run:
apachectl -M | grep headers
Expected output:
headers_module (shared)
If mod_headers is not enabled, proceed with enabling it.
sudo a2enmod headers
sudo systemctl restart apache2
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
curl -I http://yourdomain.com
Expected output (example):
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Run:
apachectl -M | grep expires
Expected output:
expires_module (shared)
If mod_expires is not enabled, enable it.
sudo a2enmod expires
sudo systemctl restart apache2
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
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
| 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!