
Understanding Apache Performance Bottlenecks on VPS
Apache performance tuning starts with identifying where your server struggles under load. Your VPS might handle 50 concurrent users smoothly but crash at 200.
The culprit usually lies in memory allocation, module configuration, or connection handling.
Apache's default configuration assumes shared hosting environments with limited resources. On your HostMyCode VPS, you control the entire stack. This means you can push Apache harder while maintaining stability.
Most performance issues stem from three areas: worker process configuration, memory management, and unnecessary modules. Each worker process consumes 10-25MB of RAM.
Run too many, and you'll swap to disk. Run too few, and requests queue up.
Choosing the Right Apache MPM for Your VPS
Apache's Multi-Processing Module (MPM) determines how your server handles concurrent connections. The choice affects memory usage, CPU efficiency, and maximum throughput.
Check your current MPM:
apache2ctl -V | grep -i mpm
Prefork MPM creates separate processes for each request. It's stable but memory-hungry. Each process uses 15-25MB. Good for PHP sites that need process isolation.
Worker MPM uses threads within processes. More efficient with memory. Handles 2-3x more concurrent connections than Prefork with the same RAM.
Event MPM (recommended for 2026) handles keep-alive connections efficiently. Frees up worker threads faster. Best choice for modern web applications.
Switch to Event MPM on Ubuntu:
sudo a2dismod mpm_prefork
sudo a2dismod php7.4 # if using mod_php
sudo a2enmod mpm_event
sudo a2enmod php7.4-fpm # use PHP-FPM instead
sudo systemctl restart apache2
Optimizing Apache Memory Configuration
Memory configuration directly impacts how many concurrent users your Apache server can handle. Get this wrong, and your VPS will either crash or waste resources.
Calculate your available memory for Apache:
free -h
# Subtract OS overhead (usually 512MB-1GB)
# Subtract other services (MySQL, Redis, etc.)
For Event MPM, edit /etc/apache2/mods-available/mpm_event.conf:
<IfModule mpm_event_module>
ServerLimit 16
MaxRequestWorkers 800
ThreadsPerChild 50
MinSpareThreads 75
MaxSpareThreads 250
ThreadLimit 64
AsyncRequestWorkerFactor 2
</IfModule>
This configuration supports 800 concurrent connections using roughly 400-600MB of RAM. Each process handles 50 threads.
Adjust based on your VPS memory.
For a 4GB VPS with 2GB available for Apache:
- ServerLimit: 20
- MaxRequestWorkers: 1000
- ThreadsPerChild: 50
Monitor memory usage after changes:
ps aux | grep apache2 | awk '{sum+=$6} END {print sum/1024 " MB"}'
Essential Apache Modules for VPS Performance
Default Apache installations load modules you'll never use. Each module consumes memory and adds overhead.
Disable unnecessary modules and enable performance boosters.
List currently loaded modules:
apache2ctl -M
Disable common modules you probably don't need:
sudo a2dismod autoindex
sudo a2dismod status
sudo a2dismod info
sudo a2dismod cgi
sudo a2dismod negotiation
sudo a2dismod userdir
Enable essential performance modules:
sudo a2enmod expires
sudo a2enmod deflate
sudo a2enmod headers
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod http2
mod_expires sets browser caching headers automatically. Configure in your virtual host or .htaccess:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
mod_deflate compresses responses before sending them. Reduces bandwidth by 60-80% for text content:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.gif$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.jpe?g$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.png$ no-gzip dont-vary
</IfModule>
Restart Apache after module changes:
sudo systemctl restart apache2
Configuring HTTP/2 for Better Performance
HTTP/2 multiplexes requests over a single connection. Reduces latency and improves page load times.
Essential for modern web performance.
Enable HTTP/2 support:
sudo a2enmod http2
sudo a2enmod ssl # HTTP/2 requires SSL
Add HTTP/2 directive to your SSL virtual host:
<VirtualHost *:443>
Protocols h2 http/1.1
ServerName example.com
DocumentRoot /var/www/example.com
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/private.key
# HTTP/2 works best with Event MPM
H2Push on
H2PushPriority * after
</VirtualHost>
Test HTTP/2 support:
curl -I --http2 https://yourdomain.com
Look for HTTP/2 200 in the response. Chrome DevTools Network tab also shows the protocol version.
PHP-FPM Integration and Optimization
Running PHP as an Apache module (mod_php) forces you to use Prefork MPM. This wastes memory and reduces concurrency.
PHP-FPM runs as a separate process pool.
Install PHP-FPM on Ubuntu:
sudo apt update
sudo apt install php-fpm
Configure Apache to use PHP-FPM:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm # adjust version as needed
In your virtual host configuration:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>
Optimize PHP-FPM pool settings in /etc/php/8.1/fpm/pool.d/www.conf:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1000
For a 4GB VPS, this configuration allows 50 concurrent PHP processes. Each PHP-FPM process typically uses 20-40MB.
Our MySQL performance monitoring guide covers database optimization that complements Apache tuning.
Apache Caching Strategies
Caching reduces server load by serving stored responses instead of generating new ones. Apache offers several caching mechanisms.
mod_cache_disk stores cached content on disk:
sudo a2enmod cache
sudo a2enmod cache_disk
sudo a2enmod headers
Configure disk caching in your virtual host:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
CacheEnable disk /
CacheRoot /var/cache/apache2/mod_cache_disk
CacheDefaultExpire 3600
CacheMaxExpire 86400
# Don't cache dynamic content
CacheDisable /wp-admin
CacheDisable /api
</VirtualHost>
Create the cache directory:
sudo mkdir -p /var/cache/apache2/mod_cache_disk
sudo chown www-data:www-data /var/cache/apache2/mod_cache_disk
mod_expires sets proper cache headers for browsers:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 hour"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/png "access plus 1 year"
</IfModule>
Connection and Timeout Optimization
Default Apache timeouts assume slow networks and patient users. Modern applications need faster failover and better resource utilization.
Edit /etc/apache2/apache2.conf:
# Reduce timeout for slow clients
Timeout 30
# Keep connections alive but not too long
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# Faster hostname resolution
HostnameLookups Off
# Server signature (security)
ServerTokens Prod
ServerSignature Off
Lower timeouts free up workers faster. KeepAlive reuses connections for multiple requests.
Set KeepAliveTimeout low (2-5 seconds) to prevent idle connections from hogging workers.
Monitor connection states:
netstat -an | grep :80 | wc -l # total connections
netstat -an | grep :80 | grep ESTABLISHED | wc -l # active
SSL/TLS Performance Optimization
SSL handshakes consume CPU cycles. Optimize cipher selection and session caching for better HTTPS performance.
Configure strong but fast ciphers in /etc/apache2/mods-available/ssl.conf:
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCipherSuite ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20:!aNULL:!MD5:!DSS
SSLHonorCipherOrder off
# Session caching
SSLSessionCache shmcb:/var/run/apache2/ssl_scache(512000)
SSLSessionCacheTimeout 300
# OCSP stapling
SSLUseStapling on
SSLStaplingCache shmcb:/var/run/apache2/ssl_stapling(32768)
Enable HTTP Strict Transport Security (HSTS):
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Test SSL performance:
openssl s_time -connect yourdomain.com:443 -new -verify 2
Monitoring Apache Performance
Enable server-status for real-time monitoring:
sudo a2enmod status
Add to your Apache configuration:
<Location "/server-status">
SetHandler server-status
Require ip 127.0.0.1
Require ip YOUR_IP_ADDRESS
</Location>
<Location "/server-info">
SetHandler server-info
Require ip 127.0.0.1
Require ip YOUR_IP_ADDRESS
</Location>
Monitor key metrics:
# Current requests and workers
curl -s http://localhost/server-status?auto
# Apache error log
sudo tail -f /var/log/apache2/error.log
# Monitor memory usage
watch 'ps aux | grep apache2 | awk "{sum+=\$6} END {print sum/1024 \" MB\"}'"
Key metrics to watch:
- Requests per second: Should increase after optimization
- Average response time: Lower is better
- Memory per worker: Should remain stable
- Active vs idle workers: Adjust MPM settings if needed
Check out our comprehensive Netdata monitoring guide for advanced server metrics.
Load Testing Your Optimized Apache Setup
Optimization without testing is guesswork. Use tools to simulate real-world traffic and measure improvements.
Install Apache Bench (ab):
sudo apt install apache2-utils
Basic load test:
ab -n 1000 -c 50 http://yourdomain.com/
This sends 1000 requests with 50 concurrent connections. Note the "Requests per second" value.
For more realistic testing, use different URLs:
ab -n 500 -c 25 http://yourdomain.com/page1.html
ab -n 500 -c 25 http://yourdomain.com/page2.html
Install wrk for advanced load testing:
sudo apt install wrk
Test with custom scripts:
wrk -t12 -c400 -d30s --script=post.lua http://yourdomain.com/
Monitor server resources during tests:
htop # CPU and memory usage
iotop # disk I/O
netstat -i # network traffic
Record baseline performance before optimization. Test again after each change to measure impact.
Ready to implement these Apache performance optimizations? HostMyCode VPS plans provide the full root access and resources you need for serious Apache tuning. Our managed VPS hosting includes expert assistance if you need help optimizing your setup.
Frequently Asked Questions
What's the best Apache MPM for high-traffic websites?
Event MPM is recommended for 2026. It handles keep-alive connections efficiently and supports more concurrent users than Prefork or Worker MPM. Use it with PHP-FPM instead of mod_php for best performance.
How much memory should I allocate to Apache on my VPS?
Allocate 60-70% of available RAM to Apache after accounting for the OS and other services. On a 4GB VPS, reserve 1GB for the system and other services, leaving 3GB for Apache workers.
Do Apache performance optimizations work with cPanel?
Yes, but cPanel manages many Apache settings through its interface. Some optimizations require careful coordination with cPanel's configuration management. Always test changes in a staging environment first.
How often should I monitor Apache performance?
Monitor continuously with automated tools like Netdata or Prometheus. Review performance metrics weekly and after any configuration changes. Set up alerts for high memory usage, slow response times, or error rate spikes.
Can I use Apache caching with WordPress?
Apache's mod_cache works with WordPress but requires careful configuration. Exclude admin areas and dynamic content. Many WordPress sites prefer dedicated caching plugins like W3 Total Cache or WP Rocket combined with Apache's browser caching headers.