PHP (Hypertext Preprocessor) is a widely used open source scripting language that is used mainly for web development. It is used generally for creating dynamic web pages or for dynamic images on websites.
In this tutorial, we will learn to install & also to upgrade PHP to latest version i.e. 7.x on CentOS/RHEL & also on Ubuntu systems.
(Recommended Read: Easiest guide for creating a LAMP server)
(Also Read: Installation of LAMP Stack on Ubuntu)
Installing PHP 7
PHP 7 is still not considered to be stable & it is the main reason that most of the Linux distributions still have old PHP 5 on their default repositories. So for installing php 7 on your systems, we need to add some repositories. We will now discuss the process to install/ upgrade php on the both CentOS/RHEL & Ubuntu systems.
The default php provided by default CentOS 7 repositories is 5.4, we can check that by running the following command from terminal,
$ php –v
PHP 5.4.16 (cli) (built: Nov 15 2017 16:33:54)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
In order to install php 7, we will first have to remove any old php packages we have installed,
$ sudo yum remove php*
Once removed we will now add the 3rd party repository named IUS, IUS is official recommended by CentOS project for php7 but there are plenty other repositories that can be used to install php7. Install IUS repository on the system with the following command,
$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
Now install the php 7 packages with the following command,
$ sudo yum install php70u php70u-mysql php70u-devel php70u-pdo php70u-mysqlnd php70u-opcache php70u-xml php70u-gd
After installation, check the version of php by executing the following command,
$ php –v
PHP 7.0.26 (cli) (built: Nov 27 2017 11:26:34) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.26, Copyright (c) 1999-2017, by Zend Technologies
Similarly as we did for CentOS, we will first remove the old php packages with the following command,
$ sudo apt-get remove php*
Now for Ubuntu, we will use the Ondrej’s PPA. Install it by executing the following commands from terminal,
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
We can now install the updated php packages on to the Ubuntu systems,
$ sudo apt-get install php7.1 php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring
That’s it guys. This was our tutorial on how to install or upgrade php version to 7.x. Please feel free to send in your queries or suggestions using the comment box below.