Upgrading PHP Nginx on Ubuntu 18.04

If you have ever installed WordPress on Ubuntu 18.04 or similar you may have noticed the WordPress warning in site health dashboard for the included default of old PHP 7.2. – “PHP is the programming language used to build and maintain WordPress. Newer versions of PHP are created with increased performance in mind, so you may see a positive effect on your site’s performance. The minimum recommended version of PHP is 7.4.” Furthermore if you do any experimenting with Laravel you may have issues because the latest Laravel 8 is only compatible with PHP7.3 and newer, among other changes.

Although there have been tutorials for the Ondrej PHP PPA, I didn’t see notes for Nginx updates, only that an Nginx separate PPA is recommended. To set this, first install php7.4 and related libraries:

sudo add-apt-repository ppa:ondrej/php
sudo update-alternatives --config php
sudo apt install php7.4 php7.4-mysql php7.4-mbstring php7.4-curl php7.4-imagick php7.4-readline php7.4-xml php7.4-yaml php7.4-zip 
sudo apt install php7.4-fpm

Verify what version you have set, 7.4 should now be the choice:

sudo update-alternatives --config php

Now copy the directory with Nginx configuration, before editing your configuration to user php7.4-fpm:

cp -r /etc/nginx/sites-available/ ~/bkup-sites-available
sudo vim /etc/nginx/sites-available/default # or whatever config file you have

If you aren’t familiar with Vim key commands, it allows editing with “i”, Esc takes you back to command mode where you can run a search-replace with:

:%s/php7.2/php7.4/g

This replaces those php7.2-fpm.sock references with php7.4-fpm.sock. (You could do this with any other text editor manually too).

After saving (“Shift+double tap Z key), a restart of Nginx loads the newer PHP:

sudo service nginx restart

and now you shouldn’t have those warnings or errors in WordPress or Laravel.

Leave a Reply

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

eight + one =