Optimizing apache (httpd) for a development setup

The apache configuration file (usually httpd.conf) that ships with most linux distros has some configuration settings that are sub-optimal for running PHP applications (many optimizations described below will benefit other applications as well). I’ve found, after a bit of trial and error, that the following settings will let you squeeze a little extra out of the server running on your development machine.

The settings below affect the overall memory usage (even when idle) of the server since they define the minimum number of httpd processes (or threads) that are spawned and maintained. On my laptop, with 4GB RAM, the prefork and mpm worker settings are as follows (apache only uses one of them, depending on how it is compiled):

httpd.conf
1
<IfModule prefork.c>
    StartServers 8
    MinSpareServers 5
    MaxSpareServers 20
    ServerLimit 50
    MaxClients 50
    MaxRequestsPerChild 300 #Respawn a process after serving 300 requests. Curtails footprint bloat due to memory leaks.
</IfModule>

<IfModule worker.c>
    StartServers 2
    MaxClients 50
    MinSpareThreads 25
    MaxSpareThreads 50
    ThreadsPerChild 25
    MaxRequestsPerChild 300
</IfModule>

Other than tweaking the process/thread variables, there are a few more settings to look at:

httpd.conf
1
KeepAlive On #Reduces page load time by reusing tcp connections
MaxKeepAliveRequests 100
KeepAliveTimeout 15 #Set this value higher for longer-lived connections between browser and server, but if there are many simultaneous requests, this can cause some of them to unnecessarily queue up.
FileETag None

Additionally, you can instruct apache to compress content sent over the network using gzip by adding the following lines (I’ve put them in a separate _moddeflate.conf under /etc/httpd/conf.d, but that’s not necessary):

mod_deflate.conf
1
#GZIP configuration for enabling compresion
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

Assuming you’re doing PHP development, you might as well go ahead and enable APC. I’ve sometimes faced an issue with APC using stale opcode even when a newer version of the source file is available, but not very often. Some people prefer to keep APC disabled on development machines. In /etc/php.d/apc.ini (on Red Hat-like systems, including Fedora, CentOS, RHEL, Amazon Linux, etc.) enable the following settings:

/etc/php.d/apc.ini
1
apc.enabled=1
apc.shm_size=512M
; This is usually too much if you're running just one application.
; It's better to start with 128 MB and upscale only if this falls short.
; You can check APC memory usage using the apc.php file that ships with the default APC installation on most systems.
; (http://pecl.php.net/package/APC)

While we’re on the subject of optimization, we might as well tune our MySQL server to use its built-in query cache. In your /etc/my.cnf add the following line:

/etc/my.cnf
1
query_cache_size=64M

Restart your apache and mysql servers to apply the new settings.