How to install PHP on Ubuntu 20.04

PHP is one of the most commonly used server side programming languages. Many popular CMS and frameworks such as WordPress, Magento and Laravel are written in PHP.

This article covers the steps required to install PHP on Ubuntu 20.04 and integrate it with Nginx and Apache.

At the time of writing, the Ubuntu 20.04 repositories included PHP 7.4 by default. We’ll also show you how to install previous PHP versions. Before choosing which version of PHP to install, make sure your applications support it.

Installing PHP 7.4 with Apache

If you are using Apache as your web server, run the following commands to install PHP and the Apache PHP module:

sudo apt update
sudo apt install php libapache2-mod-php

After installing the packages, restart Apache to load the PHP module:

sudo systemctl restart apache2

Install PHP 7.4 with Nginx

Unlike Apache, 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 commands to install PHP and PHP FPM packages:

sudo apt update
sudo apt install php-fpm

After the installation is complete, FPM will start automatically. To check the status of the service, run

systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-09 19:07:05 UTC; 37s ago

Now you can edit the Nginx server block and add the following lines so that Nginx can process PHP files:

server {

    # . . . остальной код

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:q;

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

sudo systemctl restart nginx

Installing PHP extensions

PHP Extensions are compiled libraries that extend the core PHP functionality. The extensions are available as packages and can be easily installed using apt:

sudo apt install php-[extname]

For example, to install the MySQL and GD extensions, you must run the following command:

sudo apt install php-mysql php-gd

After installing the new PHP extension, depending on your settings, remember to restart Apache or the PHP FPM service.

PHP testing

To check if your web server is configured correctly for PHP processing, create a new file called info.php inside the / var / www / html directory with the following code:




Save the file, open a browser, and visit: http: //your_server_ip/info.php.

You will see information about your PHP configuration as shown in the picture below:

Installing Previous Versions of PHP

Debian maintains the Ondřej Surý repository including several PHP versions. To enable the repository, run:

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

You can now install the PHP version you want by adding the version number to the package name:

sudo apt install php[version]

For example, to install PHP 7.1 and several common PHP modules, you must run:

sudo apt install php7.1 php7.1-common php7.1-opcache php7.1-mcrypt php7.1-cli php7.1-gd php7.1-curl php7.1-mysql


Installing PHP on an Ubuntu 20.04 server is a simple task. All you have to do is install the package with apt.

If you have any questions or requests, do not hesitate to leave comments.