With older PHP versions of 7.1 or older reaching end of life (EOL), and PHP 7.4 in full production, if you haven’t already its time that you install and configure PHP 7.4 and any required extensions, this article will walk you through the process of setting up PHP 7.4 on your Ubuntu based Nginx server, as well as installing the required and recommended PHP Extensions for WordPress website.
PHP 7.4 offers security and performance improvements over previous versions. Although PHP 7.3 is still considered to be current, I also have a guide on setting this up – Click here for how to install PHP 7.3 instead
In this article I am working on a system that was already configured and running PHP 7.3.
Before you begin, the steps in this article assume the following
- You are running either Ubuntu 16.04 or 18.04
- You have sudo or root access
- Your server runs Nginx.
1. Add PPA repository and update package lists.
Run the following commands to add the ppa:ondrej/php PPA repository to your system and update the available package list. This PPA contains the latest PHP packages and extensions for Ubuntu systems and is my recommended choice, even though there are a number of possible sources for these packages.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
This should only take a minute to complete, after which you can install and configure the new PHP package and any required extensions.
2. Install PHP 7.3 and any extensions that you need.
The next commands will install php 7.4 to your system and both the required and recommended extensions for running a WordPress website using the latest core branch.
To install php-7.4 and php7.4-fpm;
sudo apt install php7.4 php7.4-fpm
To ensure that you have all of the required & recommended extensions for running WordPress websites (these extensions should also be sufficient for most other PHP based CMS platforms or functions), run the following;
sudo apt install php7.4-common php7.4-mysql php7.4-curl php7.4-json php7.4-mbstring php7.4-xml php7.4-zip php7.4-gd php7.4-soap php7.4-ssh2 php7.4-tokenizer
A list of the required and recommended PHP extensions for WordPress
WordPress have a published list of the required and recommended PHP extensions for running WordPress optimally in the WordPress Server Environment Documentation which is available in full by clicking here, but for your reference, below is the list as of November 11th 2020, with WordPress version 5.5.3 being the latest and PHP 7.4 being the recommended version. Many of them are included in the php7.4-common package, which is why they havent been explicitly added in the earlier commands.
curl – Performs remote request operations.
dom – Used to validate Text Widget content and to automatically configuring IIS7+.
exif – Works with metadata stored in images.
fileinfo – Used to detect mimetype of file uploads.
hash – Used for hashing, including passwords and update packages.
json – Used for communications with other servers.
mbstring – Used to properly handle UTF8 text.
mysqli – Connects to MySQL for database interactions.
libsodium – Validates Signatures and provides securely random bytes.
openssl – Permits SSL-based connections to other hosts.
pcre – Increases performance of pattern matching in code searches.
imagick – Provides better image quality for media uploads. See WP_Image_Editor is incoming! for details. Smarter image resizing (for smaller images) and PDF thumbnail support, when Ghost Script is also available.
xml – Used for XML parsing, such as from a third-party site.
zip – Used for decompressing Plugins, Themes, and WordPress update packages.
filter – Used for securely filtering user input.
gd – If Imagick isn’t installed, the GD Graphics Library is used as a functionally limited fallback for image manipulation.
iconv – Used to convert between character sets.
mcrypt – Generates random bytes when libsodium and /dev/urandom aren’t available.
simplexml – Used for XML parsing.
xmlreader – Used for XML parsing.
zlib – Gzip compression and decompression.
3. Configure PHP 7.4 in your Nginx configuration.
Next, you will need to update your sites Nginx configuration to utilize the recently installed PHP 7.4, if you already had an older php version installed and configured, this will just involve updating a little bit of code within your Nginx configuration.
If you are using the default site block and only running a single site on the service, which would put your sites document root at
/var/www/html/ then you will need to edit the default configuration at
/etc/nginx/sites-available/default which you can do by issuing the following command.
sudo nano /etc/nginx/sites-available/default
If you have multiple sites and configurations, and want to apply PHP 7.4 to them individually instead of adding it as the default, then edit the appropriate configuration file within the same directory as the “default” file, or if you are using a custom built site block, wherever your sites configuration may be set up.
Once you have the file open using nano or your choice of text editor, next you need to add or amend the following code within the server configuration section to configure it for use with PHP 7.4.
Next you need to restart the php7.4-fpm service, as well as check the Nginx configuration for problems before restarting or reloading Nginx. The changes will not take affect until this is done.
To restart php7.4-fpm –
sudo systemctl restart php7.4-fpm
To check that the Nginx configuration changes have been made correctly –
sudo nginx -t
As long as the Nginx configuration test comes back as OK, then you can restart or reload Nginx using the following commands;
sudo systemctl restart nginx
sudo systemctl reload nginx
You can check the status of these service also by running the following command to make sure they successfully restarted on your server. After doing this, PHP 7.4 will be running any sites it was configured for;
sudo systemctl status php7.4-fpm nginx