Many webmasters wish they got a lot of traffic to their site. A day may come and their website may be very popular in just one day. This could be a reason that unique article is published on their site, or just a simple change in search position results in major search engines. So you get a lot of traffic, but is your server ready for this? Pages may take seconds to load and many visitors may think that the site is not available right now and go somewhere else.
Today we will talk about Linux and apache tips and tricks that will help webmaster to tune their dedicated server to handle high traffic.
How to see that you get a lot of traffic? Check log files, or check traffic rating at alexa.
How to see that your server is not capable of handling it? Use uptime command or top command. load average: 0.31, 0.46, 0.33 line is to search for. The first number shows current load (1 min average), second and third number show 5 and 15 min load average. If your 5-15 server load average is higher than 10 you have to worry about items discussed below.
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
Main parameters to tune are:
- MaxClients - sets the limit on the number of simultaneous requests that can be supported. Never set this number to high.
Setting MaxClients parameter to a big number will lead to consumption of all available memory on your server and a hard disk swapping may occur.
In other words the server will try to use hard disk space as additional memory, which is a very-very slow process. How to detect this?
Use top command. Typical output of a swap command is shown below. Swap “41852k used” and Mem “624148k used” are two lines to look at.
It memory size is approaching to total and swap used is growing then you are in trouble.
Check MaxClients parameter. How to find what is the maximum that I can set? 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. What to set it to a larger value? Add more physical RAM.
top - 01:04:08 up 4 days, 2:19, 1 user, load average: 0.31, 0.46, 0.33
Tasks: 129 total, 1 running, 128 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.0% us, 1.3% sy, 0.3% ni, 87.4% id, 5.6% wa, 0.3% hi, 0.0% si
Mem: 1027668k total, 624148k used, 403520k free, 10300k buffers
Swap: 2048276k total, 41852k used, 2006424k free, 446804k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15856 nobody 15 0 47532 7180 3532 S 3.3 0.7 0:00.17 httpd
16097 nobody 15 0 46736 6036 3212 S 1.3 0.6 0:00.06 httpd
- KeepAlive - allows multiple requests to be sent over the same TCP/IP connection. Turning it on can greatly improve the speed of your server, particularly when you have static pages and are serving quite a bit of images from your server. An example would be a catalogue site with screenshots. From my experience it is best to keep it On
- KeepAliveTimeout – sets the number of seconds that the child httpd process sits and takes memory. From my experience it is best to set it to minimum, 1-3 seconds.
- 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.
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