How To Install PHP 7.3 / 7.2 / 7.1 on Ubuntu 18.04 / Ubuntu 16.04

0

PHP is an open-source programming language widely used for web development. It is an HTML-embedded scripting language for building dynamic web sites.

By default, Ubuntu 18.04 and Ubuntu 16.04 ships PHP v7.2 and PHP v7.0 (EOL) respectively.

In this post, we will see how to install PHP 7.3 / 7.2 / 7.1 on Ubuntu 18.04 / Ubuntu 16.04.

Add PHP Repository

Ondřej Surý, a third-party repository, hosted on launchpad which offers PHP 7.3 / 7.2 / 7.1 for Ubuntu operating system.

Update the repository cache.

sudo apt update

Install the below packages.

sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https

Add the repository to your system.

sudo apt-add-repository ppa:ondrej/php

Update the repository index.

sudo apt update

Install PHP

Install PHP 7.3 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.3 with the below command.

sudo apt install -y php7.3 php7.3-cli php7.3-common

Install PHP 7.2 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.2 with the below command.

sudo apt install -y php7.2 php7.2-cli php7.2-common

Install PHP 7.1 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.1 with the below command.

sudo apt install -y php7.1 php7.1-cli php7.1-common
You can also have multiple PHP version on your system. Each PHP version will have separate configuration files under /etc/php/7.X directory.

Verify PHP Version

Once you have installed the PHP package, check its version.

php -v

Output:

PHP 7.3.7-2+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jul 25 2019 11:44:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.7-2+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

PHP Support for Web Server

Both Apache and Nginx do not support processing of PHP files by default when the browser requests PHP page. So, we need to install the PHP package on the server to support PHP files.

PHP Support for Apache

You can install a below package with Apache webserver to support PHP for LAMP stack. This package provides the PHP modules for Apache 2 web server.

READ: How To Install LAMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y apache2 libapache2-mod-php7.3

PHP Support for Ngnix

Nginx does not have PHP modules package to support PHP for LEMP stack. To have PHP support for Nginx, we can use the PHP FastCGI Process Manager to handle PHP requests.

READ: How To Install LEMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y php7.3-fpm

PHP-FPM listens on the socket run/php/php7.3-fpm.sock by default.

To make PHP-FPM to use TCP connection, edit the below file.

sudo nano /etc/php/7.3/fpm/pool.d/www.conf

Then, change the listen parameter.

FROM:

listen = /run/php/php7.3-fpm.sock

TO:

listen = 127.0.0.1:9000

Add socket details in Nginx virtual host.

server {

# other codes

  location ~* \.php$ {
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }
}

Install PHP Extensions

PHP extensions enable specific functions/support for your code. For example, installing PHP MySQL extension will let your PHP code to connect with the MySQL database.

PHP extensions package is normally named like php<version>-<extn_name>.

To install MySQL support for PHP v7.3, you can install php7.3-mysql package.

sudo apt install -y php7.3-mysql

Once you have installed a required extension, use the below command to verify it.

php -m | grep -i mysql

Output:

mysqli
mysqlnd
pdo_mysql

PHP Extensions for CMS

PHP Extensions for WordPress

The following extensions are required to install and run WordPress on Ubuntu. WordPress recommends having PHP v7.3 for the installation.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-simplexml php7.3-ssh2 php7.3-xml php7.3-xmlreader php7.3-curl  php7.3-exif  php7.3-ftp php7.3-gd  php7.3-iconv php7.3-imagick php7.3-json  php7.3-mbstring php7.3-posix php7.3-sockets php7.3-tokenizer

PHP Extensions for Joomla

The following extensions are required to install and run Joomla on Ubuntu. Joomla requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-xml php-pear php7.3-json

PHP Extensions for Drupal

The following extensions are required to install and run Drupal on Ubuntu. Drupal requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-gd php7.3-json php7.3-pdo php7.3-simplexml php7.3-tokenizer php7.3-xml

Conclusion

That’s All. Please share your feedback in the comments section.

You might also like