Apache2 on CentOS: Content Optimization via Configuration Tuning
Optimizing Apache2 on CentOS for content delivery involves adjusting configuration parameters to improve concurrency, reduce latency, and efficiently serve static/dynamic content. Below are key steps categorized by optimization type:
MPMs determine how Apache handles concurrent requests. CentOS typically uses prefork (default) for compatibility, but worker or event is better for high traffic.
prefork (suitable for non-threaded content like PHP):<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 256
MaxConnectionsPerChild 1000
</IfModule>
worker/event (threaded, better for concurrent requests):<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 256
MaxConnectionsPerChild 10000
</IfModule>
Note: Disable unused MPMs (e.g., comment out LoadModule mpm_prefork_module if using event) to avoid conflicts.KeepAlive reuses TCP connections for multiple requests, reducing handshake overhead. Add to your config:
KeepAlive On
MaxKeepAliveRequests 100 # Limits requests per connection to prevent abuse
KeepAliveTimeout 5 # Closes idle connections after 5 seconds
This is particularly effective for websites with multiple assets (CSS/JS/images) per page.
Apache needs sufficient file descriptors to handle concurrent connections.
/etc/security/limits.conf and add:* soft nofile 65535
* hard nofile 65535
/etc/pam.d/common-session and /etc/pam.d/common-session-noninteractive:session required pam_limits.so
ulimit -n 65535 temporarily.Adjust kernel settings to improve network performance and memory management. Edit /etc/sysctl.conf:
net.core.somaxconn = 65535 # Max connections queued for a socket
net.ipv4.tcp_max_syn_backlog = 65535 # Max SYN requests queued
net.ipv4.tcp_tw_reuse = 1 # Reuse TIME-WAIT sockets
net.ipv4.tcp_fin_timeout = 30 # Close idle connections faster
net.ipv4.ip_local_port_range = 1024 65535 # Available ports for outgoing connections
vm.swappiness = 10 # Reduce swapping (keep data in RAM)
Apply changes with sysctl -p.
Reduce backend load and improve response times by caching static content.
mod_expires to set cache durations:<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 month"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 30 days"
</IfModule>
mod_cache_disk to cache dynamic content (e.g., PHP pages):<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
Create the cache directory and set permissions:mkdir -p /var/cache/apache2/mod_cache_disk
chown -R apache:apache /var/cache/apache2
Reduce transfer size with Gzip compression. Enable mod_deflate:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
Exclude images (already compressed) to save CPU cycles.
Reduce memory usage by disabling unused modules. List loaded modules with httpd -M, then disable unnecessary ones (e.g., mod_autoindex if not needed):
a2dismod autoindex
systemctl restart httpd
Use tools to identify bottlenecks:
top/htop: Check CPU/memory usage.netstat/ss: Monitor connections (e.g., ss -s for socket statistics).apachetop: View real-time request stats (install via yum install apachetop).Regularly review logs (/var/log/httpd/access_log and /var/log/httpd/error_log) to spot issues.
Lower timeout values to free up resources for active connections:
Timeout 300 # Default is 300 seconds; reduce to 30-60 for most sites
For very high traffic, place Nginx in front of Apache as a reverse proxy. Nginx handles static content efficiently, while Apache processes dynamic content (PHP/Python). Configure Nginx to proxy requests to Apache (typically on port 8080).
After making changes, always test the configuration for syntax errors (apachectl configtest) and restart Apache (systemctl restart httpd) to apply changes. Monitor performance post-change to ensure optimizations meet your traffic needs.