Apache

From Just another day in the life of a linux sysadmin
Jump to: navigation, search

Load
ModSecurity
EasyApache
SSL
Robots
Domlog Diving
Apache Handlers

Head of domlog

head -n 1 /usr/local/apache/logs/domlogs/$DOMAIN.LOG | awk '{print $4}'

Basic starting point with Event

Per MJung on shared server

The settings I almost always use are:


KeepAlive               On
KeepAliveTimeout        2
MaxKeepAliveRequests    500
<IfModule event.c>
ThreadsPerChild         25
ServerLimit             16
MaxRequestWorkers       400
StartServers            6
MinSpareThreads         150
MaxSpareThreads         400
MaxRequestsPerChild     0
</IfModule>

Moving Apache logs

mkdir /home/domlogs
rsync -avHP /usr/local/apache/domlogs/ /home/domlogs/
After the data is copied, stop apache, and do a final sync of the logs. It is then safe to move the orignal directory to the side, create the symlink, and restart apache.
service httpd stop
rsync -avHP /usr/local/apache/domlogs/ /home/domlogs/
mv /usr/local/apache/domlogs{,.bak}
ln -s /home/domlogs /usr/local/apache/domlogs
service httpd start

Apache Modules

Apache is modular by design.

Most modules are prefixed with mod_.

to list modules, it's httpd -M.

in cPanel, most modules can be installed via EasyApache. Other modules require apxs command (see mod_evasive wiki) [19]


suexec module is a level of security for user protection. If it is set off, it'll throw 500 errors.

Mod_deflate settings

<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript </IfModule>

<IfModule mod_mime.c>

  1. Text
AddType text/css .css 

AddType application/x-javascript .js AddType text/html .html .htm A ddType text/richtext .rtf .rtx AddType text/plain .txt AddType text/xml .xml

  1. Image

AddType image/gif .gif AddType image/x-icon .ico AddType image/jpeg .jpg .jpeg .jpe AddType image/png .png AddType image/svg+xml .svg .svgz

  1. Video

AddType video/asf .asf .asx .wax .wmv .wmx AddType video/avi .avi AddType video/quicktime .mov .qt AddType video/mp4 .mp4 .m4v AddType video/mpeg .mpeg .mpg .mpe

  1. PDF

AddType application/pdf .pdf

  1. Flash AddType application/x-shockwave-flash .swf
  1. Font

AddType application/x-font-ttf .ttf .ttc AddType application/vnd.ms-fontobject .eot AddType application/x-font-otf .otf

  1. Audio

AddType audio/mpeg .mp3 .m4a AddType audio/ogg .ogg AddType audio/wav .wav AddType audio/wma .wma

  1. Zip/Tar

AddType application/x-tar .tar AddType application/x-gzip .gz .gzip AddType application/zip .zip

</IfModule>

<IfModule mod_expires.c> ExpiresActive On

  1. Text

ExpiresByType text/css A31536000 ExpiresByType application/x-javascript A31536000 ExpiresByType text/html A3600 ExpiresByType text/richtext A3600 ExpiresByType text/plain A3600 ExpiresByType text/xml A3600

  1. Image

ExpiresByType image/gif A31536000 ExpiresByType image/x-icon A31536000 ExpiresByType image/jpeg A31536000 ExpiresByType image/png A31536000 ExpiresByType image/svg+xml A31536000

  1. Video

ExpiresByType video/asf A31536000 ExpiresByType video/avi A31536000 ExpiresByType video/quicktime A31536000 ExpiresByType video/mp4 A31536000 ExpiresByType video/mpeg A31536000

  1. PDF

ExpiresByType application/pdf A31536000

  1. Flash

ExpiresByType application/x-shockwave-flash A31536000

  1. Font

ExpiresByType application/x-font-ttf A31536000 ExpiresByType application/vnd.ms-fontobject A31536000 ExpiresByType application/x-font-otf A31536000

  1. Audio

ExpiresByType audio/mpeg A31536000 ExpiresByType audio/ogg A31536000 ExpiresByType audio/wav A31536000 ExpiresByType audio/wma A31536000

  1. Zip/Tar

ExpiresByType application/x-tar A31536000 ExpiresByType application/x-gzip A31536000 ExpiresByType application/zip A31536000

  1. Webfonts

ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType application/x-font-woff "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

<FilesMatch "\.(?i:css|js|htm|html|rtf|rtx|txt|xml|gif|ico|jpg|jpeg|jpe|png|svg|svgz|asf|asx|wax|wmv|wmx|avi|mov|qt|mp4|m4v|mpeg|mpg|mpe|pdf|swf|ttf|ttc|eot|otf|mp3|m4a|ogg|wav|wma|tar|gz|gzip|zip)$"> <IfModule mod_headers.c> Header set Cache-Control "public, must-revalidate, proxy-revalidate" Header unset ETag </IfModule> </FilesMatch>


Apache Optimization

Starting points for optimization are listed in the wiki.

Make sure you watch changes after making them, to be sure everything is functioning correctly.

2.0, 2.2, 2.4 are supported. 1.3 is still in use, but deprecated.

Timeout = seconds before timeouts

KeepAlive = on/off switch, will keep handshakes alive up to the timeout limit... MaxKeepAliveRequests/KeepAliveTimeout. KeepAlive should generally be on, especially for messageboards like vBulletin.

MaxClients aka MaxRequestWorkers = How many requests can apache service at any given instant). Hard coded at 10,000.

ServerLimit needs to match MaxClients.

MaxRequestsPerChild = "Let's say this is 300. Each Apache process can serve 300 requests before the daemon kills it off and starts a new one."

StartServers = How many daemons get spawned at startup?

RLimitMEM, RLimitCPU, RLimitNPROC = set mem/cpu/nproc, often causes crashes or headache, so can comment them out... cPanel sets them arbitrary, so just comment them out if issues are strange.

Min/MaxSpareServers = Start server should be between these. If processes is above max, it kills them off at 1 per second.


Timeout

Timeout <integer> (Default 300)

Timeout, in seconds, controls how much time Apache will wait for the follow three conditions:

  1. The total amount of time it takes to receive a GET request
  2. The amount of time between receipt of TCP packets on a POST or PUT request
  3. The amount of time between ACKs on transmissions of TCP packets in responses.

On dedicated hardware we leave this value at 300 and it is set to 100 on VPSes. This value is pretty safe to be left alone in most cases. If a server is getting slammed by traffic, lowering Timeout may help mitigate this by decreasing the amount of time that apache will hold open a process waiting for a slow response. It may need to be increased in the odd case that code doesn't reset the timer when one of these conditions occurs.

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

KeepAlive

KeepAlive <on|off> (Default on)

KeepAlive controls whether a request is "Kept Alive" or not when it serves a request to a client. With KeepAlive on, Apache requests are kept alive so that one connection is used to serve multiple requests. With KeepAlive off, once a request is served, apache will close the connection and the client will need to open a new request. A KeepAlive connection will count as only one request for MaxRequestsPerChild, regardless of how many requests are actually sent.

KeepAlive is a tricky directive. The default is on, and for the most part, this will serve well for most servers. If a server is experiencing a lot of traffic and Apache is having trouble keeping up, turning KeepAlive off can help. The cavaet to this is if the high traffic site is either dynamic content or each page has to load a large number of individual files, such as with a forum, turning KeepAlive off is a bad idea, as it will open a new connection to load each individual file.

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

MaxKeepAliveRequest

MaxKeepAliveRequests <integer> (Default 100)

MaxKeepAliveRequests limits the number of requests that are allowed per connection when KeepAlive is on. If this is set to 0, unlimited requests will be allowed. We leave this value to 100 on VPSes, and up it to 150 on dedicated hardware. Once a connection has hit the MaxKeepAliveRequests, it will be closed. If KeepAlive is off, this value will not be read.

If a server can handle it, this value can be safely increased. If the server is having connection issues, dropping it is an option, but it is probably a better idea to just turn KeepAlive off.

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

KeepAliveTimeout

KeepAliveTimeout <integer> (Apache Default 5, Cpanel Default 15)

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. This is because server processes will be occupied waiting on connections with idle clients. We set this value to 5 on both Dedicated hardware and VPSes. If KeepAlive is off, this value will not be read.

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

MaxClients

MaxClients <integer> (Apache Prefork Default 256, Apache Worker Default ServerLimit X ThreadsPerChild, Cpanel Default 150)

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. Once an open slot is available, Apache will pick up the queued requests. Worker's MaxClients must be equal to no more than ServerLimit X ThreadsPerChild.

