Apache Configuration and Performance Tweaks [part 1]

May 24, 2010 in tech,work | Comments (2)

Note: This is part 1 in a series. Currently, I think there are likely to be three parts (configuration explained, performance tweaks, and troubleshooting), but we will have to see as I write them. I really expected this to only be a single entry.

First and foremost, I have to give credit to Matt Aurand. He took the time to read the Apache docs, and distill them into something I actually read, comprehended, and can now spit back out to you. There are some large portions of this post copied directly from what he wrote, because I could not come up with a better way to say it.  The other huge resource is the Apache Docs (1). Huge, and dry. I tried to make it less dry, and more condensed.


My background with Apache is almost entirely in LAMP servers that have cPanel/WHM installed. While I have attempted to remove any and all assumptions based on that configuration, I’m sure I’ve missed some. Feel free to point them out, if you find them.


Apache is the most common and most popular web server out there(2). It’s free, stable, and easy to support. While it does work in a sort of out-of-the-box way, there are lots of things you can do to make your server (and the apache service) more stable.

What we’ll cover in this post is all contained here. This is a copy/paste of my own VPS’s apache directives.

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
StartServers 10
MinSpareServers 10
MaxSpareServers 20
MaxClients 150
MaxRequestsPerChild 500
</IfModule>
<IfModule worker.c>
ServerLimit 5
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 500
</IfModule>

Timeout 300

MPMs:

The two most common MPMs (Multi-process modules) used with apache are mpm-prefork and mpm-worker. They’re both stable, and easy to use, but are good for very different things. Briefly: Prefork is non-threaded, and runs a lot like apache 1.3. Worker combines threading and non-threading and is extremely powerful.

According to the Apache docs, Prefork is:

This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

Worker‘s long description:

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

Follow the links for a complete description. For the remainder of this post, I will assume that you have a base understanding of Apache 2.0 and the two above MPM’s (hereafter called Prefork and Worker).

Which MPM should you use?

Apache comes by default with Prefork. If you’re using any modules that do not work correctly threaded, you should stick with prefork. Prefork is more stable, and built directly on known technology (Apache 1.3).

Prefork, however, also uses more resources on a normal server, and therefore does not always gracefully handle spikes or growth. Servers seeing bottlenecks at CPU or Memory should consider switching to Worker.

Worker shouldn’t be installed, though, if you have a compelling reason for each Apache request to be isolated. Since Worker is threaded, each child process handles multiple requests and may not be appropriate for what you need. It should be noted; a huge drawback to Worker is that if PHP has been compiled and installed to be used by Apache as a DSO, Worker will not work.

Directives

Each Module has 20+ directives that can be defined to alter the performance of apache. For the sake of brevity, I will give a brief description of each as we arrive at it, and link to where you can read more about it.

Common to both MPMs

For the full list, you can head over to the apache docs. I’ll just cover some of the most common.

Timeout $integer (Default 300) Apache#Timeout

Timeout can be set in the Server Config, or in individual vhosts.

Timeout, in seconds, controls how much time Apache will wait for the follow three conditions:
The total ammount of time it takes to recieve 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.

KeepAlive $(on|off) (Default on) Apache#KeepAlive

KeepAlive can be set in the Server Config and in individual vhosts.

The KeepAlive directive decides whether or not the connection is kept open to that client for further requests, after it serves its request. On mean yes, Off means no. “In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images. To enable Keep-Alive connections, set KeepAlive On.”

MaxKeepAliveRequests $integer (Default 100) Apache#MaxKeepAliveRequests

MaxKeepAliveRequests can be set in the Server Config or in individual vhosts.

MaxKeepAliveRequests limits the number of requests that are allowed per connection when KeepAlive is on. 0 means unlimited. A good rule of thumb in a shared environment is 150% of your MaxClients. If your Site is static, stable, and on its own servers, this number can be safely increased to 1000 or more.

KeepAliveTimeout $integer (Apache Default 5, Cpanel Default 15) Apache#KeepAliveTimeout

This value can be set in the server config and in individual vhosts.

KeepAliveTimeout, in seconds, controls how long Apache will wait for a subsequent request from a specific client before closing the connection to that client. Having this value too high will cause performance problems on high traffic servers, because server processes will be occupied waiting on connections with idle clients. If KeepAlive is off, this value will not be read.

MaxClients $integer (Apache Prefork Default 256, Apache Worker Default ServerLimit X ThreadsPerChild, Cpanel Default 150) Apache#MaxClients

MaxClients can only be set in the server config.

