How to Install PHP 8.0 on Ubuntu 20.04

Tutorial Dec 15, 2020

PHP is one of the most famous and widely used server-side programming languages out there. It powers most of the famous content management platforms, such as WordPress, Magento and Drupal.

PHP has come a long way since its inception, and now at the time of writing this, the stable version available for us to use is 8.0. Released on November 26 of 2020, and being a major release, it comes with numerous improvements and new features such as, Union Types, Named Arguments, Match Expressions, Attributes, Constructor Property, Promotion, Nullsafe Operator, Weak Maps, Just In Time Compilation and much more. Click here to learn more.

This tutorial will walk you through the installation process of PHP 8.0 on an Ubuntu 20.04.1 system. Fire up a virtual machine and let us get started!

Note: At the time of writing this, Ubuntu's official repositories doesn't offer PHP 8.0. So, unless you know what you're doing, it's best not to use this version on production servers at the moment.

1. ADDING Ondřej Surý's PPA REPOSITORY

Ubuntu's official repositories doesn't offer us PHP 8.0 yet. So, we will be using Ondrej Surý's PPA repository. It offers us access to multiple versions of PHP and extensions for us to use, including PHP 8.0.

To begin, run the following commands to take care of some dependencies,

sudo apt update
sudo apt install -y ca-certificates apt-transport-https software-properties-common

Then we can add Ondrej Surý's PPA repository,

sudo add-apt-repository -y ppa:ondrej/php

After that is done, we can proceed to install PHP 8.0 alongside an apache or nginx server.

2. PHP 8.0 WITH APACHE

First things first, we need to install apache.

sudo apt -y install apache2

To use PHP alongside apache in a non-FastCGI configuration, install php8.0-common and the php8.0 module for apache with the command,

sudo apt install -y php8.0-common libapache2-mod-php8.0

Or if you prefer going the FastCGI way, install php8.0-fpm, and the fcgid module for apache using the command,

sudo apt install -y php8.0-fpm libapache2-mod-fcgid
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

After that, we can restart the apache server,

sudo systemctl restart apache2

3. PHP 8.0 WITH NGINX

We will be using php8.0-fpm with nginx. So, lets go ahead and install them both.

sudo apt install -y php8.0-fpm nginx

Next, we will create a new nginx server configuration, one that can process .php files.

sudo vim /etc/nginx/sites-available/my_server

Enter or copy and paste the following into that file, and save it.

server {
    listen 80;
    root /var/www/html;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }
}

Next, we need to activate our newly created configuration by linking it to nginx’s sites-enabled directory

sudo ln -s /etc/nginx/sites-available/my_server /etc/nginx/sites-enabled/

Disable nginx’s default server configuration file by deleting the symlink inside the sites-enabled directory.

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

After that, we need to restart the nginx service,

sudo systemctl reload nginx

4. TESTING

We can verify our installed php version by running either php -v or pvp8.0 -v,

php -v
PHP 8.0.0 (cli) (built: Nov 27 2020 12:26:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies

To check if the server is handling php files successfully, create a file named info.php at /var/www/html/

sudo vim /var/www/html/info.php

After entering the following into the file, save it.

<?php
    phpinfo();
?>

And then visit http://localhost/info.php or http://your-server-ip/info.php on a browser.

phpinfo
phpinfo()

Tags

Lime

Simple, like quantum physics.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.