On dedicated hardware running prefork, we initially set MaxClients to 250. On VPSs and dedicated hardware running Worker, we set MaxClients to 150. 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. The MaxClient value can only be set in the server config.

ServerLimit

ServerLimit <integer> (Apache Prefork Default 256, Apache Worker Default 16)

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 not need to be set, unless it is required to increase MaxClients above the server's hard limit. Please note that there is a hard coded limit on ServerLimit, 20,000 for worker and 200,000 for prefork. This is intended to avoid nastyness caused by typos.

This value is set in the server config.

MaxRequestsPerChild

MaxRequestsPerChild <integer> (Apache Default 10000, Cpanel Default 0)

MaxRequestsPerChild sets a limit on the number of requests each individual Apache process will handle. Once this value is reached, the child process is stopped. A value of 0 will prevent processes from expiring. We usually set this value to 150% of MaxClients, to prevent accidental memory leaks. 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.

This value is set in the server config.

StartServers

StartServers <integer> (prefork default 5, worker default 3)

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.

This value is set in the server config.

RLimitCPU

RLimitCPU <min> | <max> [set in seconds] (Not set by default)

RLimitCPU determines the CPU limit for all child processes. 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". This applies only to processes forked off from Apache child processes, such as PHP or CGI processes, not the Apache children themselves. This value is expressed in seconds per process.

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.

Do not set this value on a VPS.

RLimitMEM

RLimitMEM <min> | <max> [set in bytes] (Not set by default)

RLimitMEM determines the memory limit for all child processes. 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". This applies to processes forked off from Apache child processes, such as PHP or CGI processes, not the Apache children themselves. This value is expressed in bytes per process.

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.

Do not set this value on a VPS.

Directives common to Apache 1.3 and Prefork

Below are values that are common both Apache 1.3 and Prefork builds of Apache2. Having these values outside of a prefork include in the httpd.conf with worker installed will cause apache to error on a restart and prevent apache from starting.

MinSpareServers

MinSpareServers <Integer> (Default 5)

MinSpareServers dictates what the minimal number of idle child server processes will be running at any given time. If the number of idle servers is less than this number, Apache will start them at a rate of 1 per second. This value should always be tweaked with available server resources in mind. If a server is experiencing memory shortages, this value can be safely dropped. A good rule of thumb is that this setting and StartServer should be the same.

This value is set in the server config.

MaxSpareServers

MaxSpareServers <Integer> (Default 10)

MaxSpareServers dictates what the maximum number of idle child server processes will be running at any given time. If the number of idle servers is more than MaxSpareServers, Apache will just kill the processes off. If this value is less MinSpareServer, Apache will automagically set MaxSpareServer to MinSpareServer +1. Like with MinSpareServer, this value should always be tweaked with available server resources in mind. For lower values of MinSpareServer, doubling that to get MaxSpareServer is generally a good rule of thumb.

This value is set in the server config.

Directives common to Worker, Event and Leader

The following directives are common to the Worker MPM and it's derivatives, Event and Leader. Having these directives active in the server config outside of an IfModule statement with prefork compiled, will cause apache to error out and refuse to start. Even though Worker, Event and Leader all use the same directives, they will not read directives inside another's statement. For example: Event will not read directives in a Worker IfModule include.

ThreadsPerChild

ThreadsPerChild <Integer> (Default 25)

ThreadsPerChild dictates how many threads each child process will create when the process is spawned. This value should be high enough to handle the server's load. The value of this, along with the value of ServerLimit determine what maximum MaxClients is. Generally this value doesn't need to be changed.

This value is set in the server config.

ThreadLimit

ThreadLimit <integer> (Default 64)

ThreadLimit determines what the maximum value ThreadPerChild can be. A restart will not re-read this directive, you will have to stop and start apache. Please take care when changing this directive. If ThreadLimit is too high compared to ThreadsPerChild, extra unused shared memory will be allocated to Apache. If ThreadLimit and ThreadsPerChild are set too high, it could make Apache and the server unstable. This value should be left at its default unless ThreadsPerChild is raised. There is a hard limit of 20,000 on this

This value is set in the server config.

MinSpareThreads

MinSpareThreads <integer> (default 75)

