By default, Ubuntu 20.04 comes with PHP 7.4 pre-installed. Upgrading to the latest version of PHP is simple and can be done within a few minutes.
Step 1 — Check for updates
Before we start installing PHP8 on our server we need to first update and upgrade our system. You can do so with the following command:
$ sudo apt update && sudo apt upgrade -y
Step 2 — Install add the Ondrej PPA repository
Copy and paste the command below. You will be prompted to press enter to continue with the install.
$
sudo add-apt-repository ppa:ondrej/php
Step 3 — Install PHP 8 with Apache
Assuming you are using (or plan on using) Apache Web Server then you will continue with this step. Otherwise, scroll down to see how to install PHP 8 with NGINX.
Let’s install PHP 8 with Apache module:
$ sudo apt install php8.0 libapache2-mod-php8.0
You will be asked to continue download, press ‘y’ then enter.
Now we need to restart Apache for our changes to take effect.
$ sudo systemctl restart apache2
If your new Apache Web Server needs PHP-FPM, use the code below to install PHP-FPM.
$ sudo apt install php8.0-fpm libapache2-mod-fcgid
Now we must enable PHP-FPM since this mod is not enabled by default install. To do so, run the following command:
$ sudo a2enmod proxy_fcgi setenvif
$ sudo a2enconf php8.0-fpm
Now restart Apache to reflect changes:
$ sudo systemctl restart apache2
Step 3 .2— Install PHP 8 with NGINX
If you choose NGINX as your web server, you will need to install PHP-FPM to process your php files.
$ sudo apt install php8.0-fpm
The PHP-FPM should start automatically with NGINX. You can verify PHP-FPM is running with the command:
$ sudo systemctl status php8.0-fpm
In order for NGINX to process PHP files, you will need to update the server block.
As with any server change, restart the process for the changes to take effect.
sudo systemctl restart nginx
Step 4 — Install PHP 8 Extensions
These will ‘extend’ the functionality of PHP. Some software scripts will require certain extensions to operate properly, you can do so by installing them here.
You can browse the list of available PHP extensions via the command below.
$ php -m
To install an extension, simply use this command, replacing ‘extension-name’ with the name of the extension you intend to install.
$ sudo apt install php8.0-[extension-name]
Step 5 — Verify PHP 8 is installed correctly
Now we need to check our work and verify that PHP 8 is installed and running properly.
To do so, simply run the command:
$ php -v
Which should output something like this:
If you are seeing the output above then congratulations, you now have an operating Ubuntu 20.04 system with Apache/NGINX Web server running PHP 8.