This guide walks you through practical steps to optimize Apache on a VPS, reducing memory usage, improving response times, and handling more concurrent visitors without upgrading your plan. You’ll adjust Apache’s multi-processing module (MPM), enable caching, compress content, and tune kernel settings. Before starting, ensure you have root or sudo access to your server and a backup of your Apache configuration files.
What You’ll Need
- A VPS running Linux (Ubuntu, Debian, CentOS, or similar) with Apache installed.
- SSH access with sudo privileges.
- Basic familiarity with the command line.
- Apache’s
mod_rewrite,mod_headers, andmod_deflatemodules enabled (most distros enable them by default).
Step-by-Step
- Check your current Apache configuration and MPM.
Runapache2 -V(Debian/Ubuntu) orhttpd -V(RHEL/CentOS) to see which MPM is compiled in (prefork, worker, or event). Also checkapache2ctl -Mto list loaded modules. The event MPM is generally the best for high concurrency because it uses a single thread per connection and handles keep-alive efficiently. - Switch to the event MPM (if not already using it).
On Debian/Ubuntu:sudo a2dismod mpm_prefork && sudo a2enmod mpm_event. On RHEL/CentOS, edit/etc/httpd/conf.modules.d/00-mpm.confand comment out the prefork line, uncomment the event line. Then restart Apache:sudo systemctl restart apache2orsudo systemctl restart httpd. - Tune the MPM settings.
Edit the MPM configuration file (e.g.,/etc/apache2/mods-available/mpm_event.confon Debian/Ubuntu, or inside/etc/httpd/conf/httpd.confon RHEL). Set realistic values based on your VPS memory. For a 2GB VPS, a good starting point is:StartServers 2MinSpareThreads 5MaxSpareThreads 10ThreadLimit 64ThreadsPerChild 25MaxRequestWorkers 50MaxConnectionsPerChild 1000
AdjustMaxRequestWorkerscarefully—it determines how many simultaneous connections Apache can handle. A rough formula: MaxRequestWorkers ≈ (Total RAM – Other Processes RAM) / Average Apache process size. If you run out of memory, reduce this number. - Enable KeepAlive and adjust timeouts.
In your main config or a virtual host, set:KeepAlive OnMaxKeepAliveRequests 100KeepAliveTimeout 5Timeout 60
KeepAlive allows a single TCP connection to handle multiple requests, reducing overhead. A short KeepAliveTimeout (2-5 seconds) prevents idle connections from tying up workers. - Enable compression with mod_deflate.
Add this to your config or a.htaccessfile:AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
This reduces bandwidth usage and speeds up page loads, especially for text-heavy sites.
- Set up caching with mod_expires and mod_headers.
Enable the modules:sudo a2enmod expires headers(Debian/Ubuntu) or edit the config on RHEL. Then add to your virtual host:ExpiresActive On ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType text/css "access plus 1 week" ExpiresByType application/javascript "access plus 1 week"
This tells browsers to cache static assets, reducing repeat requests.
- Disable unused modules.
Runapache2ctl -Mand remove modules you don’t need (e.g.,mod_info,mod_statusif not used,mod_autoindex). Each module consumes memory. Usesudo a2dismod module_nameon Debian/Ubuntu, or comment outLoadModulelines on RHEL. - Optimize the kernel network settings.
Edit/etc/sysctl.confand add:net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_keepalive_time = 1800 net.ipv4.ip_local_port_range = 1024 65000
Then apply with
sudo sysctl -p. This allows the server to recycle TCP connections faster, which helps under high load. - Consider switching to PHP-FPM if using mod_php.
If you run PHP, the prefork MPM is required for mod_php, which is memory-hungry. Switching to PHP-FPM lets you use the event MPM and reduces memory usage per process. On Debian/Ubuntu:sudo apt install php-fpm, then disable mod_php (sudo a2dismod php7.4or similar) and enable proxy_fcgi (sudo a2enmod proxy_fcgi). Configure your virtual host to pass PHP requests to the FPM socket. - Monitor and adjust.
After changes, restart Apache and monitor withtop,free -m, andapache2ctl status. Check the error logs (/var/log/apache2/error.logor/var/log/httpd/error_log) for warnings about MaxRequestWorkers. Gradually increase or decrease settings based on actual traffic.
Common Mistakes
- Setting MaxRequestWorkers too high—this can exhaust memory and cause swapping, making the server slower than before.
- Using prefork MPM unnecessarily—if you don’t need mod_php, prefork wastes memory.
- Leaving KeepAliveTimeout too long—a 15-second timeout means idle connections hold workers for too long, reducing capacity.
- Enabling compression on already compressed files—like images or PDFs, which wastes CPU.
- Forgetting to restart Apache after config changes—changes don’t take effect until you do.
Tips
- Use a CDN for static assets to offload traffic from your VPS.
- Enable
mod_statusand access/server-statusto see live connections and identify bottlenecks. - Consider using a reverse proxy like Varnish or Nginx in front of Apache for high-traffic sites, but only if you’re comfortable with additional complexity.
- Regularly review your Apache logs to spot slow requests or errors.
- Keep your Apache version updated—newer versions often have performance improvements.
Troubleshooting
- Server becomes unresponsive after changes: Revert to the previous configuration, restart Apache, and check memory usage. Likely cause: MaxRequestWorkers too high.
- Error: “server reached MaxRequestWorkers setting”: Increase MaxRequestWorkers slightly, but only if you have spare memory. Otherwise, reduce KeepAliveTimeout or switch to a more efficient MPM.
- PHP scripts not executing after switching to PHP-FPM: Ensure the
proxy_fcgimodule is enabled and your virtual host has the correctSetHandlerdirective. For example:SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost". - Compression not working: Verify mod_deflate is enabled and that you haven’t set
SetEnv no-gzipor similar. Also check that the response headers includeContent-Encoding: gzip. - Changes not taking effect: Remember to restart Apache. Also check that you edited the correct config file—some distros include files from
conf-enabledthat override your settings.
Frequently Asked Questions
What is the best MPM for Apache on a VPS?
The event MPM is generally best for high concurrency and low memory usage, especially if you use PHP-FPM. It handles keep-alive connections more efficiently than prefork or worker.
How do I know how much memory each Apache process uses?
Run ps aux | grep apache2 (or httpd) and look at the RSS column. Then multiply by the number of processes. A typical Apache process uses 20-50 MB, but this varies with modules and application code.
Should I disable .htaccess files for performance?
Yes, if you have full access to the server config. Set AllowOverride None in your virtual host and move .htaccess directives into the main config. This avoids per-request file lookups.
How often should I restart Apache after tuning?
Only when you change configuration. Frequent restarts are not needed and can cause downtime. Use apache2ctl configtest before restarting to catch errors.
Can I use Apache with Nginx as a reverse proxy?
Yes, this is a common setup. Nginx handles static files and acts as a proxy for dynamic requests to Apache. This can improve performance but adds complexity.
What is the impact of enabling HTTPS on performance?
HTTPS adds CPU overhead for encryption, but modern servers handle it well. Use HTTP/2 (enabled by default in Apache 2.4.17+) and session resumption to mitigate the impact.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Tongue Escape Codes: What They Are0.20 movements/s
- Claude Code Workflow Habits Worth Trying
- HDMI 2.1 Explained: Why It Matters for Gaming
- Veeam Plugin for VMware Cloud Director: How It Works
- Best Path of Titans Server Hosting: Top Picks
- Oppo First Foldable: 120Hz Display & Snapdragon 888