MinSpareThreads determines how many idle threads are present on the server at a given moment. If the number of idle threads is less than MinSpareThreads, Apache will create child processes until enough idle threads are present. This value should be set depending on traffic to the server and the type and size of CPU, as well as the amount of memory the server has. We generally set this value to 25.

This value is set in the server config.

MaxSpareThreads

MaxSpareThreads <integer> (default 250)

MaxSpareThreads sets the upper limit on how many idle workers may be present at a given time. If current idle is higher than MaxSpareThreads, apache will kill off child processes until the number of idle servers is less than this number. Apache will automatically increase MaxSpareThreads if it is not equal to or greater than the sum of MinSpareThreads and ThreadsPerChild. Like with MinSpareThreads, this value should be set depending on traffic to the server and the type and size of CPU, as well as the amount of memory the server has.. We generally set this value to 75.

This value is set in the server config.

Optimizing Apache

Worker vs Prefork

The Worker vs Prefork debate is one of the many debates that can come about when working with Apache. Which is right for you, and which should be used when. Prefork is the default MPM for Apache, and it works well for most of our customers. If the customer has an Apache module installed that does not work with threading, they will have to use Prefork.

Prefork does have its limitations, however. It uses more resources than worker, and as such it does not scale well. Servers running with mpm_prefork that have high traffic or servers that are having repeat Apache related memory problems should consider mpm_worker. Alternatively, mpm_worker shouldn't be installed if the customer has 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 to what the customer needs.

Worker should also not be used if the customer needs to use software that is not thread-safe. The DSO handler for PHP is not threadsafe, and cannot be used with Worker. If they want to use Worker with PHP, they'll need to use either SuPHP or one of the CGI handlers.

As a rule, the other experiment MPMs should be avoided. Since changing MPMs requires a recompile, always clear the recompile with the customer beforehand.

Apache is running out of connections

Template:Warning

Apache does have limits to how much traffic it can handle, but fortunately there are things we can tweak in the httpd.conf to allow Apache to handle more requests. If a server is getting legitimate traffic, ie. not getting DOS'd, these tips could help Apache keep up with the amount of traffic the server is receiving. Try one suggestion at a time and observe how the server reacts over a 5 to 10 minute span before trying something else.

Template:Box Warning

  • Raising the MaxClients - If the Apache error_log specifically mentions that Apache is hitting its Max Connections, raising MaxClients is always an option. Check and double check the bandwidth graphs to ensure there hasn't been a sudden inbound traffic spike. Raise this in increments of 50, being aware that increasing this value too high could cause other problems. The specific warning in the Apache error_log will be as follows:
WARNING: MaxClients of 500 exceeds ServerLimit value of 256 servers, lowering MaxClients to 256. To increase, please see the ServerLimit directive.


Note: raising MaxClients may require a raise in ServerLimit and ThreadLimit in worker. Apache will give an error on restart if this is the case.The ServerLimit and ThreadLimit directives will need to be above the MaxClients directive.

  • Turning KeepAlive off - If the server has fairly static content, such as a cached wordpress site, turning KeepAlive off will often help. Servers with dynamic content, such as forums do not benefit from KeepAlive off. If one site is having trouble, trying turning KeepAlive off for the one domain in that domain's vhost.
  • Dropping the KeepAliveTimeout - If the server didn't respond to turning KeepAlive off, dropping the KeepAliveTimeout will help Apache refresh its connections faster. If one site on the server is getting more traffic than others, try setting this value in the domain's vhost.

Apache is having memory issues

