How to upgrade the PHP version on my Nginx Ubuntu Droplet

Posted January 25, 2021 2.7k views

I’ve been going over the community questions and I’ve noticed one constant theme. Every now and then a question regarding the upgrade of a PHP version appears.

As such I’ve decided to create this question so it can be easily found and it’s universal, at least for Ubuntu-based droplets using Nginx.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

The first thing I want to mention is, you can’t directly upgrade a PHP version from let’s say 7.1 to 7.4. Having said that you can install a new one and start using it.

Firstly, to be able to install a certain PHP version, in this case, 742 you’ll need to add a certain repository to your system

sudo apt-add-repository ppa:ondrej/php

Update the repository index.

sudo apt update

Install PHP 7.4 with the below command.

sudo apt install -y php7.4 php7.4-cli php7.4-common php7.4-fpm

To install extensions run

sudo apt install -y php7.4-mysql php7.4-dom php7.4-simplexml php7.4-ssh2 php7.4-xml php7.4-xmlreader php7.4-curl  php7.4-exif  php7.4-ftp php7.4-gd  php7.4-iconv php7.4-imagick php7.4-json  php7.4-mbstring php7.4-posix php7.4-sockets php7.4-tokenizer

The above is the required extensions for a WordPress installation that’s why I’m posting them. Having said that it’s good to have these extensions as well.

sudo apt install -y php7.4-mysqli php7.4-pdo  php7.4-sqlite3 php7.4-ctype php7.4-fileinfo php7.4-zip php7.4-exif

That’s it, you now have your new PHP version installed.

Now we need to make it a default for your Droplet and Application.

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

Find: cgi.fix_pathinfo. Remove semi-colon and set 0


Save the file, exit, and execute

sudo systemctl restart php7.4-fpm

Next configure Nginx to use the new PHP we installed

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

Find the block

location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock; ---- Remove this line by commenting '#'

fastcgi_pass unix:/run/php/php7.4-fpm.sock;

Save the file, exit, and execute

sudo nginx -t
sudo systemctl reload nginx

You can now test php by using phpinfo() function or use php -v command into the terminal.

  • Hello,

    I followed all these steps in order to update my wordpress site from 7.2 to 7.4, but the php -v output is still showing 7.4, while my Wordpress Site Health tool is showing me 7.2. I’ve gone over the steps numerous times and made sure all were executed properly. What are my options to point wordpress/nginx to the operational 7.4 php?

    • Hi @ic891,

      In your Website’s Nginx config file, can you add the following block:

          location ~* \.php$ {
              # With php-fpm unix sockets
              fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
              include         fastcgi_params;
              fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
              fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

      and let me know how it goes?

      • I added this block to the config file, rebooted the server and when I run php -v I get this output:

        PHP 7.4.22 (cli) (built: Jul 30 2021 13:07:59) ( NTS )
        Copyright (c) The PHP Group
        Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.22, Copyright (c), by Zend Technologies

        When I go to my wordpress site health it tells me I’m still running PHP 7.2.

        • HI @ic891,

          Can you please paste the Nginx config? Both where you added the .PHP location blog and your website’s config?

          •   GNU nano 2.9.3                          /etc/nginx/sites-available/default                                     
            # You should look at the following URL's in order to grasp a solid understanding
            # of Nginx configuration files in order to fully unleash the power of Nginx.
            # In most cases, administrators will remove this file from sites-enabled/ and
            # leave it as reference inside of sites-available where it will continue to be
            # updated by the nginx packaging team.
            # This file will automatically load configuration files provided by other
            # applications, such as Drupal or Wordpress. These applications will be made
            # available underneath a path with that package name, such as /drupal8.
            # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
            # Default server configuration
            server {
                    listen 80 default_server;
                    listen [::]:80 default_server;
                    # SSL configuration
                    # listen 443 ssl default_server;
                    # listen [::]:443 ssl default_server;
                    # Note: You should disable gzip for SSL traffic.
                    # See:
                    # Read up on ssl_ciphers to ensure a secure configuration.
                    # See:
                    # Self signed certs generated by the ssl-cert package
                    # Don't use them in a production server!
                    # include snippets/snakeoil.conf;
                    root /var/www/html;
                    # Add index.php to the list if you are using PHP
                    index index.html index.htm index.nginx-debian.html;
                    server_name _;
                                                            [ Read 97 lines ]
            ^G Get Help     ^O Write Out    ^W Where Is     ^K Cut Text     ^J Justify      ^C Cur Pos      M-U Undo
            ^X Exit         ^R Read File    ^\ Replace      ^U Uncut Text   ^T To Spell     ^_ Go To Line   M-E Redo