How to Install PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa

PHP is one of the most popular and widely used server-side programming languages to date on the market. According to PHP website statistics, in 2021, it will be used by 79.1% of all known websites according to W3Tech, with version 7.0 series making up the bulk with 65.6% market share. Many popular CMS and frameworks such as WordPress, Magento, and Laravel development involve most PHP frameworks.

The latest PHP release to date is the 8 series. PHP 8 is a significant update of the PHP language. It contains many new features and optimizations, including named arguments, union types, attributes, constructor property promotion, match expression, null safe operator, JIT, improvements in the type system, error handling, and consistency.

How to Install PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa

What is PHP?

PHP “recursive acronym for PHP: Hypertext Preprocessor” is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. What distinguishes PHP from client-side JavaScript is that the code is executed on the server, generating HTML, then sent to the client. The client would receive the results of running that script but would not know the underlying code. You can even configure your webserver to process all your HTML files with PHP, and then there’s no way that users can tell what you have up your sleeve.

Our guide will install PHP 8 on Ubuntu 20.04 LTS Focal with popular web application software options Apache and Nginx.

Advertisement

Prerequisites

  • Recommended OS: Ubuntu 20.04
  • User account: A user account with sudo or root access.

Update Operating System

Update your Ubuntu operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@ubuntu ~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on adding a User to Sudoers on Ubuntu.

To use the root account, use the following command with the root password to log in.

su

Import Ondřej Surý PHP PPA

Ubuntu currently supports PHP 7.4. However, they are behind on the latest stable versions in the 7.4 series and do not have PHP 8 added to their official repository. You will need to install the official PPA from Ondřej Surý, the lead developer on PHP and Debian, and maintain Ubuntu and Debian packages. Ondřej’s repository is continuously updated monthly with the latest stable version in PHP 7.4 and 8.

Next, install the prerequisites and Ondřej Surý PPA.

sudo apt install software-properties-common && sudo add-apt-repository ppa:ondrej/php -y

You will get a message with the option to leave or hit enter, and press enter key as this is a well-known safe repository. From now on, all your PHP updates will come from this repository, and you can now proceed to install PHP 8.0.

Once done, it is a good idea to refresh your APT repositories as the PPA may bring additional upgrades to existing dependencies.

sudo apt update

Next, upgrade any packages that require it.

sudo apt upgrade -y
Advertisement

Install PHP 8.0 with Apache Option

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Install Apache Module

To install PHP 8 as an Apache module, enter the following command.

sudo apt update && sudo apt install php8.0 libapache2-mod-php8.0

Once installation is complete, restart your Apache server for the new PHP module to be loaded.

sudo systemctl restart apache2

Install Apache with PHP-FPM

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

To install PHP-FPM with the following commands.

sudo apt update && sudo apt install php8.0-fpm libapache2-mod-fcgid

Note, by default, PHP-FPM is not enabled for Apache. You must enable it by the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.0-fpm

Lastly, restart Apache.

systemctl restart apache2

Verify that PHP-FPM is working:

sudo systemctl status php8.0-fpm

Example output:

How to Install PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa

Install PHP 8.0 with Nginx Option

Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM “fastCGI process manager” to handle the PHP files.

First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required. Run the following commands and hit “Y” when prompted.

sudo apt install php8.0-fpm php8.0-cli -y

Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.

sudo systemctl status php8.0-fpm
How to Install PHP 8.0 on Ubuntu 20.04 LTS Focal Fossa

You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.

Below, example for all server blocks that process PHP files that need the location ~ .php$ added.

server {
 # … some other code
 location ~ .php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php8.0-fpm.sock;
 }

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx service for installation to be complete.

sudo systemctl restart nginx
Advertisement

Comments and Conclusion

PHP 8 is an improvement over 7.4, with speed an exciting factor amongst most features. However, you must be aware that it’s still not considered “mature software” compared to PHP 7.4. You may find that many of your favorite software like WordPress or Plugins/Themes for CMS software may conflict.

Do some research, prepare, and have 7.4 installed and ready to replace if anything goes wrong when making the switch, as 7.4 is still actively developed and packages pushed simultaneously along with the 8 packages.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x