Apache Performance Tuning

Apache's default configuration is conservative — it works on minimal hardware but leaves performance on the table on modern servers. The most impactful tuning decisions are: choosing the right MPM (Multi-Processing Module) for your workload, setting worker count to match your available memory, enabling gzip compression to reduce bandwidth, and adding caching headers to reduce repeated requests. These changes can reduce page load times by 40-60% on a typical web application.

MPM selection

MPMModelUse when
preforkProcess per connectionUsing mod_php (not thread-safe)
workerThread per connectionProxy/FastCGI setups without mod_php
eventAsync keep-aliveHigh concurrency, PHP-FPM (recommended)
# Check current MPM:
apache2ctl -M | grep mpm

# Switch to event MPM (better for PHP-FPM):
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2

Worker and connection settings

# Tune MPM event settings based on server RAM:
# Rule of thumb: MaxRequestWorkers = (RAM_MB - 200) / 20
# For 2GB RAM: (2048 - 200) / 20 = ~90

sudo nano /etc/apache2/mods-available/mpm_event.conf

Tuned mpm_event.conf for a 2GB server

<IfModule mpm_event_module>
    StartServers             2
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadLimit             64
    ThreadsPerChild         25
    MaxRequestWorkers       90
    MaxConnectionsPerChild   0
</IfModule>
# Also tune these global settings in apache2.conf or virtual host:
# KeepAlive On              ← reuse connections for multiple requests
# KeepAliveTimeout 5        ← seconds to wait for next request (default too high)
# MaxKeepAliveRequests 100

Caching static content

# Enable mod_expires and mod_headers:
sudo a2enmod expires headers

# In your virtual host config, add caching for static files:
sudo nano /etc/apache2/sites-available/example.com.conf

Cache headers configuration in virtual host

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css             "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/jpeg           "access plus 1 year"
    ExpiresByType image/png            "access plus 1 year"
    ExpiresByType image/webp           "access plus 1 year"
    ExpiresByType font/woff2           "access plus 1 year"
</IfModule>

Enabling compression

# Enable gzip compression for text responses:
sudo a2enmod deflate

# Add to virtual host or apache2.conf:
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json application/xml
    DeflateCompressionLevel 6    ← good balance of speed vs compression
</IfModule>

sudo apache2ctl configtest && sudo systemctl reload apache2

# Verify compression is working:
curl -H "Accept-Encoding: gzip" -I http://yourdomain.com/ | grep -i content-encoding

curl output showing gzip encoding

Content-Encoding: gzip

Measuring performance

# Apache Bench (ab) is included with apache2-utils
# Test: 1000 requests, 10 concurrent:
ab -n 1000 -c 10 http://yourdomain.com/

ab benchmark output

Requests per second:    452.34 [#/sec]
Time per request:       22.1 [ms] (mean)
Transfer rate:          1234.56 [Kbytes/sec] received

Percentage of requests served within:
  50%      19 ms
  90%      45 ms
  99%      123 ms
# Check server status (enable mod_status first):
sudo a2enmod status
# Visit: http://SERVER_IP/server-status?auto    (restrict to localhost in production)

Conclusion

Priority order for Apache performance tuning: (1) switch to event MPM with PHP-FPM instead of prefork with mod_php — this alone often doubles concurrent request capacity; (2) tune MaxRequestWorkers to fit your server's RAM; (3) enable gzip compression — reduces HTML/CSS/JS transfer size by 60-80%; (4) add Expires headers for static assets to eliminate repeat requests. Always benchmark before and after with ab to measure actual impact.

FAQ

Is Apache Performance Tuning important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support