Blog / Technology · May 15, 2021 1

Updating PHP on an Ubuntu Server running NGINX Web Server

All my WordPress websites have been bugging me to update PHP. I am running this website and a few others on a headless server on Amazon Web Services. Let’s update.

First, let’s find out what version I am on. From the Terminal type:

php -version

Turns out, I am running PHP 7.0.33. At the end of this, we want to be on PHP 7.4.

We’re going to install PHP from the ondrej/php repository so let’s make sure to add it to apt:

sudo apt-add-repository ppa:ondrej/php

Press ENTER to continue when Prompted.

Then update the new repository by doing:

sudo apt update

Now I am ready to install PHP 7.4 and its processor and extensions.

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

I got a warning asking about a modified php.ini file. I have always selected the option to Keep the local version currently installed.

Then install the extensions needed and recommended for WordPress:
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

and then these:

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

Now, let’s check the php version again:

php -version

Success but now we need to enable it in our Web Server. NGINX is our web server.

First, let’s configure the PHP Processor we installed:

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

Find cgi.fix_pathinfo and remove the semi-colon and change the 0 to 1. This is for security reasons beyond the scope of this post and my knowledge. I’ve always read its best practice.

Press Ctrl+X to exit and then type Yes and press Enter to commit the changes. Then restart the processor with:

sudo systemctl restart php7.4-fpm

Let’s configure NGINX  to use this processor:

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

(In my instance I had to edit each individual virtual host file for the websites on my server. I Will have to look this up to see if this is best practice or not)

Locate the section that starts with: location ~ .php$ {

Find: fastcgi_pass unix:/run/php/php7.0-fpm.sock and change the 0 to a 4. or add a # in front of it and create a new line with:

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

Press Ctrl+X to exit and then type Yes and press Enter to commit the changes.

Test the changes you made by entering:

sudo nginx -t

If you get syntax is ok and test is succesful  then you are good to go. If you get any errors go back and check for any errors, typos.

Restart NGINX:

sudo systemctl reload nginx

WordPress is no longer bugging me about PHP and, in theory, the website should be running better. 🙂