Table of Contents
In this post, you will learn how to install PHP 7.4 on Ubuntu 20.04 LTS Focal Fossa.
Update Ubuntu and Install Dependencies
Before proceeding, run a system update to ensure all your packages are up to date to avoid any conflicts during the installation.
The following dependencies will need to be installed to install PHP successfully. Most of these packages are already on your system but running the command can help ensure they’re installed.
sudo apt install software-properties-common apt-transport-https -y
Import a PHP PPA
1. The first task is to import the Ubuntu PPA.
sudo add-apt-repository ppa:ondrej/php -y
# sudo add-apt-repository ppa:ondrej/php -y
...
Get:4 http://vn.archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease [23.9 kB]
Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 Packages [106 kB]
Get:7 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main Translation-en [34.6 kB]
Fetched 278 kB in 6s (46.1 kB/s)
Reading package lists... Done
2. Once done, it is a good idea to refresh your apt repositories. After running an update, you should see a few packages that need updating, let’s run an upgrade now.
sudo apt update && sudo apt upgrade -y
Install PHP 7.4 on Ubuntu 20.04
Install PHP 7.4 with Nginx
Nginx does not contain native PHP processing like other web servers like Apache. To handle the PHP files, you must install php-fpm.
1. In your terminal, use the following command to install php7.4 and php7.4-fpm.
sudo apt install php7.4 php7.4-fpm php7.4-cli -y
2. Once installed, the php-fpm service is automatically started, and you can check the status to make sure it’s running.
sudo systemctl status php7.4-fpm
# sudo 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 2022-10-25 14:02:34 UTC; 14s ago
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 4575)
Memory: 7.8M
CGroup: /system.slice/php7.4-fpm.service
├─9129 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─9139 php-fpm: pool www
└─9140 php-fpm: pool www
3. You will need to edit your Nginx server block and add the example below for Nginx to process the php files. Below is an example of all server blocks that process PHP files that need the location ~ .php$ added.
server {
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
4. Test Nginx to make sure you have no errors with the adjustments made with the code above then restart Nginx service for installation to be complete.
sudo nginx -t
sudo systemctl restart nginx
5. As a reminder to see what version of php 7.4 is installed on your system, use the following command.
php --version
# php --version
PHP 7.4.32 (cli) (built: Sep 29 2022 22:24:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies
Install PHP 7.4 with Apache
If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.
Install Apache Module
To install PHP 7.4 as an Apache module, enter the following command.
sudo apt install php7.4 libapache2-mod-php7.4 -y
Once installation is complete, restart your Apache server to load the new PHP module.
sudo systemctl restart apache2
Install Apache with PHP-FPM
PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.
1. To install PHP-FPM with the following commands.
sudo apt install php7.4-fpm libapache2-mod-fcgid -y
2. Note, by default, PHP-FPM is not enabled for Apache. You must enable it by the following command.
sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php7.4-fpm
sudo systemctl restart apache2
# sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php7.4-fpm
sudo systemctl restart apache2Considering dependency proxy for proxy_fcgi:
Enabling module proxy.
Enabling module proxy_fcgi.
Module setenvif already enabled
To activate the new configuration, you need to run:
systemctl restart apache2
Enabling conf php7.4-fpm.
To activate the new configuration, you need to run:
systemctl reload apache2
3. Verify that PHP-FPM is working:
sudo systemctl status php7.4-fpm
# sudo 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 2022-10-25 14:02:34 UTC; 14s ago
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 4575)
Memory: 7.8M
CGroup: /system.slice/php7.4-fpm.service
├─9129 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─9139 php-fpm: pool www
└─9140 php-fpm: pool www