Next to MySQL, Apache is generally the most resource heavy service a server will run. If Apache is causing the server to go OOM, and PHP has sane memory usage directives (set in the server's php.ini), below are some tricks that can be tried. Please note that if the server is having heavy traffic and memory issues, upgrading is generally the best (and sometimes only) option and anything here would be considered a bandaid. Also, if the server in question is a VPS, they will probably have to upgrade.

  • Drop MaxRequestsPerChild and KeepAliveMaxRequests - The more requests an Apache process serves, the more memory that process will use. Dropping both MaxRequestsPerChild and KeepAliveMaxRequests (if KeepAlive is on) will force Apache to kill off old processes and spawn new processes faster. MaxRequestsPerChild is specifically designed to combat memory leaks. Do not set either of these values to 0, as this will have the direct opposite result, allowing each process to server an unlimited number of requests.
  • Drop MinSpareServers and MaxSpareServers - While the amount of resources the idle servers use is generally negilible, if these values are high, dropping these values could have a profound effect. Note: this will only help if the server is running Prefork.
  • Drop MinSpareThreads and MaxSpareThreads - Like the above, but on a server with Worker installed
  • If ServerLimit is set, make sure it is not drastically higher than MaxClients - If ServerLimit is higher MaxClients, Apache will be allocated extra shared memory that will go unused. Dropping ServerLimit to be in line with MaxClients (or MaxClients divided by ThreadsPerChild in Worker's case) will help prevent a memory leak.
  • If ThreadLimit is set, make sure it is not drastically higher than ThreadsPerChild - Just like with ServerLimit, if ThreadLimit is higher than ThreadsPerChild, Apache will be allocated extra shared memory that will go unused. Making sure that ThreadLimit and ThreadsPerChild are in line will help eliminate a potential memory leak.
  • Comment out RLimitMEM and RLimitCPU - A Cpanel script adds these to the httpd.conf, and it usually does a poor job of determining values for these directives (commonly setting them to the exact amount of RAM and CPU in the server). Commenting these values out may help with the situation.
  • Recompiling Apache with Worker - Since worker generally uses less RAM than prefork, compiling Worker into Apache could have a positive effect, especially on servers with lots of traffic.

Starting Points

Please consult the account admin comments, and/or your friendly monitoring person on duty if there is any question whether a server's httpd.conf has or has not been optimized.
IF YOU MAKE ANY CHANGES TO THE .CONF FILE YOU MUST DISTIL & rebuild:  
/usr/local/cpanel/bin/apache_conf_distiller --update
/scripts/rebuildhttpdconf
/etc/init.d/httpd restart 

Edit /usr/local/apache/conf/httpd.conf and you will see these as the default settings on any CPanel fresh install of either Apache 1.3 or Apache2 with Prefork.

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
MinSpareServers 5
MaxSpareServers 10
StartServers 5
MaxClients 150
MaxRequestsPerChild 0

Template:Warning

These are general guidelines. For any server with 1GB RAM or less, you may want to start with the VPS specs above, and then increase slowly (a day or 2 later) if its handling it. Increase values and turn KeepAlive On/Off as necessary.

Template:Box Note

Also, to ensure that you are optimizing correctly, please run the following command to determine what MPM is currently installed.

/usr/local/apache/bin/httpd -V | grep mpm

On Core-Managed and Plesk boxes, the command is as follows:

/usr/sbin/httpd -V | grep mpm

Output of the command will look like this

root@plastic [~]# /usr/local/apache/bin/httpd -V | grep mpm
-D APACHE_MPM_DIR="server/mpm/worker"

Apache 1.3 will not return any value for this command.

Template:Box Note

A Quick Note about IfModule stats

With Apache2 servers, you will often see the optimization directives in an IfModule statement, like the ones below:

<IfModule prefork.c>
    StartServers 10
    MinSpareServers 10
    MaxSpareServers 20
    MaxClients 250
    MaxRequestsPerChild 500
</IfModule>
<IfModule worker.c>
    ServerLimit 16
    StartServers 2
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 150
    MaxRequestsPerChild 500
</IfModule>

These IfModule statements are not needed by Apache in order for the service to process the optimization values. The IfModule states are there so you can have both Prefork and Worker optimizations in the same Configuration file at the same time, which is the case with some non-CPanel installs of Apache. Putting the Apache optimizations in a IfModule statement or not is completely up to your discretion.

Template:Box Note

IF YOU MAKE ANY CHANGES TO THE .CONF FILE YOU MUST DISTIL and Rebuild:

/usr/local/cpanel/bin/apache_conf_distiller --update
/scripts/rebuildhttpdconf
/etc/init.d/httpd restart

cPanel overwriting MPM Worker optimizations

Sometimes cPanel will overwrite Worker specific settings when being distilled/recreated. One way to get around this is to include the work specific options to one of the include files, such as post_virtualhosts_global.conf

 /usr/local/apache/conf/includes/post_virtualhosts_global.conf

Since you will be putting options into a non-specific location, notes will be required on both the ticket and billing, to help prevent future confusion. Also, due to the potential confusion this could cause, it is highly recommended that you do not use this include for the configuration options except for a last resort.

For VPS, Vservers, and very old hardware

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


For Storm and Dedicated Servers with between 1G and 2G of RAM

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

For Storm and Dedicated Servers with between 2G and 8G of RAM

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
    StartServers 20
    MinSpareServers 20
    MaxSpareServers 40
    ServerLimit 350
    MaxClients 350
    MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
    ServerLimit 14
    StartServers 4
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 350
    MaxRequestsPerChild 1000
</IfModule>
Timeout 300

For Storm and Dedicated Server with between 8G and 16G of RAM

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
    StartServers 20
    MinSpareServers 20
    MaxSpareServers 40
    ServerLimit 400
    MaxClients 400
    MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
    ServerLimit 16
    StartServers 5
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 400
    MaxRequestsPerChild 1000
</IfModule>
Timeout 300

For Storm and Dedicated Server with between 16G and 32G of RAM

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
    StartServers 20
    MinSpareServers 20
    MaxSpareServers 40
    ServerLimit 450
    MaxClients 450
    MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
    ServerLimit 18
    StartServers 8
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 450
    MaxRequestsPerChild 1000
</IfModule>
Timeout 300

For Storm and Dedicated Server with between 32G and 48G of RAM

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
    StartServers 20
    MinSpareServers 20
    MaxSpareServers 40
    ServerLimit 550
    MaxClients 550
    MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
    ServerLimit 22
    StartServers 10
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 550
    MaxRequestsPerChild 1000
</IfModule>
Timeout 300

For Storm and Dedicated Server with more than 48G of RAM

KeepAlive On
MaxKeepAliveRequests 150
KeepAliveTimeout 5
<IfModule prefork.c>
    StartServers 20
    MinSpareServers 20
    MaxSpareServers 40
    ServerLimit 750
    MaxClients 750
    MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
    ServerLimit 30
    StartServers 15
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadsPerChild 25
    MaxClients 750
    MaxRequestsPerChild 1000
</IfModule>
Timeout 300

One thing to note: ServerLimit is a hard limit on the number of active child processes, and must be greater than or equal to the MaxClients directive divided by the ThreadsPerChild directive. So, if you put MaxClients 500, and have ThreadsPerChild 25, ServerLimit must be set to 20 or greater. If its less, Apache will take ServerLimit x ThreadsPerChild and that will be what MaxClients is set to at runtime (in this example it would end up being 400). However, its OK to set MaxClients lower than the value of ServerLimit x ThreadsPerChild, similar to below:

ServerLimit = MaxClients / ThreadsPerChild

MaxClients = ServerLimit x ThreadsPerChild

Template:Warning

PHP Opcode Caching

Opcode cachers are great for speeding up PHP based sites. Memory is loaded into bytecode form and shared between threads and processes. It can give a good reduction in load and CPU usage to a site that isn't using one. By their nature, they will only work in an environment running some kind of persistent PHP instance such as DSO (with Prefork recommended) or FCGI (with Worker recommended). None will work effectively with suPHP due to the shared memory limitations of suPHP. Additionally, none directly require an EasyApache in order to be installed.

eAccelerator

https://wiki.int.liquidweb.com/articles/EAccelerator

Xcache

https://wiki.int.liquidweb.com/articles/XCache

APC (Alternative PHP Cache)

https://wiki.int.liquidweb.com/articles/APC



Troubleshooting Tips

Prefork is old, but stable/reliable.

Event and Worker can only run thread safe stuff. suPHP works with any MPM.

Prefork is the only thing that works with mod_php. prefork does most work in RAM, but event/worker use cores.

Event is faster than worker, but relatively new.

"Apache is running out of connections": First thing is to make sure the box isn't being abused. Check DOS Procedures. If no DOS, then try increasing max clients. Be careful, and increase it in increments; do it by 25-50 then see how it goes. If you increase it too much, it can use too much memory usage, causing the server to drive. Also, dropping the KeepAliveTimeout to like 1 second is a cool trick, as it cycles connections pretty quickly.

"Apache is having memory issues": Make sure it isn't php/ruby/etc. Check plugins and make sure code is okay.

All CMS have the ability to cache. Make sure people are caching.