Tuesday, April 28, 2015

php-fpm+mpm_event(apache2.4.7) install and setup.(ubuntu14.04 trusty) and simple comparison to mod_php+mpm_prefork

#Simple test with ab(installed by default apache2) that is basic web load test tool.
#php-fpm vs mod_php
#apm1 is php-fpm+fast-cgi+mpm_event thread; apm2 host is mod_php+mpm_prefork.


#php-fpm fast-cgi and apache2.4.x mpm-event setup
#On ubuntu 14.04 trusty.
#Apache mpm-prefork based server is default.
#To improve server performance, sometimes we need to mpm-worker or mpm-event thread based apache2.4.X web server.
#Here I introduce how to install and setup.
#I'm using recent wordpress version for this simple experiment.

1. Setup of php-fpm+mpm_event.
#MySQL:5.6 version.
root#>apt-get install mysql-server-5.6
root#>apt-get install apache2-mpm-event
root@apm1#>apt-get install libapache2-mod-fastcgi php5-fpm php5
root@apm1#>a2enmod actions fastcgi alias
root@apm1#>vi /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName 10.0.0.21

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

        #fastcgi php-fpm
        <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>
              DirectoryIndex index.php
              #Order Deny,Allow
              #Deny from all
              Options All
              AllowOverride All
              Require all granted
        </Directory>
        </IfModule>

</VirtualHost>



#Disable mpm-prefork and enable mpm_event module
roott@apm1:/etc/apache2/mods-enabled# ln -s ../mods-available/mpm_event.conf mpm_event.conf
root@apm1:/etc/apache2/mods-enabled# ln -s ../mods-available/mpm_event.load mpm_event.load
root@apm1:/etc/apache2/mods-enabled# rm mpm_prefork.* php_conf.*
root@apm1:/etc/apache2/mods-enabled# service apache2 restart


#service status and etc.
root@apm1#>service php5-fpm status
root@apm1#>/etc/init.d/php5-fpm reload


#wordpress setup
root@apm1#>cd /var/www/html/
root@apm1#>wget https://wordpress.org/latest.zip
root@apm1#>unzip latest.zip
root@apm1#>apt-get install unzip
root@apm1#>unzip latest.zip
root@apm1#>mysql -u root -p
root@apm1#>cd wordpress/
root@apm1#>cp wp-config-sample.php wp-config.php

root@apm1#>cd /var/log/apache2/
root@apm1#>/var/www/html/wordpress# vi /root/mysql_create_wordpress.sh

#Creating database and user for wordpress app.
root@apm1#>mysql -u root -p
CREATE DATABASE IF NOT EXISTS wordpress;
USE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* to wordpress@localhost identified by 'wordpresspass';
FLUSH PRIVILEGES;

#There should be no errors.

root@ts:/var/www/html/wordpress# vi /root/change_wordpress.sh
#!/usr/bin/env bash
#Using sed to change default name dbname and password. Optionally hostname(commented on)
sed -i 's/database_name_here/wordpress/g' /var/www/html/wordpress/wp-config.php
sed -i 's/username_here/wordpress/g' /var/www/html/wordpress/wp-config.php
sed -i 's/password_here/wordpresspass/g' /var/www/html/wordpress/wp-config.php
#sed -i 's/localhost/wanted_ip_or_hostname/g' /var/www/html/wordpress/wp-config.php

root@ts:/var/www/html/wordpress# bash /root/change_wordpress.sh

#popup the brower http://installed_hostname/wordpress/wp-config.php


2.mod_php based installation.
http://wnapdlf.blogspot.kr/2015/04/lamp-easy-set-up-method-on-ubuntu-1404.html



3. Let's test with simple ab web load tester.
 Because, these two server's configuration is default, lots of concurrent is not supported. So, ab option is very limited. I want to know and show how different of top command statistics.

From host to apm1 and apm2, I use ab command as follows on each server.
Each server has 2 cpus(virtual) and 1024 mega-bytes.

root-host# ab -n 100000 -c 100 http://10.0.0.21/wordpress

*apm1*php-fpm fastcgi based wordpress web server.

Top command shows that there are just two apache2 process one for new request and the other existing requests(according to the document). It handles all 100000 request at 100 concurrent request. Load average highest is 1.76


 *mod_php with mpm_prefork based wordpress web server.
root-host# ab -n 100000 -c 100 http://10.0.0.22/wordpress
 Top command shows that there are many apache2 process. It handles all 100000 request at 100 concurrent request. Load average highest is10.17.
 

 *Conclusion)
If we need to handle many concurrent request on php-based webserver, we might as well consider php-fpm(fast-cgi)+mpm_event(or worker) based one.
 It gives us lower cost on server resources.

Yet, more sutle tuning is necessary to test throughly that I did before as in server(on linux sysctl.conf tuning) and configuration such as "MaxServers","ThreadPerChild" and so on.

 Now, php-fpm is more stable than past. And mpm_event module is not experimental anymore according to each site.

http://wiki.apache.org/httpd/PHP-FPM
http://httpd.apache.org/docs/2.4/mod/event.html


Thanks for reading.




No comments:

Post a Comment