When design the high perfomance website sytem, we have many options : Apache or Nginx, PHP or PHP-FPM….
In this article, i will introduce you how we can build a high performance system with Apache 2.4, MPM-Event with PHP-FPM.
Install
Assuming that you’re starting with a basic LAMP stack, to get Event MPM and PHP-FPM up and running on Ubuntu 14.04 first install these packages:
1 2 |
sudo apt-get install libapache2-mod-fastcgi php5-fpm apache2-mpm-event |
Configuration
For php-fpm
, after install we will have main config file /etc/php5/fpm/php-fpm.conf
.
In this file you will see the line at the bottom :
1 2 3 4 |
; To configure the pools it is recommended to have one .conf file per ; pool in the following directory: include=/etc/php5/fpm/pool.d/*.conf |
We create a new config file call www.conf
on this directory and place our config :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
listen = /var/run/php5-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0660 pm = dynamic pm.max_children = 500 pm.start_servers = 16 pm.min_spare_servers = 16 pm.max_spare_servers = 100 pm.max_requests = 1024 request_slowlog_timeout = 5s slowlog = /var/log/apache2/fcgi_$pool.log.slow request_terminate_timeout = 300s rlimit_files = 131072 rlimit_core = unlimited catch_workers_output = yes env[HOSTNAME] = $HOSTNAME env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp chdir = / |
Please take a look that :
1 2 3 4 5 6 7 |
pm = dynamic pm.max_children = 500 pm.start_servers = 16 pm.min_spare_servers = 16 pm.max_spare_servers = 100 pm.max_requests = 1024 |
Those settings are very important for increase perfomance of our web server.
Next, we edit a setting file /etc/apache2/conf-available/php5-fpm.conf
for PHP-FPM working with Apache through libapache2-mod-fastcgi
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<IfModule mod_fastcgi.c> AddHandler php5-fcgi .php 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 <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule> |
Next, we edit config file for mpm-event
at /etc/apache2/mod-available/mpm-event.conf
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# event MPM # StartServers: initial number of server processes to start # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestWorkers: maximum number of worker threads # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_event_module> ServerLimit 256 StartServers 25 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 50 ThreadsPerChild 25 MaxRequestWorkers 6400 MaxConnectionsPerChild 0 </IfModule> |
Please note that those setting are very important for scale website.
Setup and running
Our last step is make them running all :
1 2 3 4 5 6 |
sudo a2enmod actions fastcgi alias sudo a2dismod mpm_prefork php5 sudo a2enconf php5-fpm sudo a2enmod mpm_event sudo service apache2 restart |
With this design, our web server can handle about 4000 CCU.