Skip to content

Apache CSP Integration

Add CSP headers to your Apache configuration.

  • Apache 2.2+ with mod_headers enabled
  • Access to httpd.conf, .htaccess, or virtual host configuration

Ensure mod_headers is enabled:

Terminal window
# Debian/Ubuntu
sudo a2enmod headers
sudo systemctl restart apache2
# RHEL/CentOS
# Usually enabled by default, check httpd.conf

Add to your virtual host or .htaccess:

# Report-only mode (recommended to start)
Header set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID"

Once you’ve tuned your policy:

Header set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID"
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
# CSP Header
Header set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://api.example.com; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID"
# Other security headers
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "DENY"
# SSL configuration
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
</VirtualHost>
DirectiveBehavior
Header setSets header, overwriting any existing value
Header appendAppends to existing header
Header addAdds header even if one already exists
Header always setSets header for all responses (including errors)

For directory-level configuration:

.htaccess
<IfModule mod_headers.c>
Header set Content-Security-Policy-Report-Only "default-src 'self'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID"
</IfModule>
<Directory /var/www/html/admin>
Header set Content-Security-Policy "default-src 'self'; script-src 'self'"
</Directory>
<Directory /var/www/html/public>
Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'"
</Directory>
  1. Check if mod_headers is enabled: apachectl -M | grep headers
  2. Verify AllowOverride if using .htaccess
  3. Check Apache error logs

Test configuration before restarting:

Terminal window
apachectl configtest

Test your configuration:

Terminal window
curl -I https://your-site.com | grep -i content-security-policy