In this days I’ve to work for a little project that require the optimization for a web server, that led me to question for optimizing Apache web server to the best way, depending in different factors. With this post I’ll show some basic questions that can help to you for optimize your web server and may vary depending in your hardware specs and the use that your web server will do or your web application.

By default apache is compiled with the mpm mod_prefork.c. We can check our compiled modules with this command:

[[email protected]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c

Before install apache we’ve to consider two options that affect to our server performance and how apache will work:

Worker VS Prefork

Worker

With this MPM we can improve our server performance using less memory. Apache will running a single process and then will run multiple threads
to serve the requests. We can limit the number of server threads with the apache directive ThreadsPerChild.

Advantatges:

  • Server can manage more clients with less resources.

Disadvantages:

  • One process failure will affect all the threads associate with this.
  • Not compatible of the installation of php as a module (mod_php), It’s require install fastcgi (running PHP in an independent process).
  • Require more configuration.

Performance directive for worker module:

  • MinSpareThreads: The minimum number of threads to spare. With this value Apache will save resources for a minimum number of possible requests, with this way new users shouldn’t wait to the server.
  • MaxSpareThreads: The maximum number of threads to spare.
  • ThreadsPerChild: The number of constant threads created by process child.
  • ThreadLimit: Sets the maximum number configured for ThreadsPerChild for the lifetime of apache process.

Common configuration for a medium based web server with 2/3 GB of memory:

<ifModule worker.c>
StartServers 4
MaxClients 320
MinSpareThreads 40
MaxSpareThreads 80
ThreadsPerChild 40
MaxRequestsPerChild 0
</ifModule>

Prefork

With prefork module exists a main process and for every request apache handle it with a new subprocess. The parent process starts as root user, and every child process will run with User and Group apache directive.
This creates an isolation for every request, so a problem with a single requst don’t affect the rest.

Advantages:

  • Server don’t require special configuration.
  • Compatible with mod_php.
  • Isolation of problems in a request.
  • Compatibility with non thread-safe capability.

Disadvantages:

  • More consume of CPU / memory.
  • Less scalability.

Performance directive for prefork module:

  • MinSpareServers: The minimum number of idle child processes.
  • MaxSpareServers: The maximum number of idle child processes.
  • MaxRequestPerChild: Sets the limit of request that individual child server process will handle. After this limit, the process will die, this prevents from leaks memory.

Common configuration for a medium based web server with 2/3 GB of memory:

<ifModule prefork.c>
StartServers 4
MinSpareServers 25
MaxSpareServers 50
ServerLimit 270
MaxClients 270
MaxRequestsPerChild 10000
</ifModule>

Apache main configuration

  • MaxClients: The maximum number of requests that can be serve at the same time. All the request above this number will be queued up to a number based on ListenBacklog directive.
    The optimal number can be different depending the number memory for our server, We can calculate with:
    [(Total RAM – Space allocated in memory by other processes) / size of every apache process or threads]
    To check the size for each apache process you can run:

    # ps -ylC httpd --sort rss
  • ServerLimit: For the prefork module sets the maximum value for MaxClients for the lifetime of apache process. With worker module combine it with the ThreadLimit directive. Minimum this value must be the same that MaxClients, else the changes made in this directive doesn’t take effect.
  • TimeOut: The time of Apache will wait for three things:
    1- The total time to receives a GET request.
    2- The amount of time between TCP packets on a POST or PUT request.
    3- The amount of time between ACK’s for TCP packets responses.A good value could be 30 seconds before close a connection, more time can occupy unnecessary resources.

    TimeOut 30
  • KeepAlive: Provide response multiple requests over the same TCP connection.
    KeepAlive On
  • MaxKeepAliveRequests: limits the number of requests allowed per connection. You can calculate it depending the average the number of request can do a visitor, the number can be considerable but don’t set very high, one attack can crash the server in the same session.
    MaxKeepAliveRequests 70
  • KeepAliveTimeout: The number of seconds Apache will wait for a subrequest before close the connection. Once the request has received, the timeout value will be the value specified by TimeOut directive.
    KeepAliveTimeout 2
  • Disabling the execution of .htacess file, prevents to apache checks if there are a .htaccess in every level of a given path.
    <directory />
    AllowOverride None
    </directory>
  • Enabling symbolic links:
    <directory />
    Options FollowSymLinks
    </directory>
  • Configure in each directory will prevent apache to check if a directory or file is a symbolic link and if match the owner of this.
    <directory /document/path>
    Options +FollowSymLinks -SymLinksIfOwnerMatch
    </directory>

For more information you can see apache module documentation: http://httpd.apache.org/docs/2.2/mod/#core

Apache performance tuning
Tagged on:                 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow

Get every new post delivered to your Inbox

Join other followers: