Howto Install PHP 5.4, 5.5 or 5.
6 on CentOS 6 and CentOS 7
January 7, 2015 31 comments
I still use CentOS as my distro of choice on any new server and
whilst things are getting better CentOS and RHEL still dont have up
to date versions of PHP available inside the default yum repos. A
couple of years ago I posted about Howto install PHP Mcrypt on
CentOS using EPEL which is one of my sites most popular posts!
So CentOS and PHP, as you can see below (taken from DistroWatch)
CentOS is a bit outdated with its PHP versions which as a PHP
developer can be a pain in the ass!
PHP Current Version CentOS 7.0-1406 CentOS 6.6
5.6.4 5.4.16 5.3.3
However there is yet again hope! This time we need 2 additional
repos. First up we need EPEL and secondly we need Remi a French
guy who is king of PHP repos!
Repo Installation
Open up a SSH connection to your server and run the following
commands (make sure you run as sudo if you need to):
For CentOS 7 (including EPEL install)
1 wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch
2 wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
3 rpm -Uvh remi-release-7*.rpm epel-release-7*.rpm
If you already have EPEL installed:
1 wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
2 rpm -Uvh remi-release-7*.rpm
For CentOS 6 (including EPEL install)
1 wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
2 wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
3 rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
If you already have EPEL installed:
1 wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
2 rpm -Uvh remi-release-6*.rpm
Enabling the Repo
Now we need to make sure the repo is enabled and select which
version you want to install. We need to head over
to /etc/yum.repos.d you should inside see a file called remi.repo .
Open the file in your favourite editor (Nano, Pico, Vi etc), youll see a
number of sections. We need to make sure that the first
section [remi] is enabled:
1 [remi]
2 name=Les RPM de remi pour Enterprise Linux 6 - $basearch
3 #baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
4 mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror
5 enabled=1
6 gpgcheck=1
7 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Note the line enabled=1 make sure this is set! Now technically you
can actually go ahead and install PHP, but you will only get PHP
5.4.*. Which might be want to you want is so skip ahead to the next
section!
If we want PHP 5.5 or PHP 5.6 we need to do a bit more work, further
down in the repo.repo file you will see two additional
sections [remi-php55] and [remi-php56] , decide which PHP version
you want to install and then enable the correct. So for PHP 5.6 we
would change to:
1 [remi-php56]
2 name=Les RPM de remi de PHP 5.6 pour Enterprise Linux 6 - $basearch
3 #baseurl=http://rpms.famillecollet.com/enterprise/6/php56/$basearch/
4 mirrorlist=http://rpms.famillecollet.com/enterprise/6/php56/mirror
5 # WARNING: If you enable this repository, you must also enable "remi"
6 enabled=1
7 gpgcheck=1
8 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Once you made your changes save your modified file and quit your
editor.
Installing PHP
Now Im assuming you dont already have PHP installed, this bit is
super simple.
1 sudo yum install php php-gd php-mysql php-mcrypt
So the above assumes you want MySQL, GD and Mcrypt support in
your PHP, but you should see something like the below depending
on which version of PHP you are trying to install:
1 ===============================================
2 ===============================================
Package Arch Version Reposito
===============================================
3
===============================================
4
Installing:
5
php x86_64 5.5.20-2.el6.remi remi-
6
php-gd x86_64 5.5.20-2.el6.remi rem
7
php-mysqlnd x86_64 5.5.20-2.el6.remi r
8
Installing for dependencies:
9
php-cli x86_64 5.5.20-2.el6.remi remi
10
php-common x86_64 5.5.20-2.el6.remi
11
php-pdo x86_64 5.5.20-2.el6.remi rem
12
php-pear noarch 1:1.9.5-3.el6.remi rem
13
php-pecl-jsonc x86_64 1.3.6-1.el6.remi.5.5.1
14
php-pecl-zip x86_64 1.12.4-1.el6.remi.5.5 r
15
php-process x86_64 5.5.20-2.el6.remi re
16
php-xml x86_64 5.5.20-2.el6.remi rem
17
18
Transaction Summary
19
===============================================
20
===============================================
Install 11 Package(s)
As you can see PHP is installing version 5.5.20-2.el6.remi from the
remi-php55 repo! Once you have hit Y to confirm the install restart
apache and magical unicorns you have a better version of PHP!
You can also change your mind in the future by going back into
the remi.repo file and enable a different PHP version and then
run yum update and if you have moved from 5.5 to 5.6 it will
upgrade PHP for you. If you want to downgrade for any reason you
will need to remove PHP ( sudo yum remove php* ) and then reinstall
the PHP modules you want.
Hope this helps!