Update to PHP 7.4 on Ubuntu 18.04 on Digital Ocean for WordPress

|

|

I kept seeing a notice on WordPress Site Health about PHP needing an update. On Ubuntu, it was showing 7.4 as the PHP version but not in WordPress. I’m writing this post to show the steps I took and it will be a resource I can find quickly in the future.

At root, check for updates to Ubuntu and all repositories. Choose Y when asked.

$ sudo apt-get update && apt-get upgrade

At root, check for installed program updates and it will automatically install those that need updates. Choose Y when asked.

$ sudo apt-get dist-upgrade

At root, clean up any packages not being used. Choose Y when asked.

$ sudo apt-get autoremove

At root, save a list of the currently installed PHP settings to a text file that you can look at later with cat packages-first.txt – you will need it later to know which extensions to install for PHP 7.4.

$ sudo dpkg -l | grep php | tee packages-first.txt

Now go into your Digital Ocean account, turn off the Droplet you are going to update PHP on, create a snapshot, then turn the droplet back on.

At root, add repository for latest PHP version.

$ sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
$ sudo apt-get update

At root, install the basic configuration of PHP 7.4.

$ sudo apt install php7.4 php7.4-common php7.4-cli

At root, install the extensions you need. Here are few from the article and you will need to install the extension you have in PHP 7.3 for 7.4 that you can find in packages-first.txt that you made above.

$ sudo apt install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-intl php7.4-mbstring php7.4-mysql php7.4-readline php7.4-xml php7.4-zip

At root, create a second file to see the currently installed extensions so you can compare it with the first file. You can find both of these in the root of your Droplet if you use SFTP access. Install PHP 7.4 extensions that you need.

$ sudo dpkg -l | grep php | tee packages-second.txt

At root, for web server integration with Apache.

$ sudo apt install php7.4-fpm
$ sudo a2enconf php7.4-fpm # For Apache only

At root, remove all versions of PHP except for the most recent version 7.3. I kept it while going through these steps as a safety blanket until I was sure that I had installed PHP 7.4 extensions of all the PHP 7.3 extensions found in packages-first.txt. I removed 7.0, 7.1, and 7.2. Add the names of the extensions for each older PHP version that are still installed to purge them too, found in packages-second.txt. You don’t want to purge the file names without a number as those are part of the server.

$ sudo apt purge php7.0 php7.0-common # Change 7.0 with all versions you want to purge.

At root, to disable PHP 7.3 and enable PHP 7.4 so that WordPress will see the correct version. Even if php -v at root would show the latest version, WordPress would not. The bottom answer from this website helped me here.

$ sudo a2dismod php7.3
$ sudo a2enmod php7.4
$ sudo systemctl restart apache2

I used these articles as reference:

Comments

12 responses to “Update to PHP 7.4 on Ubuntu 18.04 on Digital Ocean for WordPress”

  1. Khürt Williams Avatar

    Yep. These were the steps I followed a few days ago.

  2. Steve Avatar
    Steve

    Thank you for this article — it worked perfectly for me!

    1. Daniel Brinneman Avatar

      You’re welcome, Steve! Thank you for dropping by. Here’s my latest update for Ubuntu 20.04.

  3. Jon D Avatar
    Jon D

    I tried doing the above and it pulled in PHP 8. I ran

    sudo apt-get purge php8.*
    sudo apt-get autoclean
    sudo apt-get autoremove

    sudo systemctl restart apache2

    1. Daniel Brinneman Avatar

      Jon, thanks for dropping by.

  4. Jan Avatar
    Jan

    Hi

    Thanks for your explanation. I get this message:

    Cannot add PPA: ‘ppa:~ondrej/ubuntu/php’.
    ERROR: ‘~ondrej’ user or team does not exist.

    Was this updated?

    Thanks!

    1. Daniel Brinneman Avatar

      Thank you for dropping by, Jan. The command I wrote should work as I used it for updating to the latest version of Ubuntu most recently. If you are receiving error messages in the terminal, look for their explanations in a web search.

  5. […] followed my previous tutorial until I had to use these additional commands to continue. There was a DOS installation that I chose […]

  6. Jamel Avatar
    Jamel

    Life saver, this really helped me update my WordPress to get all of my plugins working correctly after updating all of them.

    1. Daniel Brinneman Avatar

      Glad it was helpful to you, Jamel.

  7. irashkin Avatar
    irashkin

    Thanks so much, best instructions I’ve found by far! I too had to follow Jon’s recommendation to remove 8.*, so thanks to you too, Jon D!

    1. Daniel Brinneman Avatar

      You’re welcome! Thanks for dropping by.

Leave a Reply to Jon D Cancel reply

%d bloggers like this: