Hosting 101 – How To Configure PHP For Nginx On CentOS/Red Hat

24th May, 2016 by
 

Previously, in our Hosting 101 series, we looked at using Nginx as a replacement web server for Apache in a LAMP stack for web hosting. At the end of our last article we had Nginx configured, but only able to serve static files such as HTML files and images. In this article, we are going to look at the changes needed to configure PHP and Nginx to work together in order to serve dynamic content to your users.

Installing PHP-fpm

The first step in this configuration is to install PHP-fpm, which is the module that Nginx will be using to call PHP to interpret scripts. Fortunately, this is included in the base repositories for CentOS and Red Hat, so installation is as simple as:

sudo yum install php-fpm

Once the module is installed we need to make a small change to the configuration. PHP-fpm’s configuration is split into a few places, a main configuration file at /etc/php-fpm.conf and the directory /etc/php-fpm.d/ which contains further configuration snippets. We will be looking at the www.conf file in that directory which contains configuration for communicating with web servers. So, let’s edit that file:

sudo nano /etc/php-fpm.d/www.conf

In this example I’m using nano to edit the file, but you can use any editor you prefer. Once the file is open you will need to find the line starting with “listen =” and then change it to match the line below:

listen = /var/run/php-fpm.sock

Once the change has been made, save and exit the file. The PHP-fpm service will then need to be restarted with the following command:

sudo service php-fpm start

Linking PHP-fpm to Nginx

Now that we have the PHP-fpm side configured, now we just need to tell Nginx how to talk to PHP-fpm. Because of the way that Nginx is configured, we can do this by creating a new file in the default.d directory which will be loaded by Nginx with its configuration file.

sudo nano /etc/nginx/default.d/php-fpm.conf

As this is a new file, simply paste in the following commands:

# Execute PHP files with PHP-FPM

location ~ .php$ {

   fastcgi_pass   unix:/var/run/php-fpm.sock;

   fastcgi_index  index.php;

   fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;

   include     fastcgi_params;

}

These directives tell Nginx that if a file ends with the extension “.php” that it needs to pass on to PHP-fpm using the special php-fpm.sock socket file. Save and exit the file once you have completed this task. The next step is to check that Nginx’s configuration doesn’t have any errors, and this is done with the following command:

nginx -t

If no errors are shown, then all is well to perform the final step which is to restart Nginx so that it picks up the new configuration.

sudo service nginx restart

Testing Your PHP Nginx Installation

At this point Nginx should be ready to serve PHP files. An easy way to test this is to create a file to specifically for this task. As Nginx’s default website directory is /usr/share/nginx/html we will create a file there:

sudo nano /usr/share/nginx/html/index.php

In this file paste in the following code:

<?php

phpinfo();

?>

Save and exit the file. You should now be able to navigate to the file index.php on your server in a web browser. If you see the code you have typed in above then something hasn’t worked and Nginx and PHP aren’t communicating properly. On the other hand, if you see a really long page full of details of the PHP configuration on your server then it’s all worked fine, and you are ready to use your PHP based web applications with Nginx on your server.

(Visited 15 times, 1 visits today)