[2 min installs] PHP on Ubuntu

Better Stack Team
Updated on August 30, 2022

How to install PHP on Ubuntu 20.04 in under two minutes:

Install PHP with Apache

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

Step 1 - Update the package list on your system

The first step is to update the package list on your system before proceeding to actual installation:

 
sudo apt update -y

apt update downloads the package lists from the repositories and updates them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.

Step 2 - Install PHP

Now, run the following command to install PHP and Apache PHP module.

 
sudo apt install php libapache2-mod-php

Step 3 - Reload Apache

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

 
sudo systemctl restart apache2

Install PHP 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.

Step 1 - Update the package list on your system

The first step is to update the package list on your system before proceeding to actual installation:

 
sudo apt update -y

apt update downloads the package lists from the repositories and updates them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.

Step 2 - Install PHP

Now install the php-fpm package from to official repository.

 
sudo apt install php-fpm

Step 3 - Adjust server block

Next step is to set Nginx to process PHP files. To do that, add the following code to the server block configuration file.

 
server {

    # ...

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

Finally restart Nginx by running the following command:

 
sudo systemctl restart nginx

That’s it

You have successfully installed PHP on your Ubuntu system running Apache / Nginx webserver.

Check Uptime, Ping, Ports, SSL and more.
Get Slack, SMS and phone incident alerts.
Easy on-call duty scheduling.
Create free status page on your domain.
Got an article suggestion? Let us know
Next article
[2 min installs] Java on Ubuntu
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.