I'm currently running apache2.2 with mod_event, php-fpm and apc on Debian Wheezy. I've hosting 3 normal websites and 1 really shitty website, which has really bad code and needs sometimes 2 minutes for a single page load. The database already runs on RDS, and shouldn't be the problem, but rather that the code does inefficient db lookups. However, we don't have a PHP developer anymore, so can't fix the code.

At some point when accessing this site, apache reaches MaxClients and FPM too.

apache2ctl -M output

  Loaded Modules:
  core_module (static)
  log_config_module (static)
  logio_module (static)
  version_module (static)
  mpm_event_module (static)
  http_module (static)
  so_module (static)
  actions_module (shared)
  alias_module (shared)
  authz_host_module (shared)
  deflate_module (shared)
  dir_module (shared)
  env_module (shared)
  expires_module (shared)
  fastcgi_module (shared)
  mime_module (shared)
  negotiation_module (shared)
  reqtimeout_module (shared)
  rewrite_module (shared)
  setenvif_module (shared)
  ssl_module (shared)
  status_module (shared)
  Syntax OK

Mod_Event config

  <IfModule mpm_event_module>
  StartServers          2
  MinSpareThreads      25
  MaxSpareThreads      75
  ThreadLimit          64
  ThreadsPerChild      25
  MaxClients          150
  MaxRequestsPerChild 0

Keepalive settings:

  Keepalive On
  MaxKeepAliveRequests 100
  KeepAliveTimeout 5

FPM config:

 pm = dynamic
 pm.max_children = 5
 pm.start_servers = 2
 pm.min_spare_servers = 1
 pm.max_spare_servers = 3



Fastcgi apache config:

<IfModule mod_fastcgi.c>
 AddHandler fastcgi-script .fcgi
 AddHandler php5-fcgi .php
 #FastCgiWrapper /usr/lib/apache2/suexec
 FastCgiIpcDir /var/lib/apache2/fastcgi
 Action php5-fcgi /php5-fcgi
 Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
 FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-   header Authorization -idle-timeout 250

Can you give me some tips how to optimise this setup? Thanks for your help.

Server load is very low(0.00 0.01 0.05 1/169 4823)

Memory looks ok:

               total       used       free     shared    buffers     cached
 Mem:          2011        431       1580          0         74        229
 -/+ buffers/cache:        126       1885

The event module settings should be set based off your memory usage. Looking at your memory load, these settings are set way to low. You need to estimate the use of each process using one of the many commands out there:

ps aux | grep 'apache2' | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}'

Please note there are a million sites that have similar commands on Google.

Since you are running PHP-FPM you may have to measure that as well (just change the grep section). The best way to do it, is under load. You can do this with an ab test, or some other similar solution.

If for example, your memory usage is 20mb per connection (which is what the command above outputs). Since you have 2011mb total ram, you need to subtract your overhead of linux, any caching mechnisms, etc... build some space in, and then take that number (say 1800mb) and divide it by your apache process.

If for example your result is 20mb per process, you can set your MaxClients to 90. If the result of the command is 1mb, then you can set it to 1800 MaxClients. At the same time, I would make sure to build in a buffer as all applications are not the same. One of your sites may use 1mb and the others use 40mb. You want to make sure you build a reasonable buffer or your Apache server will crash all the time. I usually do 10%-20% depending on the server.

Many people argue that you should set it to your PHP memory limit. This is a safety bet, and probably a good one depending on your setup. It takes a someone advanced admin to tweak the server beyond that point with little risk of failure.

Also I usually keep my KeepAlive off, I have found better performance these days by doing that.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.