PHP is one of the most popular server-side development languages. Many popular web applications are developed in PHP, such as WordPress, Typehco, Laravel, etc.
PHP8.0 is currently the latest version of PHP and introduces some major changes, performance improvements, and many new features such as named parameters, JIT compilers, union types, matching expressions, and more.
This article will show you how to install PHP 8 on Ubuntu 20.04 and integrate it with Nginx and Apache.
As of this writing, the default Ubuntu 20.04 repository includes PHP version 7.4. We will install PHP from the Ondrej/PHP PPA repository.
Before you update or install PHP8, make sure your application running on YOUR VPS supports PHP 8.0.
The same steps in this article apply to Ubuntu 18.04 and all Ubuntu-based distributions, including Kubuntu, Linux Mint, and Elementary OS.
First, enable the PHP repository
Run the following command to enable the PHP repository for ondrej/ PHP:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Copy the code
After the PHP PPA is enabled, you can install PHP8.
Install PHP 8.0 using Apache
If you are using Apache as an HTTP server, install PHP support modules for Apache.
Sudo apt update sudo apt install php8.0 libapache2-mod-php8.0Copy the code
After the installation, restart the Apache service to load the PHP module.
sudo systemctl restart apache2
Copy the code
Configure PHP – FPM
Php-fpm is a FastCGI manager for PHP. Run the following command to install the necessary packages:
Sudo apt update sudo apt install php8.0-fpm libapache2-mod-fcgidCopy the code
Php-fpm is not enabled in Apache by default. To enable it, run:
Sudo a2enconf php8.0-fpmCopy the code
To activate the changes, restart Apache:
systemctl restart apache2
Copy the code
Install PHP 8.0 using Nginx
Nginx does not have built-in support for handling PHP files. We will use PHP-FPM (fastCGI process manager) to process PHP files.
Run the following command to install PHP and the PHP FPM package:
Sudo apt update sudo apt install php8.0-fpmCopy the code
After installation, the phP-fpm service will run automatically. You can run the following command to check the service status:
Systemctl status php8.0 - FPMCopy the code
Now, you can edit the Nginx server configuration file and add the following so that Nginx can handle PHP files:
server { # . . . other code location ~ \.php$ { include snippets/fastcgi-php.conf; Fastcgi_pass Unix: / run/PHP/php8.0 - FPM. The sock. }}Copy the code
When you’re done, don’t forget to restart the Nginx service for the configuration changes to take effect.
sudo systemctl restart nginx
Copy the code
Install PHP extensions
PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are provided as packages and can be easily installed using APT:
Sudo apt install php8.0 - [extname]Copy the code
For example, to install MySQL and GD extensions, you would run the following command:
Sudo apt install php8.0-gdCopy the code
After installing the new PHP extension, remember to restart Apache or the PHP FPM service, depending on your setup.
Testing PHP processing
To test that the Web server is configured correctly for PHP processing, create a new file called info.php in the /var/www/html directory using the following code:
/var/www/html/info.php
<? php phpinfo();Copy the code
Save the file, open your browser, and go to: http://your_server_ip/info.php.
You will see information about the PHP configuration, similar to the following:
Write in the last
Installing PHP 8 on an Ubuntu 20.04 server is not a difficult task. All you need to do is enable the “ondrej/ PHP” repository and install PHP 8 using apt.