Installing Nginx with PHP and PHP-FPM

Nginx (pronounced “engine x”) is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on a CentOS 6.0 server with PHP5 support (through PHP-FPM) and MySQL support.

For this time, i’ll be use the as my hostname, and the IP Address is Please use your own settings!

On this tutorial, i’ll use CentOS 6.0 linux distributions as the server operating system, please refer to your own OS’s documentations. This is how to installing Nginx with PHP and PHP-FPM on CentOS 6.0

Step 1. Adding Extras Repositories

Unfortunatelly, php-fpm is not available yet on CentOS repos, so we’ve to add the repo from Remi RPM ( that relies on EPEL repository. To enable the Remi RPM with EPEL support, you can do this:

rpm –import

rpm -ivh


rpm –import

rpm -ivh

then install yum-priorities first (if your CentOS is not yet installed with yum-priorities):

yum install yum-priorities

Change the priority level of EPEL repo priorities to 10, edit the /etc/yum.repos.d/epel.repo. Example:

name=Extra Packages for Enterprise Linux 6 - $basearch

Do the same to REMI repo and enable it, example:

name=Les RPM de remi pour Enterprise Linux $releasever - $basearch

name=Les RPM de remi en test pour Enterprise Linux $releasever - $basearch


Step 2. Install MySQL 5

To install MySQL 5, example:

yum install mysql mysql-server

Unfortunatelly, MySQL is not starts automatically on CentOS, so we’ve to create the system startup links to start MySQL Server automatically, by this example command:

chkconfig –levels 235 mysqld on

/etc/init.d/mysqld start

Beware! please disabled the skip-networking step on MySQL (edit your own /etc/my.cnf) and run the mysql_secure_installation as soon as possible after you do MySQL installation.


Step 3. Install Nginx

To install Nginx, you can use yum:

yum install nginx

and create the system startup for it then starts it:

chkconfig –levels 235 nginx on

/etc/init.d/nginx start

NB. Check with your Webbrowser to your server address (e.g, you should see the nginx welcome page.


Step 4. Install PHP5

PHP5 will running well on Nginx server with PHP-FPM (PHP FastCGI Process Manager). It can be installed together with php-cli and some PHP5 module like php-mysql. Example install:

yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

Fix the CGI path info on CentOS, edit the /etc/php.ini. Set cgi.fix_pathinfo = 0; example:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts


Step 5 (extra) Configuring Nginx

The configuration is in /etc/nginx/nginx.conf. For example, we can increase the number of worker processes and the keep alive timeout, like:

worker_processes 5;

keepalive_timeout 2;

Or you can defined new server vhost, it’s also in nginx.conf. Example:

    server {
        listen       80;
        server_name  _;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;

        # proxy the PHP scripts to Apache listening on
        #location ~ \.php$ {
        #    proxy_pass;

        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
            deny  all;

Restart Nginx with /etc/init.d/nginx restart

Happy Nginx-ing!

Wagiman Wiryosukiro

Petani Sistem Informasi, tukang las plugin & themes Wordpress. Co-Founder Saat ini aktif sebagai Developer & kontributor di OpenMandriva Linux.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: