Hosting 101 – How To Configure PHP To Work With Nginx On Debian and Ubuntu

19th May, 2016 by

In the previous Hosting 101 article we looked at using the Nginx web server as a replacement for the usual Apache web server for a web hosting server. The default installation of Nginx only has it configured to serve static files, and if you tried browsing to a PHP file on your server in a web browser you would have been served the PHP file itself rather than the web page that would normally be output by the script. In this article we will be looking at getting PHP5 configured to work with Nginx on your web server.

How to Install PHP5-FPM: The Interface Between Nginx and PHP

The first step is to install php-fpm, which manages the interface between Nginx and PHP.

sudo apt-get install php5-fpm

Next, you will need to configure the php5-fpm package for Nginx to use it. The configuration is stored in the /etc/php5/fpm/php.ini file. Note that this is a full PHP configuration file, so if you are looking at migrating sites away from a PHP/Apache combination to a PHP/Nginx setup then this php.ini file would also need any changes you’ve made to your /etc/php5/apache2/php.ini configuration file for your sites moving over to it. For now we are just going to look at changes recommended when using Nginx.

sudo nano /etc/php5/fpm/php.ini

I’m using nano in my example, but other text editors are available. Feel free to use your favorite. Now you need to find the line with cgi.fix_pathinfo and make sure it reads as:

cgi.fix_pathinfo=0

Save and exit this file. This line informs PHP of what to do if the file requested isn’t found: with the setting of 1 it attempts to find a close match file and serve that; with 0 it responds with a file not found error. As this can be used as a way of accessing files that a web browser shouldn’t access on dynamic websites, it’s best to set this to 0. Next, you need to edit the configuration for how PHP-fpm interfaces with the web server:

sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line that starts with “listen =” and make sure it reads as:

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

Save and exit that file. The last step is to configure Nginx to use PHP files.

sudo nano /etc/nginx/sites-available/default

Find the section that starts with “#location ~ .php$ {” and remove the comments so that it looks as below:

        location ~ .php$ {

                include snippets/fastcgi-php.conf;

        #

        #    # With php5-cgi alone:

        #    fastcgi_pass 127.0.0.1:9000;

        #    # With php5-fpm:

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

        }

Save and exit the file. With this complete, we can restart both php-fpm and Nginx to get PHP support working:

sudo service php5-fpm restart

sudo service nginx restart

Any PHP files requested through Nginx will now be processed by PHP and the relevant output returned.

Something to note here is that the changes in /etc/nginx/sites-available/default are only applicable to the default configuration, so those PHP configuration lines would also need to be duplicated in the configuration files for any other virtual servers that you are using with Nginx on the server that are likely to use PHP.

How To Perform A PHP Test Run

The final thing to do is test that it’s all working, so navigate to your web root which is in /etc/nginx/sites-available/default which would likely be /var/www/html but best to check as some distributions move it around. Once in that directory create a new file:

sudo nano index.php

Then paste in the following lines of code:

<?php

phpinfo();

?>

Save it, the navigate to index.php on your server in a web browser, which should load that file which will display a very long page listing all of PHP’s configuration settings. Once you’ve confirmed it’s working it’s best to delete the file as it does provide information about server configuration that may be useful to a hacker who may stumble upon and poke at your server.

(Visited 29 times, 1 visits today)