This tutorial explains all the steps required to install the default distribution of PHP 7 on Ubuntu 18.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 18.04 LTS and How To Install MySQL 8 on Ubuntu.
Install PHP 7
In this step, we will install PHP 7.2, the default PHP package available for Ubuntu 18.04 LTS release. It can be installed using the command as shown below.
# Refresh indexes sudo apt-get update
# Install PHP 7.2 on Ubuntu 18.04 LTS sudo apt-get install php7.2
# Verify PHP php --version
# Output PHP 7.2.24-0ubuntu0.18.04.3 (cli) (built: Feb 11 2020 15:55:52) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.24-0ubuntu0.18.04.3, Copyright (c) 1999-2018, by Zend Technologies
# Autoclean sudo apt-get autoclean
# Autoremove sudo apt-get autoremove
# OR sudo apt-get --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.2, /
Install Apache2 Module and MySQL Extension
Use the below-mentioned commands to install the Apache2 Module and MySQL extension for PHP 7.
# Install Apache2 Module if not installed by default sudo apt-get install libapache2-mod-php7.2
# Install MySQL Extension sudo apt-get install php7.2-mysql
# Install MySQL Extension - Required for Wordpress Installation sudo apt-get 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-get install php7.2-cgi php7.2-cli
# Install CURL and JSON extensions sudo apt-get install php7.2-curl php7.2-json
# Install PHP GD and Imagick sudo apt-get install php7.2-gd php-imagick
# Multibyte String, Internationalization and Spell Check sudo apt-get install php7.2-mbstring php7.2-intl php7.2-pspell
# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation sudo apt-get install php-mbstring php-intl php-pspell
# Emails sudo apt-get install php7.2-imap
# SQLite sudo apt-get install php7.2-sqlite3
# Tidy and XML RPC sudo apt-get install php7.2-tidy php7.2-xmlrpc
# Excel sudo apt-get install php7.2-xsl
# FPM sudo apt-get install php7.2-fpm
Install OPcache to enable caching at the bytecode level.
# Install OPcache extension sudo apt-get install php7.2-opcache
Install the extensions to handle compressed files.
# Install Zip sudo apt-get install php7.2-zip
These are the steps required to install PHP 7 on Ubuntu 18.04 LTS and the additional packages that are mostly required for the website development.
Summary
This is how we can install PHP 7 on Ubuntu 18.04 LTS and also enable it's extension for the Apache HTTP Server. We have also installed the popular extensions for PHP which are required to host web applications.