0 3 mins read
| Published on: January 25, 2023 | Last updated on: March 20, 2024

Introduction

WARNING: PHP 8 End of Life Alert

Installing PHP 8 on Ubuntu 20.04 and combining it with Nginx and Apache is covered in detail here.

Among server-side languages, PHP has the most widespread use. PHP is used to create a wide variety of content management systems and frameworks.

The one of the current stable version of PHP is 8.0. It adds several new features, including named arguments, a JIT compiler, union types, a match expression, and more, as well as a number of breaking changes and improved efficiency.

In the time of this writing, PHP 7.4 may be found in the default Ubuntu 20.04 repository. Now we’ll use the ondrej/php PPA repository to install PHP.

Make sure your software is compatible with PHP 8 before updating or installing it.

All Ubuntu-based distributions, including Kubuntu, Linux Mint, and Elementary OS, follow the same procedures as described here, not only Ubuntu 18.04.

Enabling PHP Repository

A Debian developer by the name of Ondej Sur runs a repository that contains a number of different PHP distributions. In order to activate the repository, type:

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

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

Installing PHP 8.0 with Apache

PHP may be used as either an Apache module or PHP-FPM while the Apache web server is in use.

Install PHP as Apache Module

Installation of PHP as an Apache module is a simple process:

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

After the packages have been installed, Apache must be restarted so that the PHP module can be loaded.

Bash
sudo systemctl restart apache2

Configure Apache with PHP-FPM

Php-FPM is a PHP-specific FastCGI process manager. Execute the command below to install the required packages:

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

Apache is not configured to use PHP-FPM by default. To enable it, run:

Bash
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

To apply the modifications, restart Apache:

Bash
systemctl restart apache2

Installing PHP 8.0 with Nginx

Nginx does not have built-in support for parsing PHP files. To handle PHP files with Nginx, you need to install PHP-FPM (FastCGI Process Manager).

To install PHP and PHP-FPM packages, run the following commands:

Bash
sudo apt update
sudo apt install php8.0-fpm

After the installation is complete, the PHP-FPM service will automatically start running. You can check the status of the service by executing:

Bash
systemctl status php8.0-fpm

You should see output similar to:

Bash
 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 configure Nginx to handle PHP files, you need to modify the Nginx server block. Open the server block configuration file and add the following lines inside the server block:

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

This configuration tells Nginx to pass PHP files to the PHP-FPM process for execution. The location block matches any request ending with .php and passes it to the PHP-FPM socket file (/run/php/php8.0-fpm.sock).

After making the changes, save the configuration file and restart the Nginx service for the new configuration to take effect:

Bash
sudo systemctl restart nginx

Your Nginx server is now set up to handle PHP files using PHP-FPM.

Installing PHP Extensions

PHP extensions are libraries that enhance PHP’s core functionality, enabling features such as database interaction, image manipulation, and more. These extensions can be easily installed using the apt package manager on Debian-based systems, including Ubuntu.

To install a PHP extension, run the following command:

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

For instance, to install the MySQL and GD extensions, you would execute:

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

Efficient Installation of Multiple Extensions

To optimize the process of installing multiple PHP extensions, consider these approaches:

  1. Single Command Installation: You can install multiple extensions simultaneously by specifying them in a single apt command:
Bash
sudo apt install php8.0-mysql php8.0-gd php8.0-curl
  1. Brace Expansion: This technique utilizes brace expansion to minimize typing when installing several extensions. It is particularly handy for quickly adding multiple extensions:
Bash
sudo apt install php8.0-{mysql,gd,curl}

This command expands to install the MySQL, GD, and cURL extensions for PHP 8.0.

Restart Your Web Server

After installing new PHP extensions, it’s crucial to restart your web server or PHP FastCGI Process Manager (FPM) to ensure the extensions are properly loaded.

  • For Apache:
Bash
sudo systemctl restart apache2
  • For PHP-FPM:
Bash
sudo systemctl restart php8.0-fpm

Restarting your server or PHP service activates the newly installed extensions, making them available for use in your PHP applications.

Conclusion

It doesn’t take much effort to set up PHP 8 on a server running Ubuntu 20.04. Installing PHP 8 with apt is as simple as enabling the “ondrej/php” repository.

Please share your thoughts and questions by posting a comment below.

IF YOU ARE HAVING PROBLEMS WITH THIS GUIDE, EMAIL US AT:

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

Copyright @2022-2024 All Right Reserved – PCPlanet

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. You understand and give your consent that your IP address and browser information might be processed by the security plugins installed on this site. By clicking “Accept”, you consent to the use of ALL the cookies.
.
Accept Read More

Privacy & Cookies Policy