Beruflich Dokumente
Kultur Dokumente
T 01 252.5 6 0.5 6 5
E H E LLO @ WIRE H IVE.COM
WIRE H IVE .CO M
S U P PO RT TOPIC
Configuring
Apache and PHP
for stability and
performance.
SUPP O RT TOP I C
In this document we are going to go through the initial things you should look at
to get your server to a point where you can leave it overnight without worry it’s
going to crash.
Prefork MPM is the default option and uses multiple child processes with one
thread each and each process handles one connection at a time. (This is a thread
safe option)
Worker MPM uses multiple child processes with many threads each. Each thread
handles one connection at a time.
The first line in the PHP Installation Manual dictates that if you’re running PHP as an
Apache module (mod_php) that you need to use a ThreadSafe MPM mode, which
limits it to only being run in PREFORK mode.
Support Topic 3
Configuring Apache and PHP
for stability and performance
The main issue with mod_php and Mpm Prefork is that every time Apache forks
into a new process (every new request) the entire PHP module and all its sub
modules have to be loaded into memory along with all your other data which
means you’re wasting a lot of memory on multiple copies of the same thing.
To get around the memory usage caused by loading php into every Apache
process is to use PHP as an external resource (PHP-FPM instead of mod_php) this
then also allows you to run Apache with MPM Worker which mean Apache has
a master process (now smaller as it doesn’t include all the bloat that comes with
PHP) that spawns a small number of child processes to deal with http requests,
CSS, Images, file downloads etc allowing for faster parallel processing.
A brief look into the configuration files for PHP-FPM and mod_php
The configuration files for PHP are very similar regardless of if you’re using mod_
php or PHP-FPM and are stored in /etc/php/ and /etc/php/fpm
./PHP-FPM.conf basic settings for PHP-FPM (most things in here can be left as
default)
./pool.d/www.conf This is the default FPM Pool definition, you can create more files or
more entries in the same file if you need more pools however that
is not something we will cover in this document.
The main file you need to look at for initial tuning is /etc/php/fpm/pool.d/www.
conf
This file contains the configuration for startup and the number of processes that
can be spawned while its running. By default there are a lot of variables in this file
as well as some good comments explaining what everything does. However to
keep things simple the file can be stripped down to just the following:
Support Topic 4
Configuring Apache and PHP
for stability and performance
[www]
user= www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children=10
pm.start_servers =5
pm.min_spare_servers = 2
pm.max_spare_servers = 7
chdir = /
The configuration above start PHP-FPM with 5 processes waiting for connections
from Apache and will keep between 2 and 7 running at any time with a hard limit
of 10. At the point it has 10 active processes all working any new requests will be
queued until one is spare.
These processes will run as the www-data:www-data user, this should normally be
configured to run as the same user your Apache process runs as.
For mod_php as it’s loaded into Apache as a module and you configure it within
Apache’s settings.
etc/apache2/apache2.conf
or
/etc/httpd/conf/httpd.conf
The variables you need to search for are in the <IfModule Prefork.c> section.
They do much the same thing as with the options in PHP-FPM however the
variable names are slightly different.
Looking at a default install the numbers might mean very little at the moment
but Apache has a default configuration of allowing 150 MPM Workers, which
means after adding mod_php for a site that uses 75mb of RAM per process (quite
common) a spike in website traffic that uses all of these at the same time (often
happens when a search engine crawls your site) would mean 11.25GB of RAM
would be required for Apache alone.
On the other hand PHP-FPM comes with a default limit of 10 processes which is
much more stable however can again be tuned to match your server.
Support Topic 5
Configuring Apache and PHP
for stability and performance
Fact Finding
Things you will need to know:
• How much memory the system has
• What the system needs to do aside from processing PHP requests
• How much memory each PHP process consumes
• What you need restart for your changes to take effect
Once you can predict the maximum amount of RAM a PHP process is going to
require and the amount of RAM you have in the system to use you can easily work
out what the hard limit for the maximum number of processes should be set to.
Note this may change over time as the code for the site changes and increases or
decreases the amount
Most of the information you need can be found from the application ‘top’ which
comes installed on most systems. Running top from the command line on your
server will bring you into a screen like the one below. (This needs to be done
once apache and PHP have been running for a while, if you do it straight away PHP
will not yet have loaded all its sub modules leading to lower memory usage per
process and unsustainable hard limits).
Support Topic 6
Configuring Apache and PHP
for stability and performance
Figure 1
From the screenshot in Figure 1 you can see the total system RAM highlighted in
the top box, a good idea of the other high resource system processes and amount
of RAM used by each PHP-FPM process in the lower box.
For reference the basic Linux system uses about 250mb of RAM.
The memory usage by PHP-FPM in this example is a little higher than I would
expect to see, we normally see PHP running at around 95-100mb per process
however the Apache2 processes can be seen using 15MB of RAM each.
If you look at Figure 2 you will see again this server has 4GB of RAM, MySQL is
using 73mb this time, the system takes about 250mb and instead of having a PHP-
FPM process we are seeing lots of Apache ‘httpd’ processes spawned instead
each using ~70MB.
Support Topic 7
Configuring Apache and PHP
for stability and performance
Figure 2
Number of Processes =
(Total System RAM – (Large processes + system usage rounded up)) / (PHP / Apache
Process size)
Example 1:
(4000 – (410 +200 +250)) / (152+93+91+88+78+74 /6) = 32.708
Consequently the safe number of maximum php-fpm processes this server should
be configured to run is 32. (pm.max_children=32)
Once you have made a change you will need to restart php-fpm for it to take
effect, this can be done with ‘/etc/init.d/php-fpm restart’. Restarting just Apache
will NOT apply these changes as PHP is now running as a separate process and as
such needs restarting independently.
Support Topic 8
Configuring Apache and PHP
for stability and performance
Example 2
(3924 – (250 +73)) / 70 = 51
which would need to be set in httpd.conf for the ServerLimit and MaxClients
variables.
Having adjusted a setting in here you will need to restart Apache for this change
to take effect with ‘service httpd restart’ or ‘/etc/init.d/apache2 restart’.
WARNING: [pool www] server reached pm.max_children setting (10), consider raising
it.
This means Apache has started queuing requests waiting for a free PHP process
to pass its request to which may cause delays in page load time but is an
improvement on the server crashing and being offline.
Running the server right up to its memory limit with the maximum number of PHP
processes simultaneously may give you the ability to handle a large number of
simultaneous requests however each one is going to be fighting for CPU time with
the next so may take longer than it would if you only had one or 2 processed at a
time and then moved onto the next.
Support Topic 9
Configuring Apache and PHP
for stability and performance
You may find by the time you have filled ½ your system RAM with PHP processes
you have over saturated your CPU’s and that adding more concurrent PHP
requests actually just bogs the system down further slowing down your page
loads.
To see these differences however you will need to load test your site and server
with different configurations in place to find out which give the best compromise
between page load speed and concurrent user capacity.
If you get to the point where your server is still not able to handle the number
of requests you’re receiving here are a couple of options you should look into
further:
© Wirehive. 2016
This is an unpublished work the copyright in which vests in Wirehive Ltd. All rights re-
served. The information contained herein is confidential and the property of Wirehive
Ltd, and is supplied without liability for errors and omissions. No part may be repro-
duced, disclosed or used except as authorised by contract or other written permission.
The copyright and the foregoing restriction on reproduction and use extend to all
media in which the information may be embodied.