Pre-Flight Check: Know Your Current Baseline
Before tweaking Apache configuration, you need a performance baseline. Run these commands to see what you're working with:
sudo apache2ctl status
free -m
top -p $(pgrep apache2 | head -1)
Note your current memory usage and active Apache processes. This gives you a before-and-after comparison once your Apache server performance tuning changes take effect.
Check your current Apache version and loaded modules:
apache2 -v
apache2ctl -M
Ubuntu VPS users often run default settings that waste memory on unused modules. We'll fix that.
Configure the Right MPM Module for Your Workload
Apache ships with multiple processing modules (MPMs). Most Ubuntu installations default to Event MPM, which handles concurrent connections efficiently. Verify your current MPM:
apache2ctl -V | grep MPM
If you're running Prefork MPM (common with PHP sites), consider switching to Event MPM with PHP-FPM. This change alone can double your concurrent connection capacity.
To switch to Event MPM:
sudo a2dismod mpm_prefork
sudo a2dismod php7.4 # or your PHP version
sudo a2enmod mpm_event
sudo systemctl restart apache2
Then install PHP-FPM if you haven't already:
sudo apt update
sudo apt install php8.1-fpm # adjust version as needed
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
This configuration separates web serving from PHP processing. Both become more efficient.
Optimize Memory Settings and Process Limits
Open your main Apache configuration file:
sudo nano /etc/apache2/apache2.conf
Find the Event MPM configuration block or add it if missing:
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
These settings work well for a 2GB VPS. For larger instances, scale proportionally:
- 4GB VPS: MaxRequestWorkers 800
- 8GB VPS: MaxRequestWorkers 1200
- StartServers: Keep low (2-4) to save boot memory
The key metric is MaxRequestWorkers. Set it too high and you'll run out of memory. Too low and you'll reject legitimate traffic.
Calculate your optimal MaxRequestWorkers by monitoring actual memory usage per Apache process:
ps aux | grep apache2 | awk '{sum+=$6} END {print "Average memory per process:", sum/NR, "KB"}'
Enable Smart Caching to Reduce Server Load
Apache's mod_cache can dramatically reduce load for static content. Enable the required modules:
sudo a2enmod cache
sudo a2enmod cache_disk
sudo a2enmod expires
sudo a2enmod headers
Create a cache directory with proper permissions:
sudo mkdir -p /var/cache/apache2/mod_cache_disk
sudo chown www-data:www-data /var/cache/apache2/mod_cache_disk
Add cache configuration to your virtual host or main config:
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheMaxFileSize 1000000
CacheIgnoreHeaders Set-Cookie
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
</IfModule>
This setup caches static files on disk and sets proper browser cache headers. Your server will handle repeat visits much faster.
Disable Unnecessary Modules
Ubuntu's Apache enables many modules by default. Each loaded module consumes memory and CPU cycles. List currently enabled modules:
apache2ctl -M | sort
Common modules you can safely disable on most hosting setups:
sudo a2dismod autoindex
sudo a2dismod negotiation
sudo a2dismod userdir
sudo a2dismod status # unless you need server-status
sudo a2dismod info # unless you need server-info
Keep essential modules like rewrite, ssl, headers, and deflate. If you're unsure about a module, check what uses it before disabling.
Many Hostperl VPS hosting customers see 10-15% memory reduction just from cleaning up unused modules.
Configure Compression for Faster Response Times
Enable mod_deflate to compress text content before sending to browsers:
sudo a2enmod deflate
Add compression configuration to your site's virtual host file:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
</IfModule>
This reduces bandwidth usage by 60-70% for text content. Your pages load faster and consume fewer resources per request.
Monitor Performance with Built-in Status Pages
Apache includes useful monitoring tools. Enable the status module:
sudo a2enmod status
Add status configuration to a virtual host (restrict access for security):
<Location "/server-status">
SetHandler server-status
Require ip 127.0.0.1
Require ip YOUR_MANAGEMENT_IP
</Location>
<Location "/server-info">
SetHandler server-info
Require ip 127.0.0.1
Require ip YOUR_MANAGEMENT_IP
</Location>
Replace YOUR_MANAGEMENT_IP with your actual IP address. Now you can visit http://yourserver.com/server-status to see real-time Apache performance metrics.
The status page shows active connections, request rate, and memory usage. Monitor it during traffic spikes to see how your tuning performs.
This monitoring capability proves valuable for shared hosting customers who need to track resource usage closely.
Set Up Log Rotation to Prevent Disk Issues
Large access logs can fill your VPS disk quickly. Configure logrotate for Apache:
sudo nano /etc/logrotate.d/apache2
Update the configuration for more frequent rotation:
/var/log/apache2/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
if /bin/systemctl status apache2 > /dev/null ; then \
/bin/systemctl reload apache2 > /dev/null; \
fi;
endscript
}
This rotates logs daily and keeps 14 days of compressed history. High-traffic sites might need hourly rotation.
You can find more detailed log management techniques in our complete logrotate guide for Ubuntu VPS.
Apply Security Headers While Maintaining Performance
Security headers protect your site without hurting performance. Add these to your virtual host:
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set Referrer-Policy strict-origin-when-cross-origin
These headers are processed efficiently by mod_headers and provide essential security without measurable performance impact.
For SSL-specific security improvements, check our SSL security headers guide which covers similar concepts for production environments.
Test Your Configuration Before Going Live
Always test Apache configuration changes before applying them to production:
sudo apache2ctl configtest
If the test passes, reload Apache to apply changes:
sudo systemctl reload apache2
Use reload instead of restart when possible - it applies new configuration without dropping existing connections.
Benchmark your site before and after changes using Apache Bench:
ab -n 1000 -c 10 http://yoursite.com/
This sends 1000 requests with 10 concurrent connections. Compare response times and requests per second to measure improvement.
Ready to put these Apache optimizations to work? Our managed VPS hosting gives you full root access to implement these performance tweaks. We handle the server maintenance while you focus on optimization.
Frequently Asked Questions
How much memory should I allocate per Apache worker?
Plan for 15-25MB per worker process with PHP applications. Monitor actual usage with ps aux and adjust MaxRequestWorkers accordingly. A 2GB VPS can typically handle 80-100 workers safely.
Should I use Prefork or Event MPM for WordPress sites?
Use Event MPM with PHP-FPM for better performance and memory efficiency. Prefork MPM is only necessary for legacy PHP applications that aren't thread-safe.
How often should I check Apache performance metrics?
Check server-status daily during normal operation, and monitor continuously during traffic spikes. Set up automated alerts if response times exceed your targets.
What's the fastest way to identify Apache performance bottlenecks?
Use server-status to see current load, check error logs for timeout messages, and monitor system memory usage. Most bottlenecks stem from inadequate MaxRequestWorkers settings or insufficient RAM.
Can I apply these settings to shared hosting environments?
Shared hosting customers cannot modify Apache's global configuration. These optimizations require VPS or dedicated server access where you control the Apache installation.

