April 2012
« Mar   May »

Boosting site performance using Nginx, Php-FPM and APC

Earlier I had tried to boost the php based web application using Nginx weberver using php-cgi module. It’s also showing better performance compare to older Apache-Php module.

Ngnix is the fastest http server and we are using the power of cgi scripts to boost the php compilation speed. Obviously the combination of Nginx+ php-cgi module performs much better for small high speed websites. But this new technology php-fpm would also helpful to manage the different cgi requests effectively.

In this post, I am using the power of php-fpm and APC which would accelerate the pages response time much more faster. Here you need to understand the role of diff application.

1. Nginx : It’s a fastest, light weight http webserver which is good for server static pages/files.
2. Php-fpm (FastCGI Process Manager) : This is the manager for administrating cgi process.
3. APC [Alternative PHP Cache]: This program will caches all the php request which had being executed recently and serves it when it requested next time until the cache expires. So it’s saves the execution time as well as server processing power.
4. Memcache : It is used for caching mysql queries which is frequently executed by php.

Let’s start the server preparation.,

I would not recommend to do it on any existing production servers since this process requires php re-installation. So try it on fresh server ( Here i uses CentOS6.2 (64 bit).

Installing PHP-FPM from Remi Repository.

Let’s completed the basic LAMP setup on new server and install the Remi- repository suit to your platform. You may get the install document from here, skip Apache,

#yum install php* mysql*
# wget
#rpm -ivh remi-release-6-1.el6.remi.noarch.rpm --nodeps
#yum --enablerepo=remi install php php-fpm
#chkconfig --level 345 php-fpm on
#/etc/init.d/php-fpm restart
[root@ ~]# netstat -nlp | grep "9000"
tcp        0      0    *                   LISTEN      10188/php-fpm
[root@oyemedia ~]#
#yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt

2. Installing Nginx from Repository

Here we need to use the repository provided by the Ngnix website. Create a repository file under “/etc/repos.d/” to grab it. Create a file named “/etc/yum.repos.d/nginx.repo” and add the following content.

name=nginx repo

Installing Nginx using yum.

#vi /etc/yum.repos.d/nginx.repo
#yum install nginx
#chkconfig  nginx on
#service  nginx start
[root@ ~]# netstat -nlp | grep "80"
tcp        0      0        *                   LISTEN      32586/nginx
[root@oyemedia ~]#

3. Installing APC and memcache

  #yum install gcc, make pcre
#pecl install apc
# yum install php-pecl-memcache
#vi /etc/php.ini
root@oyemedia ~]# php -m | egrep -E "apc|memcache"

Add the APC extenion in /etc/php.ini file eg: extension = You may configure it’s value by adding/modifying the values in ” /etc/php.d/apc.ini” file as well.

We have completed the half part of the setup.

Next we need to re-tune the Nginx for servicing Php pages along with zip compression enabled. This is the virtual host file for serving joomla site. You may download the files from here

server {
    rewrite ^(.*)$1 permanent;
server {
  server_name ;
  access_log      /home/mydomain/access.log;
  error_log     /home/mydomain/public_html/error.log  warn
  root        /home/mydomain/public_html;
  index       index.php index.html;

client_header_timeout 240s;
client_body_timeout 240s;
fastcgi_read_timeout 240s;

location / {
    expires 30d;
    error_page 404 = @joomla;
    log_not_found off;

    try_files $uri $uri/ /index.php?q=$request_uri;

 location @joomla {
    rewrite ^(.*)$ /index.php?q=$1 last;

error_page 500 502 503 504 404  /404.html;
        location = /404.html {
         root  /home/mydomain/public_html/;

gzip on;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript

application/javascript application/x-javascr$
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";

# caching of files
location ~* \.(ico|pdf|flv)$ {
expires 60d;

location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
expires 14d;

location ~ \.php$ {
            root           html;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   /home/mydomain/public_html$fastcgi_script_name;
            include        fastcgi_params;
# Select files to be deserved by nginx
 location ~* ^.+.(swf|zip|rar|html|htm|pdf)$ {
#location ~* ^.+.(zip|rar|html|htm|pdf)$ {
    root  /home/mydomain/public_html/;
    expires           7d;

Note : You may need to change the ip address [] and the web location [ /home/mydomain/public_html] according to your setup.

Now test the Nginx config and restart the service,

[root@ ~]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@oyemedia ~]# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@ ~]#

Whola !!! 🙂 That’s it.

3 comments to Boosting site performance using Nginx, Php-FPM and APC

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>