PHP is a most popular server scripting language used for creating dynamic and interactive Web pages. PHP is widely-used programming language in the Web. This tutorial explains how to install PHP 8.0 on Ubuntu 20.04 system.
PHP 8.0 is the latest major release and includes performance improvements, and lots of new features such as named arguments, JIT compiler, union types, match expression, and more. By default, Ubuntu 20.04 ships with PHP version 7.4, which is supported by the most popular CMS and frameworks such as WordPress, Magento, and Laravel. We’ll install PHP from the ondrej/php PPA repository.
Before upgrading to or installing PHP 8, make sure that your applications support it.
Enable PHP Repository
First of all we need to enable the PHP repository, run the below command:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Now, we can go ahead to install PHP 8.
Install PHP 8.0 with Apache
Apache is the most used web server in the world. If you’re using Apache as a web server, you can run PHP as an Apache module or PHP-FPM.
Install PHP as Apache Module
It is very easy task to install Apache Module. Run the below commands:
sudo apt update
sudo apt install php8.0 libapache2-mod-php8.0
After the packages installed, you must restart the Apache to take the effect of changes.
sudo systemctl restart apache2
Configure Apache with PHP-FPM
Php-FPM is a FastCGI process manager for PHP. Execute the below commands to install:
sudo apt update
sudo apt install php8.0-fpm libapache2-mod-fcgid
PHP-FPM is not enabled in Apache by default, You should enable it by typing:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm
To get the changes, restart Apache:
systemctl restart apache2
Installing PHP 8.0 with Nginx
By default, Nginx doesn’t have a built-in support for processing PHP files. So we need to use PHP FPM service to handle the PHP files.
Run the following commands to install PHP and PHP FPM packages:
sudo apt update
sudo apt install php8.0-fpm
The FPM service will start automatically, once the installation has been finished. You can check the status of the service by typing:
systemctl status php8.0-fpm
● 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-20 11:09:17 UTC; 26s ago
After that, you need to edit domain Nginx server block and add following lines to work PHP file using Nginx:
server {
# . . . other code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
}
Save the configuration file and restart the nginx service for the new configuration take effect:
sudo systemctl restart nginx
Installing PHP Extensions
You can install PHP extensions as per requirements. You can easily install extensions using apt package manager in following form:
sudo apt install php8.0-[extname]
For example, to install CLI and PDO extensions, you would run the following command:
sudo apt install php8.0-cli php8.0-pdo
Again, restart the Apache or PHP FPM service as per your setup.
Testing PHP
Once the installation complete, you can verify version by typing:
php --version
PHP 8.0.0RC3 (cli) (built: Nov 27 2020 12:26:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.0.0RC3, Copyright (c), by Zend Technologies
Conclusion
This tutorial shown you how to install PHP 8.0 and configure with Apache and Nginx on your Ubuntu 20.04 system.
If you have any questions or feedback, please leave a comment below.