How to Install PHP on Ubuntu 20.04

Posted 

3 min read

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

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

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

Installing PHP 7.4 with Apache

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

sudo apt updatesudo apt install php libapache2-mod-php

Once the packages are installed, restart Apache for the PHP module to get loaded:

sudo systemctl restart apache2

Installing PHP 7.4 with Nginx

Unlike Apache, Nginx doesn’t have built-in support for processing PHP files. We’ll use PHP-FPM (“fastCGI process manager”) to handle the PHP files.

Run the following commands to install PHP and PHP FPM packages:

sudo apt updatesudo apt install php-fpm

Once the installation is completed, the FPM service 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

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

server {

    # . . . other code

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

Do not forget to restart the Nginx service so that the new configuration takes effect:

sudo systemctl restart nginx

Installing PHP extensions

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

sudo apt install php-[extname]

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

sudo apt install php-mysql php-gd

After installing a new PHP extension, depending on your setup, do not forget to restart Apache or PHP FPM service.

Testing PHP Processing

To test whether the webserver is configured properly for PHP processing, create a new file named info.php inside the /var/www/html directory with the following code:

/var/www/html/info.php
<?php

phpinfo();

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

You’ll see information about your PHP configuration as shown on the image below:

Installing Previous PHP Versions

Ondřej Surý, a Debian developer, maintains a repository that includes multiple PHP versions. To enable the repository, run:

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

You can now install ant PHP version you need by appending the version number to the package name:

sudo apt install php[version]

For example, to install PHP 7.1 and few common PHP modules, you would 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

Conclusion

Installing PHP on Ubuntu 20.04 server is a straightforward task. All you need to do is to install the package with apt.

If you have any questions or feedback, do not hesitate to leave a comment.