How to install PHP on Ubuntu 20.04

s2 0

PHP is one of the most widely 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 required 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 contain the PHP 7.4 version. We’ll also show you how to install previous versions of PHP. Before choosing which version of PHP to install, make sure your applications support it.

Install 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 updatesudo apt install php libapache2-mod-php

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

sudo systemctl restart apache2

Install PHP 7.4 with Nginx

Unlike Apache, Nginx doesn’t have built-in support for processing PHP files. We 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

After the installation is complete, 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 to allow Nginx to process PHP files:

server {

    # . . . other code

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}

Don’t forget to restart the Nginx service for the new configuration to take effect:

sudo systemctl restart nginx

Install PHP extensions

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

sudo apt install php-[extname]

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

sudo apt install php-mysql php-gd

After installing a new PHP extension, don’t forget to restart the Apache or PHP-FPM service, depending on the setup.

Test PHP processing

To test that the web server is properly configured for PHP processing, create a new file called info.php within the /var/www/html Directory with the following code:

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

phpinfo();

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

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

Install previous PHP versions

Ondřej Surý, a Debian developer, maintains a repository that contains several versions of PHP. To activate the repository, do the following:

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

You can now install any 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 some popular 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

diploma

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

If you have any questions or feedback, don’t hesitate to leave a comment.

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04