Thursday 21 November 2013

setup nginx web server with PHP

Nginx (engine x) is a high performance lightweight HTTP server, more and more sites are using nginx, according to Netcraft survey (http://news.netcraft.com/archives/2013/11/01/november-2013-web-server-survey.html), nginx powers 15% of the busies sites in November 2013.

Nginx installation is very straight forward, we can download latest source code from http://nginx.org/en/download.html or point our yum source to http://nginx.org/packages/OS/OSRELEASE/$basearch/ and install using yum.
Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “5” or “6”, for 5.x or 6.x versions, respectively.
So for CentOS 6.3, we can point our YUM source to: http://nginx.org/packages/centos/6/$basearch/

If we install nginx using YUM, its binary is at /usr/sbin/nginx and configuration file at /etc/nginx/nginx.conf
[root@centos ~]# cat /etc/sysconfig/nginx
# Configuration file for the nginx service.

NGINX=/usr/sbin/nginx
CONFFILE=/etc/nginx/nginx.conf
We can use service command to start and stop nginx service:
[root@centos ~]# service nginx stop
Stopping nginx:                                    [  OK  ]
[root@centos ~]# service nginx start
Starting nginx:                                    [  OK  ]
[root@centos ~]# service nginx status
nginx (pid  2518) is running...
In Apache, the most common way of running PHP is through php5_module, we need to compile PHP against Apache and load mod_php module in Apache configuration files.
Unlike Apache, the most common way of running PHP in Nginx is through fastcgi
To configure fastcgi, add these lines to nginx.conf
    location ~ \.php$ {
        root   /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
 Restart nginx to make our changes take effect
[root@centos conf.d]# service nginx restart
Stopping nginx:                                    [  OK  ]
Starting nginx:                                    [  OK  ]
All the requests to PHP will be served by fastcgi running localhost port 9000, to start fastcgi on localhost, run this command
[root@centos conf.d]# php-cgi -b 127.0.0.1:9000 &
Confirm php-cgi is listening on port 9000
[root@centos conf.d]# netstat -pan | grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      3209/php-cgi

Now create our info.php in directory /usr/share/nginx/html
[root@centos conf.d]# cat <<EOF > /usr/share/nginx/html/info.php
> <?php
> phpinfo();
> ?>
> EOF

Launch web browser and visit http://localhost/info.php, the value of Server API should be CGI/FastCGI
FastCGI Process Manager (FPM) provides more features useful for heavy load sites, in production we should use FPM instead.
Since PHP 5.3.3, FPM is included in the PHP cores, we can use --enable-fpm to turn on php-fpm when installing PHP.

References:
http://nginx.org
http://news.netcraft.com/archives/2013/11/01/november-2013-web-server-survey.html
http://php-fpm.org/download/
http://www.php.net/manual/en/install.fpm.install.php

No comments:

Post a Comment