How to Install PHP 8.0 on Ubuntu 20.04

PHP is a very popular server-side programming language used for creating dynamic websites and apps. It is free and open-source. PHP 8 brings many optimizations and powerful features to the language; including the Just-in-time (JIT) compiler, attributes, named arguments, and more

In this tutorial, you will learn how to install PHP 8.0 on Ubuntu 20.04, and configure it to work with Apache and NGINX web servers.

Prerequisites

  • An NGINX or Apache webserver running on Ubuntu 20.04
  • A user with sudo privileges

Add the ondre/php repository

At the time of this post, PHP 7.4 is the latest version available in the default Ubuntu package repository. 

To install PHP 8, you need to add the ondre/php repository. There are multiple PHP versions and extensions available in this private package archive.

Run the following command to add the ondre/php repository on Ubuntu 20.04.

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

Next, run the command below to check for package updates.

$ sudo apt update

Install PHP 8 for Apache

After adding the ondre/php repository and checking for package updates, you may run the next command to install PHP 8 on Ubuntu 20.04.

$ sudo apt install php8.0

When prompted, enter y to continue.

Install PHP 8.0 for Apache
Install PHP 8.0 for Apache

After the installation completes successfully, you can check the version of PHP with the next command.

$ php -v
Check PHP version
Check PHP version

Test PHP 8 on Apache

To confirm if PHP is working correctly with Apache, create an index.php file in the default document root as follows.

$ sudo nano var/www/html/index.php

Copy and paste the sample PHP code below into the text editor.

<?php
phpinfo();
?>

Now, open a web browser and go to serverIP/index.php. Replace serverIP with your actual server IP. If you see the PHP 8.0.3 info page, then everything is in order.

PHP Version 8.0.3 page on Apache
PHP Version 8.0.3 page on Apache

Install PHP 8 for NGINX

Unlike Apache, NGINX does not have built-in capabilities to process PHP documents. To workaround this, you need to install PHP-FPM (i.e., PHP FastCGI Process Manager,) and configure it to work with NGINX.

Firstly, ensure that you add the ondre/php repository.

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

Also, check for package updates with:

$ sudo apt update

To install PHP-FPM 8.0 on Ubuntu 20.04, run the command below.

$ sudo apt install php8.0-fpm

Check the version of PHP with:

$ php -v

Next, open the default NGINX server block file as follows.

$ sudo nano /etc/nginx/sites-available/default

Scroll down until you get to the line that begins with index, and then add index.php to the list as shown in the image below.

Add index.php to NGINX index directive
Add index.php to NGINX index directive

Scroll down further until you see the # pass PHP scripts to FastCGI server section. Change this portion of the configuration file to look like what you see in the code block below.

# pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

Notice that specific lines have been uncommented, and php7.4-fpm.sock has been replaced with php8.0-fpm.sock.

Save your changes and close the server block file.

Test PHP 8 on NGINX

To confirm if PHP 8 is working correctly with NGINX, create an index.php file in the default document root with the command below.

$ sudo nano var/www/html/index.php

Copy and paste the sample PHP code below into the text editor.

<?php
phpinfo();
?>

Save and close the index.php file.

Finally, open a web browser and go to serverIP/index.php. Replace serverIP with your actual server IP.

If you see the PHP Version 8.0.3 info page, then you are on course.

PHP Version 8.0.3 page on NGINX
PHP Version 8.0.3 page on NGINX

Conclusion

In this tutorial, we have described the steps to install and configure PHP 8 on Apache and NGINX web servers. If you have any questions or comments, do share them with us below.

Leave a Comment