Downgrade PHP 7.2 to PHP 7.1 with Nginx Support on Ubuntu 16.04 / 17.10 and 18.04

If you’re running PHP applications that only support PHP versions up to 7.1 and you upgrade your systems to 7.2, you may run into issues. On the other hand, if you upgrade to Ubuntu 18.04 which only has PHP 7.2 packages in its default repositories, all your applications that support PHP 7.1 and below may fail.

Some applications do not yet support PHP 7.2. If you find yourself running PHP 7.2 for apps who don’t yet support it, you’ll probably want to downgrade to a supported version of PHP.

This brief tutorial shows students and new users how to run Ubuntu 18.04 but still install PHP 7.1 to support your apps. This post should be quick and easy. The good thing about this is you can install PHP 7.1 along with PHP 7.2 and have multiple applications with different PHP needs.

Some apps can be configured to support PHP 7.2 while others support PHP 7.1

For this post, we’re going to be supporting PHP 7.1 only on Ubuntu

If you can’t get PHP 7.1-FPM packages on Ubuntu by default, you’ll want to add this third party repository to your system. the repository contains multiple versions of PHP packages.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

After adding the repository, you can now go and install PHP 7.1-FPM and related modules.

sudo apt install php7.1-fpm
sudo apt install php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip

The main configuration file for PHP 7.1 with Nginx support can be found at

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

In the file above is where you configure PHP related settings with Nginx support. Some cool settings are:

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 64M
cgi.fix_pathinfo = 0
max_execution_time = 30
display_errors = Off
max_input_vars = 1500
date.timezone = America/Chicago

After making those changes save the file and exit.

Unlike Apache2, you can’t run commands to disable Nginx PHP support. you can only include the PHP configuration file with your Nginx settings. So if you want to run PHP 7.1 with your Nginx configurations on Ubuntu, simply include the line as shown below:

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

    client_max_body_size 100M;

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

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass        unix:/var/run/php/php7.1-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }

}

If you want to enable PHP 7.2 instead, change the line to the one below.

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }

Restart Nginx and PHP -FPM 7.1 for the changes to apply by running the commands below.

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

That’ it! Now PHP 7.1 should be the default PHP processor for your app.

You may also like the post below: