CentOS Nginx + PHP-FPM (Part 3)

Nginx with PHP-FPMGreat to see you have made it this far. Good news is that hopefully by the end of this post we will have a fully working Nginx/PHP-FPM setup. So far we have both of the required services installed, there is just some final configuration to do so everything works as it should.So the first thing is to start up Nginx, as we have installed this via yum we don’t have to install any specical scrpits like php-fpm, just run these commands to start, and also flag to start on a system reboot.

service nginx start
chkconfig nginx on

As default all of our http traffic will be going to the standard http port (80) and Nginx is already bound to this as you can see from the output below:

[root@localhost nginx]# netstat -ltunp | grep "nginx\|php"
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      18521/php-cgi
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      19461/nginx.conf

However you can also see that the php service is listening on port 9000, as Nginx doesn’t perform any PHP processing we need to have it pass back any requests that are for a php file.

This is whats normally called a “reverse proxy”, and it’s actually quite simple, we just need to tell Nginx to pass back any files with the extension .php.
So open up the configuration file for Nginx, and uncomment the lines that you can see below. You will notice that my “root” and “fastcgi_param” are different to yours don’t forget to update these like mine, otherwise you will just get an error on php pages as it would have the incorrect path.

nano /etc/nginx/nginx.conf
location ~ \.php$ {
root           /usr/share/nginx/html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
include        fastcgi_params;

}

service nginx restart

So now our php requested should be sent back to php-fpm as a reverse proxy, sounds like time to test it out!

cd /usr/share/nginx/html
echo hello > hello.html
echo " " > phpinfo.php

Next browse to your ip address followed by the above file names, the hello.html will be served by nginx as thats a static file, where as the phpinfo.php should be reverse proxied to php-fpm.

http://your.ip.goes.here/hello.html
http://your.ip.goes.here/phpinfo.php

So thats it you now have a fully working setup, the next part (4) will help you configure name based virtual hosts in Nginx so you can host multiple websites. Future lessons are also going to setup bind for your DNS, ssl on nginx, mySQL so we can get wordpress up and running, plus loads more!

Technology enthusiastic with many ongoing online projects one of which is this personal blog PingBin. While also working full time within a data center designing and maintaining the network infrastructure.

Leave a reply:

Your email address will not be published.