Fine tuning a Linux Apache MySQL PHP (LAMP) Server

Source: Internet
Author: User
Tags geoip apache log

I started to write this post before weeks ago and finally publish it even if it's not totally finish. it is just a little feedback about tuning a full lamp server with some user traffic and services load. important thing to notice is that all stuff in this post isNot the solution. You will probably have to tune little more for adapt all this to your personal server usage, server load, development & architecture. so, use those tips as a kind of inspiration instead of an "how ". don't forget that when you do such tuning, take care to keep a backup of your previous configuration files.

We will try to tune the following server:

  • Current OS: Debian GNU Linux kernel 2.4.32 IPv4 + grsec
  • 1go Ram DDR
  • Intel (r) celeon (r) CPU 2.66 GHz
  • Swap 512mo
  • 3go on/and 226go on/home
  • Running services are Qmail, bind9, MRTG, Apache 2.2.2, PHP 5.1.4, MySQL 5.0.21


The best way for tuning a server is to have dedicated services on one server and so, having multiple server especially for MySQL and Apache.

We were runing a heavy website with dotclear and the heavy phpads with all its stuff (geoip, all counters, etc .)
The server up to a load of 114 in some peak with a swap totally used! And so .. a big freeze of services... 70 K mails/day, 110 k pv/day, 12 k V/day, 47 SQL queries/sec

In fact, services weren't so loaded but the box was crashing a lot and swapping often without using too much CPU.

First things that I do was to change the Linux kernel from a 2.4.32 to a 2.6.18. Lot of things were improves in 2.6. I convey you to take a look at those posts:
Http://www-128.ibm.com/developerworks/linux/library/l-web26/
Http://developer.osdl.org/craiger/hackbench/
Http://kerneltrap.org/node/903

After this update, I take the time to update all Version Software for using a MySQL 5.0.27, PHP 5.2 etc. without looking at the changelogs, bugfixes will still help us
After this, we will tune our software configuration that still use default values (this is really bad! Then we will tune a little the kernel without recompile a new one.

  • Apache 2.2.2 prefork

OurHttpdIs using some modules as URL rewriting, server info, PhP5, geoip and other basic modules. we cocould optimize much more by using an Apache 2.2.3 worker and only useful modules or even more delivering static pages and Using proxy for dynamic pages. all this depend on your developments and your server usage. here we will only focus on the Apache prefork.
Nowadays, it's important to keep activeKeepaliveFunctionality. this will increase the speed of delivring pages for lot of modern browsers (It's supported by IE, Firefox, Safari, opera, etc .). the only thing is to touch a little to the default value. in fact, if your keepalive Time Out is too big, you will keep an entire Apache slot open for a user that is probably gone! A 4 seconds timeout is enough for delivering a full web page and take care of any network congestion. maxkeepaliverequests is used to define the maximum number of request manage by an Apache slot during a keepalive session. before t if you have lot of pictures to load on your web pages you don't really need to have a big value at this state.

Keepalive on
Keepalivetimeout 4
Maxkeepaliverequests 500

As I don't have lot of memory available on the server I'm constraint to decrease drastically the number of running servers from 150 to 60. as I have an Apache using Approximatly 13mo of memory (withdraw 3mo of shared memory), I need approximately 600 mo of available memory when all the Apache child process are running.We have to consider, for our further tuning, that this memory is used.It's really important in our case to dedicate memory for avoid to swap too much and lost the box in a freeze. You can follow your memory usage by usingTopAnd looking for your Apache/httpd process. (do a quick"Man top"For know more). If you have little more free memory you can take a look to the Apache documentation for further tuning.

Serverlimit 60
Maxclients 60

Our server is often overload, with lot of traffic. when I need to restart the Apache, or in case of any crashes the Apache server start with only 5 child server process and will add new one 1 second later, 2 new Child 2 second later, 4 New at the third second, etc. it's really too long when you are in a peak! So, I configured startservers for let us start directly with 30 child server process. That will help us to deliver quickly the clients and minimize the impact of the server restart.

