Setup Nginx Web Servers with PHP Support on Ubuntu Servers

laptop 4662049 640

If you’re going to be developing any PHP application, you’re mostly going to need PHP server scripts installed. PHP is an open source server scripting language use for creating dynamic, powerful web applications and websites.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

This brief tutorial is going to show students and new users how to setup Nginx web server environment with PHP support. Most popular content management systems like WordPress, Joomla, Drupal use PHP.

PHP is a big deal!

When you’re ready to set up Nginx environment with PHP support, follow the steps below:

Step 1: Install Nginx Web Server

sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always startup when the server boots up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Step 2: Install PHP-FPM  and Related Modules

After installing Nginx above, run the commands below to install PHP-FPM and related PHP modules. PHP-FPM is a version for Nginx webserver while PHP is Apache2. There are many PHP modules that perform different functions. however, there are some important ones that are always needed when developing PHP based websites.

sudo apt-get install php-fpm php-mcrypt php-cli php-mysql php-gd php-imagick php-recode php-tidy php-xmlrpc

The line above will allow PHP to function with many popular PHP based websites and applications.

Step 3: Configure Nginx PHP Settings

Now that Nginx and PHP are installed, you may want to configure Nginx to use PHP properly. The default Nginx PHP configuration file is located at /etc/php/7.x/fpm/php.ini

The X in the location will be 0 or 1 depending on the php version installed.

Open PHP Nginx configuration file by running the commands below

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

Then edit the file to suit your environments. Some important lines to consider:

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 64M

Next, open the Nginx site configuration file. by default it’s stored at /etc/nginx/sites-available/default

If you have a custom file, then edit it to enable Nginx PHP support. Run the commands below to open Nginx default site configuration file

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

Then make uncomment the highlighted lines below to enable Nginx PHP support.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;       

     # pass PHP scripts to FastCGI server
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

Restart Nginx and PHP-FPM services

sudo systemctl restart nginx.service
systemctl restart php7.1-fpm.service

Step 4: Test PHP-FPM Setup

At this point, Nginx and PHP-FPM should be installed and ready. to test your Nginx PHP settings, create a blank file with the line below:

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

Then add the line in the file and save.

<?php phpinfo( ); ?>

Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php


You should see something similar to the image below. if you do, then you’re all good!

php-fpm on apache2


Congratulations! You’ve successfully installed and configured Nginx and PHP on Ubuntu servers

You may also like the post below:


  1. it tells me the connection timed out or 404 not found

  2. Just to keep this up-to-date. The current version of php-fpm for Ubuntu 19.10 is 7.3 as of 21 Apr 2020. Great article and still very relevent for current versions of Ubuntu.

  3. $ sudo apt-get install php-fpm php-mcrypt php-cli php-mysql

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package php-mcrypt is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘php-mcrypt’ has no installation candidate

    $ php-gd php-imagick php-recode php-tidy php-xmlrpc

    php-gd: command not found

    Not getting anywhere ….

    1. do not include the php-mcrypt from the sudo command:

      sudo apt-get install php-fpm php-cli php-mysql php-gd php-imagick php-recode php-tidy php-xmlrpc

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.