• Call: +1 (858) 429-9131

Apache server optimization

One Comment

  1. bobinson says:

    Apache server optimization

    Apache server performance can be improved by custom configuration of the server. You can follow the below steps:


    Run apache with only the required modules. This reduces the memory footprint and hence the server performance.
    In Debian / Ubuntu

    For example disable a module called foo or mime_magic, enter:
    a2dismod foo
    a2dismod mime_magic

    For enabling the module:
    a2enmod module name
    In CentOS / Redhat (RHEL) / Fedora Linux

    To enable modules, you need to modify *.conf file stored in /etc/httpd/conf.d/ directory. Apache scans for files with the .conf suffix at start up. So if the system does not need to use mod_python, rename ‘python.conf’ to ‘python.bak’ and restart Apache with the command ‘service httpd restart in order to disable that particular module and save memory.
    cd /etc/httpd/conf.d/
    mv perl.conf no.perl.bak
    /etc/init.d/httpd restart

    To re-enable modules, simply rename them to their original names and restart Apache to get back module functionality:


    Multi-Processing Modules (MPMs) are responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests. Only one MPM can be loaded into the server at any time.

    For finding the current mpm, use the below command. httpd -l

    Worker MPM : uses multiple child processes. It’s multi-threaded within each child and each thread handles a single connection. Worker is fast and highly scalable and the memory footprint is comparatively low. It’s well suited for multiple processors. On the other hand, worker is less tolerant to faulty modules and faulty threads can affect all the threads in a child process.

    Prefork MPM : uses multiple child processes, each child handles one connection at a time. Prefork is well suited for single or double CPU systems, speed is comparable to that of worker and it’s highly tolerant to faulty modules and crashing children. But the memory usage is high, more traffic leads to more memory usage.

    The HostnameLookups directive enables DNS lookup so that hostnames can be logged instead of the IP address. This adds latency to every request since the DNS lookup has to be completed before the request is finished. HostnameLookups is Off by default in Apache 1.3 and above. Leave it Off and use post-processing program such as logresolve to resolve IP addresses in Apache’s access logfiles When using Allow from or Deny from directives, use IP address instead of a domain name or a hostname. Otherwise a double DNS lookup is performed to make sure that the domain name or the hostname is not being spoofed.

    If AllowOverride is not set to ‘None’, then Apache will attempt to open .htaccess file (as specified by AccessFileName directive) in each directory that it visits. For example:

    DocumentRoot /var/www/html AllowOverride all

    If a request is made for URI /index.html, then Apache will attempt to open /.htaccess, /var/.htaccess, /var/www/.htaccess, and /var/www/html/.htaccess. These additional file system lookups add to the latency. If .htaccess is required for a particular directory, then enable it for that directory alone.
    FollowSymLinks and SymLinksIfOwnerMatch:

    If FollowSymLinks option is set, then the server will follow symbolic links in this directory. If SymLinksIfOwnerMatch is set, then the server will follow symbolic links only if the target file or directory is owned by the same user as the link.

    If SymLinksIfOwnerMatch is set, then Apache will have to issue additional system calls to verify whether the ownership of the link and the target file match. Additional system calls are also needed when FollowSymLinks is NOT set. For example:

    DocumentRoot /vaw/www/html Options SymLinksIfOwnerMatch

    For a request made for URI /index.html, Apache will perform lstat() on /var, /var/www, /var/www/html, and /var/www/html/index.html. These additional system calls will add to the latency. For maximum performance, set FollowSymLinks everywhere and never set SymLinksIfOwnerMatch. Or else, if SymLinksIfOwnerMatch is required for a directory, then set it for that directory alone.

    Total number of concurrent connections.

    Locate it in the configuration file. This should be set to a reasonable value.

    MaxClients = 150 x RAM (GB)

    So for example if you have 2 GB or RAM set this value to 300. There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached.

    This value should be same as MaxClients

    ServerLimit = 150 x RAM (GB)

    MinSpareServers and MaxSpareServers

    MaxSpareServers and MinSpareServers control how many spare (unused) child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so having MaxSpareServers set too high can cause resource problems. On the other hand, if the number of unused servers drops below MinSpareServers, Apache will fork (an expensive operation) new child-processes until MinSpareServers is satisfied.

    Leave those values to:

    MinSpareServers 5

    MaxSpareServers 10

    If you have more them 2 GB of RAM and you run a resource intensive website consider increasing MaxSpareServers.

    Controls the number of request the a child serves before the child is killed. This should not be set too low as it will put an unnecessary load on the apache server to recreate the child. MaxRequestsPerChild 1000 for 1 GB RAM 10,000 for 2 GB and 0 for more than 2 GB RAM
    KeepAlive and KeepAliveTimeout:

    The KeepAlive directive allows multiple requests to be sent over the same TCP connection. This is particularly useful while serving HTML pages with lot of images. If KeepAlive is set to Off, then for each images, a separate TCP connection has to be made. Overhead due to establishing TCP connection can be eliminated by turning On KeepAlive.

    KeepAliveTimeout determines how long to wait for the next request. Set this to a low value, perhaps between two to five seconds. If it is set too high, child processed are tied up waiting for the client when they could be used for serving new clients.

    Sets the number of child server processes created on startup. This setting depends greatly on the type of webserver you run. If you run low traffic websites on that server set it low to something like 5. If you have resource intensive websites on that server you should set it close to MaxClients.

    StartServers 5

    The amount of time Apache will wait for three things: the total amount of time it takes to receive a GET request, The amount of time between receipt of TCP packets on a POST or PUT request, the amount of time between ACKs on transmissions of TCP packets in responses.

    The default value is 300. You should set time to something a bit lower. A setting of 150 is probably ok. This will also help in case of small DOS attacks like to ones targeting some phpBB forums. Do NOT set it any lower then 10 as your users will start having timeout problems.

    Timeout 150

    After you have done all the necessary changes you can go ahead and restart Apache.

    Sample values:

    MinSpareServers 5 MaxSpareServers 10 ServerLimit 600 MaxClients 600 MaxRequestsPerChild 0 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 3

    Timeout 30

Leave a Reply