This tutorial explains all the steps required to install the default distribution of PHP 7 on Ubuntu 20.04 LTS. The steps should be similar on other versions of Ubuntu and Linux based systems. In case you are planning to use PHP with the Apache HTTP Server and MySQL database server, you can install Apache 2 and MySQL 8 before starting the installation of PHP 7. It can be done by following the guides - How To Install Apache 2 On Ubuntu 20.04 LTS and How To Install MySQL 8 on Ubuntu 20.04 LTS. Also, PHP can be used with NGINX Web Server instead of the Apache HTTP Server. NGINX is widely used as a load balancer and reverse proxy for Apache Web Server and other servers. You can follow How To Install And Configure Nginx on Ubuntu 20.04 LTS and Configure Virtual Host Or Server Block On Nginx.
Install PHP 7
In this step, we will install PHP 7.4, the default PHP package available for Ubuntu 20.04 LTS release. It can be installed using the command as shown below.
# Refresh indexes sudo apt update
# Install PHP 7.4 on Ubuntu 20.04 LTS sudo apt install php7.4
# Verify PHP php --version
# Output PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
# Autoclean sudo apt autoclean
# Autoremove sudo apt autoremove # OR sudo apt --purge autoremove
It will ask to confirm the installation. Press Y and hit Enter to confirm the installation. The important paths and files of the PHP installation are /usr/lib/php/7.4, /usr/bin/php7.4, and /etc/php/7.4/cli/php.ini.
Install Apache2 Module
Use the below-mentioned commands to install the Apache2 Module for PHP. Also, refer to How To Install Apache 2 On Ubuntu 20.04 LTS.
# Install Apache2 Module if not installed by default sudo apt install libapache2-mod-php7.4
PHP for NGINX
We can install PHP FPM and configure it for Nginx. This will eliminate the need for the Apache Web Server and Nginx can handle the PHP requests.
# FPM sudo apt install php7.4-fpm
You can follow How To Install PHP For Nginx On Ubuntu 20.04 LTS for further details.
Install MySQL Extension
Use the below-mentioned commands to install the MySQL extension for PHP 7. Also, refer to How To Install MySQL 8 on Ubuntu 20.04 LTS.
# Install MySQL Extension sudo apt install php7.4-mysql
# Enable PDO module sudo phpenmod pdo_mysql
# Install MySQL Extension - Required for Wordpress Installation sudo apt install php-mysql
Additional Extensions
You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.
# Install CGI and CLI if not installed by default sudo apt install php7.4-common php7.4-cgi php7.4-cli
# Install CURL and JSON extensions sudo apt install php7.4-curl php7.4-json
# Install PHP GD and Imagick
sudo apt install php7.4-gd php-imagick
# Multibyte String, Internationalization and Spell Check sudo apt install php7.4-mbstring php7.4-intl php7.4-pspell
# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation
sudo apt install php-mbstring php-intl php-pspell
# Emails sudo apt install php7.4-imap
# SQLite Database sudo apt install php7.4-sqlite3
# Tidy and XML RPC sudo apt install php7.4-tidy php7.4-xmlrpc
# Excel sudo apt install php7.4-xsl
Install OPcache to enable caching at the bytecode level.
# Install OPcache extension sudo apt install php7.4-opcache
Install the extensions to handle compressed files.
# Install Zip sudo apt install php7.4-zip
These are the steps required to install PHP 7 on Ubuntu 20.04 LTS and the additional packages that are mostly required for the website development.
Summary
This tutorial provided all the steps required to install PHP 7 on Ubuntu 20.04 LTS and also enable the extension for the Apache HTTP Server. We have also installed the popular extensions for PHP which are required to host web applications.