keepcalculator

06 Numbers and Powers

Apache Web Server Performance Tuning on a VPS

Card No. 06·71159 · Updated · Standard formula, shown below

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, and mod_deflate modules enabled (most distros enable them by default).

Step-by-Step

  1. Check your current Apache configuration and MPM.
    Run apache2 -V (Debian/Ubuntu) or httpd -V (RHEL/CentOS) to see which MPM is compiled in (prefork, worker, or event). Also check apache2ctl -M to 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.
  2. 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.conf and comment out the prefork line, uncomment the event line. Then restart Apache: sudo systemctl restart apache2 or sudo systemctl restart httpd.
  3. Tune the MPM settings.
    Edit the MPM configuration file (e.g., /etc/apache2/mods-available/mpm_event.conf on Debian/Ubuntu, or inside /etc/httpd/conf/httpd.conf on RHEL). Set realistic values based on your VPS memory. For a 2GB VPS, a good starting point is:
    StartServers 2
    MinSpareThreads 5
    MaxSpareThreads 10
    ThreadLimit 64
    ThreadsPerChild 25
    MaxRequestWorkers 50
    MaxConnectionsPerChild 1000
    Adjust MaxRequestWorkers carefully—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.
  4. Enable KeepAlive and adjust timeouts.
    In your main config or a virtual host, set:
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 5
    Timeout 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.
  5. Enable compression with mod_deflate.
    Add this to your config or a .htaccess file:
    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.

  6. 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.

  7. Disable unused modules.
    Run apache2ctl -M and remove modules you don’t need (e.g., mod_info, mod_status if not used, mod_autoindex). Each module consumes memory. Use sudo a2dismod module_name on Debian/Ubuntu, or comment out LoadModule lines on RHEL.
  8. Optimize the kernel network settings.
    Edit /etc/sysctl.conf and 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.

  9. 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.4 or similar) and enable proxy_fcgi (sudo a2enmod proxy_fcgi). Configure your virtual host to pass PHP requests to the FPM socket.
  10. Monitor and adjust.
    After changes, restart Apache and monitor with top, free -m, and apache2ctl status. Check the error logs (/var/log/apache2/error.log or /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_status and access /server-status to 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_fcgi module is enabled and your virtual host has the correct SetHandler directive. 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-gzip or similar. Also check that the response headers include Content-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-enabled that 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

The whole Numbers and Powers drawer →