Tuning Apache and Nginx on a VPS means adjusting web server parameters to get the most out of your available RAM, CPU, and connections. Without these changes, even a mid-range VPS can feel sluggish or unstable under moderate traffic.
This guide covers the highest-impact changes you can make today — no extra hardware required.
Why Tuning Matters on a VPS
Unlike a dedicated server with headroom to spare, a VPS has fixed RAM and CPU. Apache's and Nginx's default values are designed to work everywhere, not to perform optimally on your specific plan.
An untuned server typically shows:
- High response times under moderate traffic loads.
- Child processes holding RAM they no longer need.
- Connections dropped before the client finishes receiving data.
- No static-file caching, forcing a disk read on every request.
Check the VPS servers guide to understand what resources your instance actually has before touching any parameter.
Apache Tuning: the Key Settings
Apache uses an MPM (Multi-Processing Module) to handle connections. The most common MPM on Linux VPS hosts is event or worker; avoid prefork if you're running PHP-FPM.
MPM Event Configuration
Edit /etc/httpd/conf.modules.d/00-mpm.conf (CentOS/RHEL) or /etc/apache2/mods-enabled/mpm_event.conf (Debian/Ubuntu):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
Practical rule: MaxRequestWorkers should not exceed (available RAM in MB) / (average process RAM). On a 2 GB VPS with ~20 MB processes, a safe ceiling is 80-100.
Keep-Alive and Timeouts
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
A high KeepAliveTimeout keeps sockets open for no reason. Five seconds is enough for most sites.
Compression and Static File Caching
# mod_deflate — enable gzip compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
# mod_expires — cache headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
Nginx Tuning: the Key Settings
Nginx is asynchronous and event-driven, which makes it naturally more efficient for concurrent connections. Even so, its defaults leave performance on the table.
Worker Processes and Connections
worker_processes auto; # one per CPU core
worker_connections 1024; # connections per worker
events {
use epoll;
multi_accept on;
}
worker_processes auto detects available cores. multi_accept on lets each worker accept several connections in a single cycle.
HTTP Buffers and Timeouts
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
keepalive_requests 100;
client_body_buffer_size 128k;
client_max_body_size 10m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 1 32k;
postpone_output 1460;
}
Adjust client_max_body_size based on whether your site accepts file uploads.
Open File Cache
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
This directive reduces filesystem syscalls by keeping descriptors for frequently accessed files in memory.
Gzip in Nginx
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_comp_level 4 gives a solid balance between compression ratio and CPU use. Higher levels barely shrink files further but burn noticeably more CPU.
Quick Comparison: Defaults vs. Tuned Values
| Parameter | Apache (default) | Apache (2 GB VPS) | Nginx (default) | Nginx (2 GB VPS) |
|---|---|---|---|---|
| Processes/workers | MaxClients 150 | MaxRequestWorkers 80 | worker_processes 1 | worker_processes auto |
| KeepAlive timeout | 15 s | 5 s | 75 s | 15 s |
| Gzip | Off | On | Off | On, level 4 |
| File cache | No | mod_expires | No | open_file_cache |
Key Takeaways
- Size MaxRequestWorkers (Apache) or worker_connections (Nginx) to match your VPS's actual RAM.
- Drop KeepAliveTimeout to 5-15 seconds to free sockets faster.
- Enable gzip at compression level 4; it cuts transferred traffic by up to 70% for HTML/CSS/JS.
- Use mod_expires in Apache or open_file_cache in Nginx to reduce disk I/O for static assets.
- Monitor RAM and CPU after every change; never copy values from a 16 GB server onto a 1 GB VPS.
Need help dialing in your server configuration or figuring out whether your current VPS can handle your traffic? Reach out to the team at elenlace.com for a free infrastructure review.
FAQ
Apache or Nginx: which is faster on a VPS?
Nginx is more efficient for concurrent connections and RAM usage thanks to its async, event-driven architecture. However, Apache with MPM Event and PHP-FPM comes very close. In practice, the difference comes down to tuning more than choice of server.
How often should I revisit these settings?
Revisit them whenever you upgrade your VPS plan, launch a new high-traffic site, or notice latency spikes. A quarterly review is a solid maintenance habit.
Can I apply these changes on a live production server without restarting it?
Yes. Both Apache (apachectl graceful) and Nginx (nginx -s reload) reload their configuration without dropping active connections. Always validate syntax first: apachectl configtest or nginx -t.
Does tuning replace a CDN?
No — they're complementary. Tuning optimizes your origin server; a CDN offloads static-file traffic to edge nodes close to your users. Ideally, apply both.
Prefer it done for you? El Enlace handles hosting and professional web development.
Compare providers
Other providers and guides worth comparing: