Installing PHP is an essential part of any web development pipeline. Without a server-side scripting language, you can’t implement any logic in your sites.
In this article, we’re going over how to install PHP in Ubuntu 20.04 and 18.04 LTS versions. The process itself is quite straightforward and is almost the same for both versions.
How to install PHP on Ubuntu 20.04 LTS?
We’ll be going over two methods for installing PHP on your Ubuntu machine, one using Apache, the other using Nginx.
Installing PHP with Apache
In case you’re using Apache as your primary webserver, use the following command to install PHP along with the Apache PHP module.
On Ubuntu 20.04, the following command will install PHP 7.4, while on Ubuntu 18.04, this will install PHP 7.2.
sudo apt update
sudo apt install php libapache2-mod-php
Once the packages are installed, restart Apache to load up the PHP module.
sudo systemctl restart apache2
Also read: Mint vs Ubuntu: Linux distro comparison
Installing PHP with Nginx
Nginx doesn’t have built-in support for processing PHP files, which means that we’ll have to use PHP-FPM, also known as fastCGI Process Manager, to handle PHP files.
sudo apt update
sudo apt install php-fpm
The FPM service will automatically start once the installation is complete. To check the service’s status, use this command.
systemctl status php7.4-fpm
Once you’ve verified the service is installed and running, make the following addition to your Nginx server block to allow it to process PHP files.
server {
# . . . other code
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
After this, restart the Nginx service for the changes to take effect.
sudo systemctl restart nginx
Also read: How to install themes in Ubuntu?
How to install PHP on Ubuntu 18.04 LTS?
If you’re using the 18.04 version of Ubuntu, the commands to install PHP via Apache or Nginx remain the same. However, keep in mind that both these commands will install PHP 7.2 as it is the default PHP version in the repository.
To check if the FPM service has started when working with Ubuntu 18.04 LTS, you’ll have to modify the aforementioned command to make sure you get the right PHP version.
systemctl status php7.2-fpm
Installing PHP additions/extensions
PHP’s core functionality might leave you asking for more. There are many PHP extensions/libraries available that you can use to make your workflow faster.
To install these, simply use the apt command in the following syntax.
sudo apt install php-packagename
For example, MySQL and GD, some of the most common PHP extensions, can be installed like this.
sudo apt install php-mysql php-gd
Depending on what server you’re running, be sure to restart either Apache or the PHP-FPM service once the installation is complete.
Also read: 25 essential Linux Terminal commands
How to test your PHP installation?
Testing your installation is rather easy. Create a new file called info.php in the /var/www/html directory and add the following code.
<?php
phpinfo();
Now save this file and head over to your browser. Type localhost/info.php and press enter. If everything worked, you should see a page showing your PHP configuration.
Also read: How to remove a Directory in Linux?
How to install older versions of PHP on Ubuntu?
One of Debian’s developers, Ondřej Surý, maintains a repository of PHP versions. Here’s how you can enable the repo.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Now you can install just about any PHP version using the apt command.
sudo apt install php(version)
For example, to install PHP 7.2, the command would be something like this.
sudo apt install php7.2
The same thing applies to installing packages as well.
Also read: How to install PHP on Windows manually?