Sie sind auf Seite 1von 10

Support Topic 23-24 H E RCULE S WAY 1

Configuring Apache and PHPFARNBO RO UG H


for stability and performanceH AMPS H IRE G U1 4 6UU

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.

Great brands are built on great


experiences. We deliver them.
wirehive.com
Support Topic 2
Configuring Apache and PHP
for stability and performance

SUPP O RT TOP I C

Configuring Apache and PHP


Often people blame Apache and PHP for being slow or crashing their server
when in fact both of these things can be addressed with a little bit of tuning.

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.

Apache MPM Modes


Prefork and Worker are two types of MPM (Multi-Processing Modules) Apache
provides.

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.

Which one should you choose?


This decision will ultimately be made for you depending on what you select when
it comes to PHP. This is a configurable you’re going to need to come back too
later.

PHP as a Module or as an external resource?


The traditional way to run Apache + PHP is to use Apache with ‘mod_php’. This
is quick and functional and this is fine for smaller sites however if you’re trying to
run a complex ecommerce site built on a large PHP framework with support for
external plugins and 3rd party modules and you shouldn’t just put up with the
mediocre basic settings and high resource usage that it leads too.

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.

Configuring Apache to us PHP-FPM instead of mod_


php
Switching from mod_php to using PHP-FPM requires a few system changes to be
made. Information on installing PHP-FPM can be found here.

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.ini where your normal php settings changes need to go

./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

Tuning to stop your server crashing


The process of tuning Apache/PHP is almost the same regardless of whether
you’re using mod_php or PHP-FPM however as discussed earlier the file you need
to edit and the variables are slightly different.

What you’re aiming to do is to configure a hard limit on the number of PHP


processes (or Apache processes with mod_php included) so that the system
doesn’t run out memory and start using SWAP space while dealing with a high
number of requests.

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.

In this instance the server has:


• 4GB Total RAM
• varnishd is using 407M
• mysqld is using 196MB
• PHP-FPM has 6 running processes each using between 74mb and 152mb.

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

Working out your limits


To work out the hard limit for the maximum number of PHP/Apache processes that
can be run you need to take the total system ram, minus system usage and other
major processes, and then divide the remainder by the amount of RAM each PHP/
Apache process uses.

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’.

Conclusions and after thoughts


At this point you should have made your decision on running how you want to run
PHP, looked at the configuration files and found the variables you need to adjust,
worked out how much resource you have and worked out what you need to limit
your system too and applied these new settings.

Some other things to consider with mod_php:


Limiting your Apache to a low number of maximum workers may limit your ability
to answer large numbers of requests for static content while all your processes are
busy processing PHP files.

Some other things to consider with php-fpm:


If your site gets enough connections to hit the hard limit in PHP you will see the
following in your Apache logs:

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.

What you can do if this is still not enough:

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:

• Full Page Caching


• Getting a bigger server
• Load balance across multiple backend servers

We are always happy to discuss these technical options at Wirehive so give us a


call.
W IRE H IVESupport Topic T 01 252.5 6 0.5 6 5 10
Configuring
2 3-24 H E RCULE Apache and PHPE H E LLO @ WIRE H IVE.COM
S WAY
FARNBO RO for UG
stability
H and performanceWIRE H IVE .CO M
HAMPS H IRE G U1 4 6 UU

© 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.

Das könnte Ihnen auch gefallen