Print
Parent Category: Tutorials
Hits: 43686

Today we will talk about Linux and apache tips and tricks that will help webmaster to tune their dedicated server to handle high traffic.

Apache configuration
httpd.conf is a file file, usually located at /usr/local/apache/conf/httpd.conf path containing configuration settings for apache server. Cannot find the location of the file? Use locate command: >cd / then >locate httpd.conf. You may find the values in conf/extra directory. Search for http-mpm.conf and http-default.conf
Main parameters to tune are:
In Apache MPM Prefork Module: 
  1. StartServers
  2. MinSpareServers
  3. MaxSpareServers
  4. ServerLimit
  5. MaxClients
  6. MaxRequestsPerChild

 

KeepAlive options:

 

  1. KeepAlive
  2. KeepAliveTimeout
  3. MaxKeepAliveRequests

Timeout options:

1. Timeout

 

 

Apache MPM Prefork Module

Another handy tweak is to make some adjustments to the Apache MPM prefork module. This is assuming you are using Apache in prefork mode, which is highly recommended and likely if you are on a small VPS.

This module controls the number of processes and spare processes Apache will start and run. This is especially important if you are running a small VPS that is handling MySQL and Apache. Unless you are getting slammed with really heavy traffic on a regular basis (in which case you should be on a dedicated server) there is no need to be running the default configuration. Find these lines in your httpd.conf file:
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>

MinSpareservers and MaxSpareServers control the number of spare processes your webserver is permitted to run and StartServers controls how many are started by default.

ServerLimit - controls the maximum configured value for MaxClients.
MaxClients – sets the limit on the number of simultaneous requests that can be supported.
Reducing MaxClients on a webserver that is serving dynamic content (e.g. WordPress) can make a big difference. If you experience a traffic spike on your VPS and your MaxClients is set too high your server will more than likely get stuck in an endless loop of swapping pages from physical memory to virtual memory, commonly referred to as thrashing. The accepted way of calculating an appropriate MaxClients value is dividing your total available system memory by the size per Apache process. For example, if you had a 500MB left for Apache to use and each Apache process was using around 20MB you would set your MaxClients to (512-12) / 10 = 50. To check real time memory usage on your VPS use top.Want to set it to a larger value? Add more physical RAM.  In other words, calculate the average of your httpd process, divide total available memory by the average leaving some for the system. E.g. in case of 1 Gig RAM and average httpd process size 7MB on this server it is safe to set it to 100.
If you use MySQL database, MaxClients parameter should be equal or close to max_connections parameter in MySQL conf file (my.cnf)


MaxRequestsPerChild -  limits the number of requests a child server will handle during it’s life. We can safely reduce this value and realize a small gain.

So let’s go ahead and pare down those values:
<IfModule prefork.c>
StartServers 3
MinSpareServers 3
MaxSpareServers 10
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 2000
</IfModule>

Remember these are not concrete “best” values, they depend on the size of your VPS and how small or large you Apache process is.
Optimize Your KeepAlive

KeepAlive allows your visitors to issue multiple requests over the same TCP connection, in theory this helps improve latency because your visitors can request your webpage, images, and javascripts all over one connection. Unfortunately, Apache must use a worker process to service each and every request. The worker process stays busy servicing each request for a full 15 seconds by default, even if your visitor is no longer using it! This means you have less worker processes available on your system at any given time. With the limited system resources you have on your small VPS we always want open worker processes to be actually working. One way of accomplishing this is turning off KeepAlive. Find this line in your httpd.conf file:
KeepAlive On

and change it to:
KeepAlive Off

If you have a site with lots of images and javascripts it is usually better leave KeepAlive turned on and make some additional tweaks.

If you decide to leave KeepAlive turned on it is important you change the default KeepAliveTimeout value. This prevents unused connections from staying open for excessive amounts of time. Find this line in your httpd.conf file:
KeepAliveTimeout 15

You want to leave this connection open for 2 seconds, just long enough for the client to request most if not all of the necessary files. It is best to se it to minimum, 1-3 seconds. So change that line to:
KeepAliveTimeout 2

If you are going to leave KeepAlive on you will want to increase MaxKeepAliveRequests. Setting this higher allows more requests per connection and increases efficiency. Find this line:
MaxKeepAliveRequests 100

and change it to:
MaxKeepAliveRequests 200

MaxRequestsPerChild – sets how many requests to serve per new httpd child process. You may set it very low, thus constantly freeing the memory, however on a particular case values like 15 or 20 may work well. As an example our site showing 10 images per page has this parameter set to 15.
MinSpareServers
MaxSpareServers
StartServers
Adjust Timeout

Another minor tweak that will give you a small performance boost as well as help reduce the effects of a DOS attack is changing the TimeOut Directive. This directive tells Apache how many seconds to wait while receiving an incoming request, processing it, and sending back a response. Find this line:
Timeout 120

and change it to:
Timeout 40
After you change settings in httpd.conf do not forget to restart apache. You may do this from control panel or from command line > service httpd restart
Also modules are important. For example mod_expires.c. How to check if modules are enabled:
httpd -l

Note:

We use Hosting and VPS Hosting, from: www.star-host.org

We like and trust them.

Good prices, high security.