Introduction
WARNING: PHP 8 End of Life Alert
Before proceeding with any system updates or software deployments, it’s crucial to address a significant change in your technology stack. PHP 8 has reached its End of Life (EOL), meaning it will no longer receive security updates or support. Using outdated software can introduce vulnerabilities and impact the security and functionality of your applications.
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:
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:
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.
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:
sudo apt update
sudo apt install php8.0-fpm libapache2-mod-fcgid
Apache is not configured to use PHP-FPM by default. To enable it, run:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm
To apply the modifications, restart Apache:
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:
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:
systemctl status php8.0-fpm
You should see output similar to:
● 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:
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:
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:
sudo apt install php8.0-[extension-name]
For instance, to install the MySQL and GD extensions, you would execute:
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:
- Single Command Installation: You can install multiple extensions simultaneously by specifying them in a single
apt
command:
sudo apt install php8.0-mysql php8.0-gd php8.0-curl
- Brace Expansion: This technique utilizes brace expansion to minimize typing when installing several extensions. It is particularly handy for quickly adding multiple extensions:
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:
sudo systemctl restart apache2
- For PHP-FPM:
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.