Linux

How to Install PHP on Ubuntu 20.04

How to Install PHP on Ubuntu 20.04

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 needed to install PHP on Ubuntu 20.04 and integrate it with Nginx and Apache.

At the time of writing, the default Ubuntu 20.04 repository includes version PHP 7.4. We will also show you how to install the previous PHP version. Before choosing which version of PHP to install, make sure your application supports it.

Install PHP 7.4 with Apache

If you use Apache as your web server, run the following command to install PHP and the Apache PHP module:
sudo apt update
sudo apt install php libapache2-mod-php

After the package is installed, restart Apache for the PHP module to load:

sudo systemctl restart apache2

Install PHP 7.4 with Nginx

Unlike Apache, Nginx does not have built-in support for processing PHP files. We will use PHP-FPM (“fastCGI process manager”) to handle PHP files.

Run the following command to install PHP and PHP FPM packages:
sudo apt update
sudo apt install php-fpm

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

systemctl status php7.4-fpm

Output :

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;
    }
}

Don’t forget to restart the Nginx service so that the new configuration takes effect:

sudo systemctl restart nginx

Install PHP extensions

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

sudo apt install php-[extname]

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

sudo apt install php-mysql php-gd

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

Test PHP Processing

To test whether the web server is configured correctly for PHP processing, create a new file called info.php in 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 will see information about your PHP configuration as shown in the image below:

1

Previous PHP Installation Version

Ondřej Surý, a Debian developer, manages a repository that includes several versions of PHP. To activate the repository, run:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

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

sudo apt install php[version]

For example, to install PHP 7.1 and some common PHP modules, you will 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 an Ubuntu 20.04 server is an easy task. All you need to do is install the package with apt.

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

Related posts

How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04

Linux

How to Make a Minecraft Server on Ubuntu 20.04

Linux

How to Install the SNMP Cacti Monitoring Tool on Debian 10

Linux

How to Monitor CPU Usage in Linux Over Time

Linux

Using the ‘ldd’ command on Linux

Linux

How to Set Up WireGuard VPN on CentOS 8

Linux

How to Install and Configure Redis on Ubuntu 20.04

Linux

How to Install Flatpaks on Ubuntu 20.04

Linux

How to Install the ElkArte Forum Software on Ubuntu 20.04

Linux