MaxClients sets the limit on the simultaneous requests that will be served. Connections over this value will be queued, which is controlled by the ListenBacklog variable. Worker’s MaxClients must be equal to no more than ServerLimit X ThreadsPerChild.

MaxClients can be increased if a server is having trouble keeping up with legitimate traffic, and can vary greatly, depending on the type of content being served, the kind of server it is, and the amount of memory the server has. Increasing this value should be done cautiously, because if it is set too high you will see an increase in server instability, as each additional MaxClient causes apache to use more resources.

If you set prefork’s MaxClient above 256, the ServerLimit may also be also need to be set.

ServerLimit (Apache Prefork Default 256, Apache Worker Default 16) Apache#ServerLimit

This value is set in the server config.

With PreFork, ServerLimit sets the MaxClients for the lifetime of the Apache process. With worker, ServerLimit combined with ThreadLimit determines the server’s MaxClients. Please note that the ServerLimit cannot be changed via a restart. It requires that you stop and start apache. This Value does need only be set if you need to increase MaxClients above the server’s hard limit and has a hard-coded limit of 20,000 for worker and 200,000 for prefork. This is intended to avoid nastyness caused by typos.

MaxRequestsPerChild $interger (Apache Default 10000, Cpanel Default 0) Apache#MaxRequestsPerChild

This value is set in the server config.

MaxRequestsPerChild sets a limit on the number of requests each individual Apache process will handle before the child process is stopped. A value of 0 (infinity) will prevent any processes from expiring. If the content being served by Apache is stable (even if not static) this directive can be set quite high, to allow processes to serve more content.

StartServers $interger (prefork default 5, worker default 3) Apache#StartServers

StartServers is set in the server config.

StartServers controls how many child processes are spawned at startup. Please note that after start, this directive isn’t used. What value this should be set to is dependent on the type and size of CPU, as well as the amount of memory the server has. Due to the way apache works, setting this value lower than MinSpareServers or more than MaxSpareServers is counterproductive as it generally only puts extra load on the server itself.

RLimitCPU $interger|$interger (set in seconds) (Not set by default) Apache#RLimitCPU

RLimitCPU can be set in the server config, an individual domain’s vhost, a directory block or a .htaccess file in the domain’s docroot.

RLimitCPU determines the CPU limit for all *child* processes such as PHP or CGI processes, not the Apache children themselves. The first value is a soft limit and the second value is a hard limit. Either value can be set to a number or the word “max”. If you are on a VPS, it is strongly advised that you do not set this value, as it could cause significant problems for your host.

RLimitMEM $interger|$interger (set in seconds) (Not set by default) Apache#RLimitMEM

RLimitMEM can be set in the server config, an individual domain’s vhost, a directory block or a .htaccess file in the domain’s docroot.

RLimitMEM determines the MEM limit for all *child* processes such as PHP or CGI processes, not the Apache children themselves. The first value is a soft limit and the second value is a hard limit. Either value can be set to a number or the word “max”. If you are on a VPS, it is strongly advised that you do not set this value, as it could cause significant problems for your host.


If you’ve made it this far, congratulations. You now have a much more firm understanding of what Apache directive does, and how you can leverage it for your own server-worldly good.

Next week I’ll talk about the tricks I’ve picked up as a SysAdmin at a Web Host, and how they can help you.

__________________________________________________________________

References:

http://httpd.apache.org/docs/2.0/

2. http://news.netcraft.com/archives/2010/02/22/february_2010_web_server_survey.html


2 Responses to “Apache Configuration and Performance Tweaks [part 1]”

RSS feed for comments on this post. TrackBack URL

  1. Comment by Jim — January 19, 2011 at 7:26 pm   Reply

    I am on the unending journey to optimize my Apache settings, so I appreciate your post. One quick question about your settings, you have Server Limit set at 5 and MaxClients set at 150, but from what I understand, MaxClients cannot be more than ServerLimit. What am I missing?

    • Comment by benny Crampton — January 19, 2011 at 8:44 pm   Reply

      No problem at all!

      I think you’re referring to this section, right?


      ServerLimit 5
      StartServers 2
      MinSpareThreads 25
      MaxSpareThreads 75
      ThreadsPerChild 25
      MaxClients 150
      MaxRequestsPerChild 500

      If so, the “” means to indicate that those settings should be used in the event that you are using the WorkerMPM. In the case of the worker module, MaxClients should actually be equal to ServerLimit x ThreadsPerChild. Each “server” in this case, is a child Apache process, and the number defined by ThreadsPerChild indicates the number of threads that can be run in each “server”.

      I hope that helps! Let me know if you have more questions. :)

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>