Search

How to Install PHP 8.0 on Ubuntu 20.04 / 18.04

  • Share this:
post-title

What is PHP ?

PHP is one of the most widely used server-side programming languages. It’s the language of choice when developing dynamic and responsive websites. In fact, popular CMS platforms such as WordPress, Drupal, and Magento are based on PHP.
At the time of penning down this guide, the latest version of PHP is PHP 8.0. It was released on November 26, 2020. It boasts of new features and optimisations such as union types, named arguments, null safe operator, match expression, JIT, and improvements in error handling and consistency.

Step 1: Ondřej Surý PPA Repository will be add

PHP 7.4 is the default PHP version in Ubuntu 20.04 repositories at the time of writing this tutorial. To install the latest version of PHP, we are going to use the Ondřej Surý PPA repositories. This repository contains multiple PHP versions and PHP extensions.

In spite of all this, we have to update the package of Ubuntu your Ubuntu system packages and install some dependencies as shown.

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

Next  , Ondřej Surý PPA Repository will be add 

sudo add-apt-repository ppa:ondrej/php

When prompted, press ENTER to proceed with adding the Ondřej Surý PPA  repository.

Step 2: Install PHP 8.0 with Apache on Ubuntu Operating System

 

Next, update the system repositories to start using the PPA.

sudo apt update 


If you are running the Apache web server, install PHP 8.0 with the Apache module as shown.

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


Next, restart the Apache webserver to enable the module.

sudo systemctl restart apache2

Next,If you want to use Apache web server with PHP-FPM, run the command below to install the required packages:

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

Next,Since PHP-FPM is not enabled by default, enable it by invoking the following commands:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

Next,Then restart the Apache web server for the changes to come into effect.

sudo systemctl restart apache2
Step 3: Install PHP 8.0 with Nginx on Ubuntu Operating System

 

Next,If you choose to use PHP 8.0 with Nginx installation, the most recommended step to take is to install PHP-FPM to process PHP files.
Therefore, install PHP and PHP-FPM using the following command:

sudo apt install php8.0-fpm


Next,The PHP-FPM service should start automatically. You can verify this as shown:

sudo systemctl status php8.0-fpm


Next,For Nginx to process PHP files, configure your Nginx server block by updating the server section as shown:

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


Finally, start the Nginx web server for the changes to come into effect.

sudo systemctl restart nginx
Step 4: Install PHP 8.0 Extensions in Ubuntu

PHP extensions are libraries that extend the functionality of PHP. These extensions exist as packages and can be installed as follow:

sudo apt install php8.0-[extension-name]


The example below installs the SNMP, Memcached, and MySQL extensions.

sudo apt install php8.0-snmp php-memcached php8.0-mysql
Step 5: Verify PHP 8.0 Installation in Ubuntu

To confirm the version of PHP installed 

php -v

You can also check with the phpinfo() in function print 

<?php echo phpinfo(); ?>