Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support On CentOS 6.0 - Page 2

5 Installing PHP5

We can make PHP5 work in nginx through PHP-FPM (PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites). We can install php-fpm together with php-cli and some PHP5 modules like php-mysql which you need if you want to use MySQL from your PHP scripts as follows:

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

Then open /etc/php.ini and set cgi.fix_pathinfo=0:

vi /etc/php.ini

; 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

(Please read to find out why you should do this.)

In addition to that, in order to avoid errors like

[08-Aug-2011 18:07:08] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead in /usr/share/nginx/html/info.php on line 2

... in /var/log/php-fpm/www-error.log when you call a PHP script in your browser, you should set date.timezone in /etc/php.ini:

; Defines the default timezone used by the date functions
date.timezone = "Europe/Berlin"

You can find out the correct timezone for your system by running:

cat /etc/sysconfig/clock

[root@server1 nginx]# cat /etc/sysconfig/clock
[root@server1 nginx]#

Next create the system startup links for php-fpm and start it:

chkconfig --levels 235 php-fpm on
/etc/init.d/php-fpm start

PHP-FPM is a daemon process (with the init script /etc/init.d/php-fpm) that runs a FastCGI server on port 9000.


6 Configuring nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

vi /etc/nginx/nginx.conf

The configuration is easy to understand (you can learn more about it here: and here:

First (this is optional) you can increase the number of worker processes and set the keepalive_timeout to a reasonable value:

worker_processes  5;
    keepalive_timeout  2;

The virtual hosts are defined in server {} containers. Let's modify the default vhost (in /etc/nginx/nginx.conf as well) as follows:

    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;
            try_files $uri =404;
            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;

server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like

In the location / part, I've added index.php to the index line. root /usr/share/nginx/html; means that the document root is the directory /usr/share/nginx/html.

The important part for PHP is the location ~ \.php$ {} stanza. Uncomment it to enable it. Change the root line to the web site's document root (e.g. root /usr/share/nginx/html;). Please make sure that you change the fastcgi_param line to fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; because otherwise the PHP interpreter won't find the PHP script that you call in your browser ($document_root translates to /usr/share/nginx/html because that's what we have set as our document root).

PHP-FPM is listening on port 9000 on by default, therefore we tell nginx to connect to with the line fastcgi_pass;. It is also possible to make PHP-FPM use a Unix socket - I will describe this in chapter 7.

Now save the file and restart nginx:

/etc/init.d/nginx restart

Now create the following PHP file in the document root /usr/share/nginx/html...

vi /usr/share/nginx/html/info.php


Now we call that file in a browser (e.g.

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5, including the MySQL module:


7 Making PHP-FPM Use A Unix Socket

By default PHP-FPM is listening on port 9000 on It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php-fpm.d/www.conf...

vi /etc/php-fpm.d/www.conf

... and make the listen line look as follows:

;listen =
listen = /tmp/php5-fpm.sock

Then restart PHP-FPM:

/etc/init.d/php-fpm restart

Next go through your nginx configuration and all your vhosts and change the line fastcgi_pass; to fastcgi_pass unix:/tmp/php5-fpm.sock;, e.g. like this:

vi /etc/nginx/nginx.conf

        location ~ \.php$ {
            root           /usr/share/nginx/html;
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

Finally restart nginx:

/etc/init.d/nginx restart



About The Author

Falko Timme is the owner of Boost Your Site mit Timme Hosting - ultra-schnelles nginx-WebhostingTimme Hosting (ultra-fast nginx web hosting). He is the lead maintainer of HowtoForge (since 2005) and one of the core developers of ISPConfig (since 2000). He has also contributed to the O'Reilly book "Linux System Administration".

Share this page:

7 Comment(s)

Add comment


From: Zenettii at: 2011-08-17 16:58:33

I recently setup my own CentOS box with nginx, php, mysql to run wordpress. also installing a security app or two.

Generally found the process fairly easy, I've used spawn-fcgi binary for looking after php-cgi process. have noticed that it's not the most stable, and had to write a basic bash script to place in cron, to run a quick process check every 60 seconds and if php-cgi is not up, launch it, this is a basic form of watchdog which has proven priceless. 

 I run https (SSL such as openSSL needs to be installed and configured, and certifcates) which was interesting to setup, also only run https with 1 worker_process, or CPU will complain.


Strongly advise you not to run these as root. If any of the applications can be remotely exploited, allowing that to happen under a root user is just a recipe for failure and 'pwnage'. Exploits come out on a regular basis, and you can bet that within 1 hour they are used on hundreds of targets before the application developer can produce a patch/fix, and it gets applied by the web hosts.


From: tabunon at: 2011-12-17 07:19:37


 nginx didn't show up on the browser after going through Step #4. Anything I missed in setting up my box?

From: tabunon at: 2011-12-17 07:55:59

I'm building a web server running from a virtual machine and everything went well. Following the procedures in running the webserver, I don't seem get nginx running - seeing it from the browser. I ran 'netstat' to see the http service running - nothing wrong there, I guess.

From: Vman at: 2012-03-07 05:13:20

Please update your repository link in the beginning steps:

Great article, thank you.

From: Anonymous at: 2012-04-15 20:49:13

i followed your guide. everything was ok until this step: Now we call that file in a browser (e.g.

when i call "localhost/info.php" it says

You have chosen to open


which is a: BIN file (20 bytes)

 from: http://localhost

Would you like to save this file?

why this is saying so? whats the problem?

From: Anonymous at: 2013-01-17 23:05:21

the correct way to install repositories:
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

From: Joe T at: 2013-12-21 09:02:28

installing nginx from source and wanted to say thanks for helping me figure it out