Minspareservers and maxspareservers is used in same way as startserver. when your Apache server isn' t load, there is idle child waiting for connection. it's not usefull to have all your child still open but, in case of a new peak the best way to minimize its impact on your server is to deliver web pages as quick as possible. so keeping some idle child process still waiting for client isn' t so stupid. furthermore in case of Our touchy server we consider to be able to allocate 600mo of RAM. so, we can use it even if it's for idle child process as we dedicate this ram for Apache. for avoid any module memory leak, and having fully available child I set the maxrequestperchild to 1000, that mean that each 1000 request, the child will be kill and Apache server will spare a new one. you'll probably have to set this value to a higher number. it's depend of the structure of your web page. you will have to monitor a little your server after those change for being sure to don't have too much child kill/spare instead of delivering web pages.

Startservers 30
Minspareservers 30
Maxspareservers 30
Maxrequestsperchild 1000

Follow some security issue, we don't display too much information about our server. as we don't need the reverse lookup on the Client IP, we keep the default value of hostnamelookups to off and by this way we save some network traffic and server load.

ServerTokens Prod
Serversignature off
Hostnamelookups off

  • PHP 5.1.4

For perform our page generation and save some CPU we use the PHP extensioneaccelerator. Take a look at the documentation for install it.
We dedicate 32mo of our Ram for eaccelerator (Shm_size) And will use it with shared memory and File Cache ("Shm_and_disk"Value for keys, sessions and Content Variable ). (memory is really useful in our case, because of all the mails, Apache log and MySQL disk access that generate too much I/O and slow down considerably all the server ). as we don't change often the PHP script on the server we don't need to useCheck_mtimeFunctionality. when set to "1 ″, that will do a stat on the PHP script for checking of last modification date we don't need this because we want to save disk access and we don't have so far updates on the Running scripts. we just have to clean the cache directory after an update.

Eaccelerator. shm_size = "32 ″
Eaccelerator. cache_dir = "/www/tmp/eaccelerator"
Eaccelerator. Enable = "1 ″
Eaccelerator. optimizer = "1 ″
Eaccelerator. check_mtime = "0 ″
Eaccelerator. DEBUG = "0 ″
Eaccelerator. Filter = ""
Eaccelerator. shm_max = "0 ″
Eaccelerator. shm_ttl = "3600 ″
Eaccelerator. shm_prune_period = "1 ″
Eaccelerator. shm_only = "0 ″
Eaccelerator. Compress = "1 ″
Eaccelerator. compress_level = "9 ″
Eaccelerator. Keys = "shm_and_disk"
Eaccelerator. Sessions = "shm_and_disk"
Eaccelerator. content = "shm_and_disk"

  • MySQL 5.0.24

As I don't manage how has been coding ratio of running script, I decrease all the timeout MySQL connection for avoid congestion. then I increase the number off simultaneous MySQL connection as we had lot of "too transaction connection" error message.

Wait_timeout = 6
Connect_timeout = 5
Interactive_timeout = 120
Max_connections = 500
Max_user_connection= 500

Now we change the touchiest part of the MySQL configuration: the RAM usage. it's touchy because a bad value can really decrease your server performance and result in a big server swap. after some test I decrease the table cache and the key buffer cache to 256mo. in fact we don't have so far available RAM as we had 600mo for ourHttpdAnd we have lot of other services running. I tried to set it up little higher, hopping that the swap won't be to big, but in fact, due to our I/O load the swap were totaly not a good thing for MySQL

If you are using MyISAM tables I suggest you to use the"Concurrent_insert = 2"That will really increase your server performance in your case. myISAM use table lock, with concurrent insert, the engine will sometime bypass the lock and allow insert and select to run concurrently. we also disable all engine that is not used (InnoDB, bdb ). take a look at the MySQL documentation for better tuning.

Join_buffer_size = 1 m
Sort_buffer_size = 1 m
Read_buffer_size = 1 m
Read_rnd_buffer_size = 1 m
Table_cache = 256 m
Max_allowed_packet = 4 m

