Sep 5, 2023 4 min read

How to Install PHP 8 on Ubuntu 20.04

Install PHP 8 on Ubuntu 20.04 with our step-by-step tutorial. It is an upgrade to the server-side scripting language used for web development.

Install PHP 8 on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install PHP 8 on Ubuntu 20.04, let's briefly understand - What is PHP?

PHP 8 is a major upgrade to the popular server-side scripting language used for web development. Released in 2020, it brings significant improvements in performance and features. PHP 8 introduces the new Just-in-Time compiler, resulting in faster code execution. It also includes a host of new features like named arguments, union types, and attributes.

PHP 8 offers improved error handling and better support for the latest standards and protocols. It brings enhanced type safety and stricter error checking, making it easier for developers to write robust and secure code. With its numerous enhancements, PHP 8 is a game-changer for web development, empowering developers to create faster and more efficient websites and applications.

In this tutorial, you will install PHP 8 and combine it with Nginx and Apache on Ubuntu 20.04. The default Ubuntu 20.04 repositories feature PHP 7.4 at the time of writing. We'll use the ondrej/php PPA repository to install PHP.

Advantages of PHP 8

  1. Improved Performance: PHP 8's Just-in-Time compiler leads to faster code execution, resulting in improved website and application performance.
  2. Enhanced Features: PHP 8 introduces new features like named arguments, union types, and attributes, enabling developers to write more flexible and efficient code.
  3. Better Error Handling: PHP 8 offers improved error handling, making it easier to catch and address issues during the development process.
  4. Enhanced Type Safety: With stricter error checking and improved type safety, PHP 8 helps developers write more reliable and secure code.
  5. Compatibility and Support: PHP 8 provides better compatibility with the latest standards and protocols, ensuring a seamless development experience and easier integration with other technologies.

Make sure your programs support PHP 8 before updating or installing it.

Step 1 - Enable PHP Repository

1) Run the following command to enable the repository:

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

You can install PHP 8 after the PPA has been enabled.

Step 2 - Install PHP 8.0 with Apache

You can run PHP as an Apache module or PHP-FPM if you're using Apache as your web server.

Install PHP as Apache Module

The process of installing PHP as an Apache module is simple:

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

Restart Apache after installing the packages to allow the PHP module to load:

sudo systemctl restart apache2

Configure Apache with PHP-FPM

PHP's Php-FPM is a FastCGI process manager. To install the required packages, use the following command:

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

PHP-FPM is disabled by default in Apache. Run the following command to enable it:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

Restart Apache to apply the changes:

sudo systemctl restart apache2

Install PHP with Nginx

Nginx doesn't come with built-in PHP processing capabilities. The PHP files will be handled by PHP-FPM ("fastCGI process manager").

To install PHP with PHP FPM, use the following commands:

sudo apt update
sudo apt install php8.0-fpm

The FPM service will start immediately after the installation is complete. Run this command to see if the service is up and running.

sudo systemctl status php8.0-fpm
Output
● 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

To allow Nginx to process PHP files, update the Nginx server block and add the following lines:

server {

    # . . . other code

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

Remember to restart the Nginx service for the new settings to take effect:

sudo systemctl restart nginx

Step 3 - Install PHP Extensions

Extensions for PHP are compiled libraries that enhance PHP's fundamental capabilities. Extensions are distributed as packages and can be installed using apt:

sudo apt install php8.0-[extname]

Let's take an example, to install MySQL and GD extensions, use the following command:

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

Remember to restart Apache or PHP FPM after installing a new PHP extension, depending on your setup.

Test PHP Processing

Create a new file named info.php in the /var/www/html directory with the following code to see if the webserver is properly set for PHP processing:

<?php

phpinfo();

Save the file, then go to http://your_server_ip/info.php in your browser. You will be able to see the information about your PHP configuration.

FAQs to Install PHP 8 on Ubuntu 20.04

Are there any specific requirements for installing PHP 8 on Ubuntu 20.04?

No, there are no specific requirements. However, it's recommended to update your system packages before installing PHP 8.

Can I upgrade from an older version of PHP to PHP 8 on Ubuntu 20.04?

Yes, you can upgrade from older PHP versions to PHP 8. Before upgrading, it's advised to backup your existing code and configurations.

How can I verify if PHP 8 is installed correctly on Ubuntu 20.04?

You can check the PHP version by running the command php -v. It should display the PHP version as 8.0.x.

Does installing PHP 8 on Ubuntu 20.04 affect my existing PHP configurations?

Installing PHP 8 should not affect your existing PHP configurations. However, it's always a good practice to back up your configurations before proceeding.

Are there any additional PHP extensions I need to install with PHP 8 on Ubuntu 20.04?

The required PHP extensions depend on your project's specific requirements. You can install additional extensions using the apt package manager.

How do I switch between PHP versions after installing PHP 8 on Ubuntu 20.04?

You can use the update-alternative command to switch between different PHP versions installed on your system.

How do I uninstall PHP 8 from Ubuntu 20.04?

To uninstall PHP 8, you can use the command sudo apt remove php8.0 on Ubuntu 20.04. Additionally, you can remove any related PHP packages and configurations.

Conclusion

We hope this tutorial helped you understand how to install PHP 8 on Ubuntu 20.04

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.