How to Install PHP 8 on Ubuntu 20.04 LTS Server

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.

At the time of writing this guide, the latest version of PHP is PHP 8.0. It was released on November 26, 2020. It boasts of new features and optimizations such as union types, named arguments, null safe operator, match expression, JIT, and improvements in error handling and consistency.

By default, Ubuntu 20.04 Lts does not come with PHP 8.0 version in its default repository So, you need to add from the ondrej/php PPA repository. This tutorial is also applicable to Ubuntu 18.04.

Following steps we are goining to execute:

  • Step 1)–Enable PHP Repository
  • Step 2)– Install PHP 8
  • Step 3) – Installing PHP Extensions
  • Step 4.1) – Install PHP 8 with Apache
  • Step 4.2) – Install PHP 8 with Nginx
  • Step 5) – Switch Default PHP Version (If your Server Running Multiple PHP Version)
  • Step 6)Testing PHP

Note: If you are going to upgrading your existing live server, Before upgrading to or installing PHP 8, make sure that your applications support it and make complete backup your existing environment to avoid any data losses.

Step 1)–Enable PHP Repository

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

Step 2)– Install PHP 8

Execute the following command to install PHP 8

sudo apt install php8.0

After the installation has been completed, you can confirm the installation using the following command

php -v

Step 3) – Installing PHP Extensions

Now, install the required PHP modules for your application. Use the following command to search all available PHP 8.0 modules.

sudo apt search php8.0-*

Then install the required PHP modules. The following command will install some frequently used PHP modules on your system.

sudo apt install php8.0-common php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip -y

You can choose exact extensions as per your requirement from the search list.

Step 4.1) – Install PHP 8 with Apache

Apache is a popular web server widely used for deploying PHP web applications. On Debian-based systems, this is available with the name “apache2”.

An Apache module is available to integrate PHP. For PHP 8.0 install the following Apache module

sudo apt install libapache2-mod-php8.0 

Then enable PHP 8 module

sudo a2enmod php8.0 

Step 4.2) – Install PHP 8 with Nginx

There is no official PHP module for the Nginx server. In that case, we will use PHP-FPM to deploy PHP applications over Nginx web servers.

So, first, install PHP fpm on your system.

sudo apt install php8.0-fpm 

Once the installation is finished, enable and start the service.

sudo systemctl enable php8.0-fpm
sudo systemctl start php8.0-fpm  

Next, update the Nginx server block configuration with the following code.

server {

   # server block code ...

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

The above code instructs the Nginx server to process all files with the “.php” extension using PHP-fpm.

Finally, restart the Nginx webserver to apply the changes.

sudo systemctl restart nginx 

Step 5) – Switch Default PHP Version (If your Server Running Multiple PHP Version)

You can easily switch between multiple PHP versions installed on any system. Execute the following command on the terminal:

sudo update-alternatives --config php  

This system has PHP 8.0 and PHP 7.4 installed. Select a PHP version on our choice.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.0   80        manual mode

Press  to keep the current choice[*], or type selection number: [ENTER CHOICE HERE]

Step 6)Testing PHP

To test whether the web server is configured properly for PHP processing, create a new file named info.php inside the /var/www/html directory with the following code:

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

phpinfo();

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

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

Hope this tech article is helpfull for you while you want to install PHP 8.0 and comfortably integrate it with either Apache or Nginx web servers. Awaited your feedback because it makes us more perfect.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Pingback

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.