PHP is a popular server-side programming language used by many CMSs and frameworks like WordPress, Magento, and Laravel.
This guide will show you how to install PHP on Ubuntu 20.04 and integrate it with either Nginx or Apache.
PHP Versions
The default version of PHP in the Ubuntu 20.04 repositories is PHP 7.4. We will also cover how to install previous PHP versions. Ensure your applications support the PHP version you choose to install.
Installing PHP 7.4 with Apache
Apache supports PHP natively. If you do not have Apache already installed refer to this tutorial:
Configuring Apache on your Linux Web server
If you’re using Apache as your web server, follow these steps to install PHP and the Apache PHP module:
Update your package list:
sudo apt update
Install PHP and the Apache PHP module:
sudo apt install php libapache2-mod-php
Restart Apache to load the PHP module:
sudo systemctl restart apache2
Installing PHP 7.4 with Nginx
Nginx does not natively support PHP file processing, so we’ll use PHP-FPM (FastCGI Process Manager) for this purpose. Follow these steps:
Update your package list:
sudo apt update
Install PHP and PHP-FPM:
sudo apt install php-fpm
Check the status of the PHP-FPM service:
systemctl status php7.4-fpm
The service should be active and running.
Edit the Nginx server block to enable PHP file processing:
server {
# other code...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Restart Nginx to apply the new configuration:
sudo systemctl restart nginx
Installing PHP Extensions
PHP extensions add functionality to PHP. They can be installed easily using apt
. For example:
To install MySQL and GD extensions, run:
sudo apt install php-mysql php-gd
- After installing a new PHP extension, restart Apache or PHP-FPM service depending on your setup.
Testing PHP Processing
To check if your web server is correctly configured to process PHP:
Create a file named info.php
in the /var/www/html
directory with the following content:
<?php
phpinfo();
?>
- Save the file and open your browser, then visit
http://your_server_ip/info.php
.
You should see a page displaying information about your PHP configuration.