Key_buffer = 256 m
Key_buffer_size = 256 m
Thread_cache = 256 m
Thread_concurrency = 2
Thread_cache_size = 40
Thread_stack = 128 K

Concurrent_insert = 2

Query_cache_limit = 1 m
Query_cache_size = 256 m
Query_cache_type = 1
Skip-bdb
Skip-InnoDB

  • Linux Kernel 2.6.18

Here is a touchy part of our tuning, we will try to perform the Linux kernel behavior with our server load for save some memory and avoid too much swap. furthermore, has we done a great stuff above this part, we have to manage more TCP connection and support correctly the peak. we will use the command "sysctl" for doing our update on values.

# Display value of a variable or group of Variable
Sysctl [-N] [-E] variable...
# Set a new value toe the specified variable
Sysctl [-N] [-E] [-q]-W variable = value...
# Display all the variable
Sysctl [-N] [-E]-
# Load a sysctl Config File
Sysctl [-N] [-E] [-q]-P (default/etc/sysctl. conf)

For our test we will create a test config file "/etc/sysctl. conf. Testing" and we will load it by using the following command line:

Sysctl-P/etc/sysctl. conf. Testing

When you will be gglad of your change you cocould rename the file for "/etc/sysctl. conf ". all the sysctl variable are supported ented with the kernel sources. I suggest you to download the documentation corresponding to your kernel version and read it carefully if you decide to change some values.
A really good article on security focus give us some key for minimize the impact ofSyn Attack/SYN Spoofing. In this goal we activate the syncookies and the route Validation

Net. ipv4.conf. Default. rp_filter = 1
Net. ipv4.tcp _ syncookies = 1
Net. ipv4.tcp _ synack_retries = 3
Net. ipv4.tcp _ syn_retries = 3

As we had some SWAP troubles, important thing to do is to change the valueVM. swappinessWhere the default value is 60. This variable control how much the kernel shoshould favor swapping out applications, its value can be 0 to 100. I set it to 10 for minimize the swap.

VM. swappiness = 10

We upgrade the max backlog for support more TCP traffic and we change the congestion control algorithm to Bic. the Linux Kernel support lot of congestion algorithm like Reno (default one), HTCP, Vegas, Westwood, etc.

Net. Core. netdev_max_backlog = 2500 # interface Buffering
Net. ipv4.tcp _ max_syn_backlog = 4096
Net. Core. somaxconn = 1024 # limit of socket listen () Backlog. Default is 128.
Net. ipv4.tcp _ congestion_control = BIC

For avoid to have a big TCP queue and so memory usage for not really active connection I decrease some TCP timeout and force the kernel to recycle quickly TCP connection. We don't cache the valueSsthresh(Slow start threshold) for avoid to impact a given host to have a ced ssthresh for all is next connections.

Net. ipv4.tcp _ keepalive_time = 900
Net. ipv4.tcp _ fin_timeout = 30
Net. ipv4.tcp _ max_orphans = 16384
Net. ipv4.tcp _ tw_reuse = 1
Net. ipv4.tcp _ tw_recycle = 1
Net. ipv4.tcp _ rfc1337 = 1
Net. ipv4.tcp _ no_metrics_save = 1

It's critical to use the optimal send and receive Socket buffer size for the link you are using. in our case we have a 100 Mbits link connection. so for a better TCP connection and congestion control we had to increase the TCP buffer. you can read more about this here.

Net. Core. rmem_max = 16777216
Net. Core. wmem_max = 16777216
Net. ipv4.tcp _ rmem = 4096 87380 16777216
Net. ipv4.tcp _ WMEM = 4096 65536 16777216

  • That's all folks!

Now, this server support twice more traffic load. technical aspect was our traffic growth bottleneck. lot of other tuning cocould be done for better performance (on I/O and disk access, other kernel options, compile a new kernel, using Apache worker, etc .). this post was just some clues about how to tune your servers. one important thing to don't forget is whatever you tune on your server, that will Never be enough if you have a bloody developed programs running on it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.