Guide: Installing PHP for Nginx (Ubuntu 16.04 / 17.10)

Last updated on

In this guide we will install and configure PHP-FPM to work with Nginx.

Unlike Apache, Nginx does not contain native PHP processing. For that we have to install PHP-FPM (FastCGI Process Manager). FPM is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

1. Install PHP-FPM

Let’s begin by installing PHP-FPM. It is also recommended to install a helper package php-mysql to allow PHP to function properly with MySQL.

sudo apt-get install php-fpm php-mysql

2. Configure PHP-FPM

If you ever plan to use WordPress or an application that allows uploading of files to the server, you should probably increase the max upload and post file size. The default is 2MB, which is often not enough for uploading images and other media via WordPress. You can skip this step if you don’t plan to upload files larger than 2MB.

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

In nano, press CTRL + W and search for upload_max_filesize and change the value to 64M (for 64 megabytes).

. . .
upload_max_filesize = 64M
. . .

Press CTRL + W again and search for post_max_size and change the value to 64M (for 64 megabytes).

. . .
post_max_size = 64M
. . .

Save file and exit. (Press CTRL + X, press Y and then press ENTER

Now restart PHP-FPM

sudo service php7.0-fpm restart

3. Configure Nginx

We now need to add some PHP directives to our server block. The location of the server block may vary depending on your setup. By default, it is located in /etc/nginx/sites-available/default. However, if you have previously set up custom server blocks for multiple domains (in a previous guide for example), you will need to add the PHP directives to each server block separately. A typical custom server block file location would be /etc/nginx/sites-available/

For the moment, we will assume you are using the default. Edit the file in nano.

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

Search for the line server_name and ensure you domain or IP is listed here. (You can use CTRL + W to search)

        server_name YOUR_DOMAIN_OR_IP_HERE;

Search for the line index index.html index.htm index.nginx-debian.html; and add index.php before index.html, so it should look like this:

index index.php index.html index.htm index.nginx-debian.html;

Scroll down to the line #location ~ \.php$ {. You will need to remove the # signs before the lines marked in red below.

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #       # With php7.0-cgi alone:
        #       fastcgi_pass;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;

Once you’ve made the necessary changes, save and close (Press CTRL + X, then press y and ENTER to confirm save)

Now check the config file to make sure there are no syntax errors:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If no errors, you can reload the Nginx config.

sudo service nginx reload

4. Test PHP

To see if PHP is working correctly, let’s a create a new PHP file called info.php in the web server’s root directory. By default, this is located in /var/www/html/, or if you set up multiple domains in a previous guide, it may be located in /var/www/

sudo nano /var/www/html/info.php

Type or paste the following code into the new file:


Save and close (Press CTRL + X, then press y and ENTER to confirm save)

You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php: http://your_domain_or_IP/info.php

phpinfo() outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version and server information.


You have now successfully installed PHP-FPM for Nginx.

Make sure to delete info.php as it contains information about the web server that could be useful to attackers.

sudo rm /var/www/html/info.php

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

Your email address will not be published. Required fields are marked *