Jan 12, 2018

Install or Upgrade to PHP 7.X on Ubuntu

This post explains how to install or upgrade to PHP 7.2 on Ubuntu. It is assumed PHP 5.x is running on an Ubuntu 14.04 machine in case of upgrade.

Note: This new version brings with it a number of new features and a few incompatibilities that should be tested for before switching PHP versions in production environments.

Read Also: Installing Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu 14.04

Steps:

1. Add a PPA for PHP 7.2 Packages

sudo add-apt-repository ppa:ondrej/php

2. update the local package cache

sudo apt-get update
sudo apt-get upgrade

Apache

3. For Apache, install the new packages: (To configuring PHP-FPM With Nginx, go to step 6)

sudo apt-get install php7.2

If you are using MySQL, make sure to re-add the updated PHP MySQL bindings:

sudo apt-get install php7.2-mysql

If you want to install all common packages, you can run following command

sudo apt-get install php7.2-cli php7.2-common libapache2-mod-php7.2 php7.2 php7.2-mysql php7.2-fpm

You might need to install mbstring package also:

sudo apt-get install php7.2-mbstring

4. To update web-server configuration:

Apache with php-fpm:

a2disconf php5-fpm
a2enconf php7.2

Apache with mod_php:

sudo a2dismod php5
sudo a2enmod php7.2

5. Restart Apache

sudo service apache2 restart

Nginx

6. If you are using Nginx as the web server and PHP-FPM to execute PHP code, install the new PHP-FPM package and its dependencies:

sudo apt-get install php7.2-fpm

If you are using MySQL, make sure to re-add the updated PHP MySQL bindings:

sudo apt-get install php7.2-mysql

7. Now you need to update fastcgi_pass configuration to point new version file. Open the default site configuration file

sudo nano /etc/nginx/sites-enabled/default

In the file, go to Server > location ~ \.php$ > fastcgi_pass. Probably it would look like below:

fastcgi_pass unix:/var/run/php/php5-fpm.sock;

change the above with new version:

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

Similarly, you can do the same with your any other virtual sites defined in /etc/nginx/sites-enabled.

8. Restart Nginx

sudo service nginx restart

9. Before upgrading, if you want to do it from scratch means want to uninstall the existing nginx and install a fresh version.

To uninstall:

sudo apt-get purge nginx nginx-common nginx-full

To install:

sudo apt-get update
sudo apt-get install nginx

Clean up

Run phpinfo(), php --info or php -v to verify PHP 7.2 setup.

php 7.2 install upgrade

If all is well, you can clean up old versions:

sudo apt purge php5*  

Now you have working PHP 7.2 environment.

Enjoy PHP !!