How to upgrade PHP to Version 7.4 for WordPress running in Ubuntu 18.04

Ashok Raja T
Technology Specialist
August 1, 2021
Rate this article
Views    11452

Although WordPress works with older versions of PHP, the official minimum recommended version of PHP for WordPress is 7.4. In this article let us see, how to upgrade the version of PHP to Version 7.4 for WordPress sites running behind Nginx on a Ubuntu 18.04 server.

In Ubuntu 18.04 server, the maximum version of PHP available in the official repo is version 7.2. If you are already in Ubuntu 20.04, this may not be an issue as version 7.4 is already available in the official repo.

WordPress Warning

warning

PHP Version In Server

01_initial_server

Upgrade PHP To Version 7.4

Execute the below commands in the terminal to add the external repo that contains the latest version of PHP, and to install PHP 7.4 FPM and related PHP modules.

# Update the repos and upgrade the packages
sudo apt update
sudo apt upgrade

# Add external repo from where 
# the latest version of PHP can be installed
sudo add-apt-repository ppa:ondrej/php
sudo apt update

# Add PHP FPM and PHP modules required for WordPress
sudo apt install php7.4-fpm php7.4-common php7.4-mysql \
php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd \
php7.4-imagick php7.4-cli php7.4-mbstring \
php7.4-mbstring php7.4-opcache php7.4-readline \
php7.4-soap php7.4-json php7.4-zip

Nginx Config Change

Change the line fastcgi_pass unix:/run/php/php7.2-fpm.sock; in the Nginx configuration (located in /etc/nginx/sites-available/[your_site_config]) to fastcgi_pass unix:/run/php/php7.4-fpm.sock;

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

Settings in www.conf

Ensure that, in the file “/etc/php/7.4/fpm/pool.d/www.conf“, the group and user account are pointing to www-data by validating the below params.

user = www-data 
group = www-data 
listen.owner = www-data
listen.group = www-data

Changes in php.ini file

Edit the php.ini file by executing the command sudo nano /etc/php/7.4/fpm/php.ini to modify the file upload limit and max memory allocation.

php.ini file changes are not mandatory. Perform the below change only if necessary.
upload_max_filesize = 8M 
post_max_size = 8M 
memory_limit = 256M

Re-start PHP-FPM & Reload Nginx Config

Follow the below steps to restart the PHP-FPM service for enabling the changes that are performed in the previous steps.

# Validate the changed settings 
sudo php-fpm7.4 -t

# Restart PHP
sudo systemctl restart php7.4-fpm.service

# Check the status of PHP service
sudo systemctl status php7.4-fpm.service

# Ensure that the service is auto-starting on re-boot
sudo systemctl enable php7.4-fpm.service

Execute the below commands to re-load Nginx configuration.

# Validate Nginx Config
sudo nginx -t

# Re-load Nginix Config
sudo nginx -s reload
If you are looking for ways to protect your Nginx Server, check out my earlier article here.

Remove PHP 7.2

All PHP 7.2 modules start with the name php7.2. Those list of packages can be viewed by executing the command apt list --installed php7.2*. As we have upgraded the PHP, remove the old packages by executing the below commands in the terminal.

sudo apt-get purge php7.2*
sudo apt-get autoclean
sudo apt-get autoremove

Set the Default Version of PHP

Although we require version 7.4, the repo that we have added would have installed PHP 8 as well. Core WordPress works without any issues with PHP 8.0. At the same time, ensure that all WordPress plugins that you have currently installed are also compatible with PHP 8.0. Till that time, stop and disable the services related to 8.0 and also change the default version of PHP to 7.4. Follow the below steps to make those changes.

sudo update-alternatives --set php /usr/bin/php7.4
sudo systemctl stop php8.0-fpm.service
sudo systemctl disable php8.0-fpm.service

Screenshots From Server & WordPress

Output from Server
php8_to_7.4

OutPut From WordPress
php_info_after_update

Subscribe To Our Newsletter
Loading

Leave a comment