Apache vs Nginx on VPS: Which One to Choose?
Apache and Nginx are the two web servers that have dominated the market for years, but they work very differently at the architectural level. Understanding these differences helps you choose the right one for your workload.
Apache: Process-based Architecture
Apache uses a process-based or thread-based model where each connection gets its own process or thread. This makes it easy to configure on a per-directory basis using .htaccess files.
Pros
- Supports
.htaccess— you can change config at the directory level without restarting - Extensive modules — you can do many things from a single config file
- Most WordPress and PHP applications are tested on Apache first
- Plenty of old but comprehensive documentation and tutorials
Cons
- Uses more RAM than Nginx, especially with many concurrent connections
- C10K problem — performance drops when handling thousands of concurrent connections
- Serving static files is slower compared to Nginx
Nginx: Event-driven Architecture
Nginx uses an event-driven, asynchronous architecture. A single server can handle multiple connections simultaneously without spawning a new process for each connection.
Pros
- Uses significantly less RAM, especially under high load
- Serves static files (CSS, JS, images) much faster than Apache
- Perfect for reverse proxying in front of backend applications
- Config syntax is clean and easy to understand
Cons
- Does not support
.htaccess— you must configure everything in server blocks - Requires config reload when making changes (not a full restart, but still necessary)
Comparison Table
| Criteria | Apache | Nginx |
|---|---|---|
| Architecture | Process/Thread-based | Event-driven |
| RAM Usage | Higher | Lower |
| Static files | Good | Much faster |
| .htaccess | Supported | Not supported |
| Reverse proxy | Can do it | Better suited |
| PHP support | mod_php built-in | Via PHP-FPM |
Which One Should You Choose?
Choose Apache if
- You use shared-hosting style applications that depend on
.htaccess - You need specific modules like mod_rewrite that are complex in Nginx
- You have older applications written specifically with Apache config in mind
Choose Nginx if
- You want high performance with low RAM usage
- You're doing reverse proxying in front of Node.js, Python, Go, or other applications
- You have high traffic or many concurrent connections
- You serve large amounts of static files
Summary
Nginx is a better choice in terms of performance for low-RAM VPS, but Apache still has advantages for applications that heavily rely on .htaccess. Some organizations use both: Nginx as the front-end receiving requests and forwarding them to Apache — getting the best of both worlds.
Using Both: Nginx + Apache
Some organizations use Nginx as a front-end reverse proxy receiving traffic from the internet and forwarding it to Apache running on port 8080. This way they get the benefits of both — Nginx handles static files and SSL termination, while Apache supports .htaccess and legacy applications.
# Nginx listens on port 80/443, forwards to Apache on port 8080
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# Static files served directly from Nginx
location ~* \.(css|js|jpg|png|gif|ico|woff2)$ {
root /var/www/html;
expires 1y;
}
}
Verify Nginx is Working Correctly
After setting up or making changes to Nginx, verify that everything is running as expected:
# Check Nginx syntax
nginx -t
# Check if Nginx is running
systemctl status nginx
# Test a request to your site
curl -I https://yourdomain.com
# Check active connections
netstat -an | grep :80 | grep ESTABLISHED | wc -l
nginx -t before reloading to catch syntax errors early and prevent downtime.Common Issues & Troubleshooting
Nginx: "502 Bad Gateway" after changing PHP-FPM socket
The socket name in your config doesn't match the actual PHP-FPM socket:
# Check where PHP-FPM socket is located
ls /run/php/
# You might see php8.3-fpm.sock or php8.1-fpm.sock
# Fix it in your Nginx site config
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
nginx -t && systemctl reload nginx
Apache: .htaccess doesn't work / mod_rewrite doesn't redirect
You need to enable AllowOverride All in your VirtualHost config:
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
# Enable mod_rewrite
a2enmod rewrite
systemctl restart apache2
Nginx: Config changes don't take effect
# Test config before reloading
nginx -t
# If it passes, reload
systemctl reload nginx
Apache: Memory usage is abnormally high
Apache is using the prefork MPM which spawns many processes. Try switching to the event MPM:
a2dismod mpm_prefork
a2enmod mpm_event php8.3-fpm
systemctl restart apache2
Frequently Asked Questions
Can I switch from Apache to Nginx mid-deployment?
Yes, but you'll need to convert all your config — Apache VirtualHosts become Nginx server blocks, and .htaccess rewrite rules become Nginx location directives. It's recommended to test on a staging environment first.
For a 1 GB RAM VPS, which should I choose?
Nginx is clearly the winner — it uses significantly less RAM than Apache. For WordPress on low-RAM VPS, Nginx + PHP-FPM saves about 200–400 MB compared to Apache + mod_php.
Can Nginx handle multiple domains (virtual hosting)?
Yes, create multiple server blocks in /etc/nginx/sites-available/ and symlink them to sites-enabled/, similar to Apache VirtualHosts.
I need .htaccess but want Nginx performance. Is that possible?
Not directly — Nginx doesn't support .htaccess. If your application depends heavily on .htaccess, you'll either need to use Apache or the Nginx+Apache stack. Alternatively, you can migrate your .htaccess rules directly into your Nginx config.