How to install PHP 8 on Ubuntu 20.04

s2 0

PHP is one of the most widely used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento and Laravel are written in PHP.

PHP 8.0 is the latest major version of the PHP language. It introduces several breaking changes, performance improvements, and many New features
such as named arguments, JIT compilers, union types, match expressions, and more.

This article shows you how to install PHP 8 on Ubuntu 20.04 and integrate it with Nginx and Apache.

At the time of writing, the default Ubuntu 20.04 repositories contain the PHP 7.4 version. We install PHP from the ondrej / php
PPA repository.

Before upgrading to or installing PHP 8, make sure your applications support it.

The same steps apply to Ubuntu 18.04 and all Ubuntu-based distributions, including Kubuntu, Linux Mint, and Elementary OS.

Activate the PHP repository

Ondřej Surý, a Debian developer, maintains a repository that contains several versions of PHP. To activate the repository, do the following:

sudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/php

Once the PPA is activated, you can install PHP 8.

Install PHP 8.0 with Apache

If you are using Apache as your web server, you can run PHP as an Apache module or PHP-FPM.

Install PHP as an Apache module

Installing PHP as an Apache module is a simple task:

sudo apt updatesudo apt install php8.0 libapache2-mod-php8.0

Once the packages are installed, restart Apache for the PHP module to load:

sudo systemctl restart apache2

Configure Apache with PHP-FPM

PHP-FPM is a FastCGI process manager for PHP. Run the following command to install the required packages:

sudo apt updatesudo apt install php8.0-fpm libapache2-mod-fcgid

By default, PHP-FPM is not activated in Apache. To activate it, do the following:

sudo a2enmod proxy_fcgi setenvifsudo a2enconf php8.0-fpm

To activate the changes, restart Apache:

systemctl restart apache2

Install PHP 8.0 with Nginx

Nginx does not have built-in support for processing PHP files. We use PHP-FPM (“fastCGI Process Manager”) to handle the PHP files.

Run the following commands to install PHP and PHP-FPM packages:

sudo apt updatesudo apt install php8.0-fpm

After the installation is complete, the FPM service will start automatically. To check the status of the service, run

systemctl status php8.0-fpm
● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-12-03 16:10:47 UTC; 6s ago

You can now edit the Nginx server block and add the following lines to allow Nginx to process PHP files:

server {

    # . . . other code

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

Don’t forget to restart the Nginx service for the new configuration to take effect:

sudo systemctl restart nginx

Install PHP extensions

PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are available as packages and can easily be installed as well apt
:

sudo apt install php8.0-[extname]

For example, to install MySQL and GD extensions, run the following command:

sudo apt install php8.0-mysql php8.0-gd

After installing a new PHP extension, don’t forget to restart the Apache or PHP-FPM service depending on your setup.

Test PHP processing

To test that the web server is properly configured for PHP processing, create a new file called info.php within the /var/www/html Directory with the following code:

/var/www/html/info.php
<?php

phpinfo();

Save the file, open your browser and visit: https://your_server_ip/info.php.

You will see information about your PHP configuration similar to the following:

diploma

Installing PHP 8 on the Ubuntu 20.04 server is an easy task. All you have to do is activate the “ondrej / php” repository and install PHP 8 as well apt.

If you have any questions or feedback, don’t hesitate to leave a comment.