Platform: CentOS
Skill level: Intermediate
Time needed: 10 Minutes

How to Upgrade PHP on CentOS Server

If you want to upgrade your current PHP to newer version on CentOS server, these steps are work great for me.

 

1. Verify Your PHP Version

First, you need to know your current version of PHP.

php -v

You should get your current PHP version details

PHP 5.3.3 (cli) (built: Jul 9 2015 17:39:00) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

 

2. Decide Where You Want to Upgrade

Then decide which PHP version you want upgrade to. For me, I want to upgrade from PHP 5.3 to PHP 5.6, since some of my website platform didn’t support PHP 7 yet.

 

3. Install Remi & EPEL Repositories

In case you never did it before, then you need to install Remi & EPEL Repositories. Since I’m using CentOS 6.6, I run this command below:

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm && rpm -Uvh epel-release-latest-6.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && rpm -Uvh remi-release-6*.rpm

Enable Remi’s global repository

nano /etc/yum.repos.d/remi.repo

Change the enabled value from 0 to 1

[remi]
name=Remi's RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Do the same thing to Remi’s PHP repository. I edit [Remi-php56] because I decide to upgrade my PHP version to PHP 5.6. If you want to upgrade to PHP 7.0 or 7.1, you can find the configuration on /etc/yum.repos.d/remi-php70.repo or /etc/yum.repos.d/remi-php71.repo.

[remi-php56]
name=Remi's PHP 5.6 RPM repository for Enterprise Linux 6 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/6/php56/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/6/php56/mirror
# NOTICE: common dependencies are in "remi-safe"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

 

4. Upgrade PHP

Now it’s time to upgrade your PHP version using this command below

yum -y upgrade php*

Once it’s completed, you can verify your PHP version again.

php -v

Your PHP should be successfully upgraded to a newer version.

PHP 5.6.30 (cli) (built: Jan 19 2017 08:09:42) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

 

5. Check & Troubleshoot

The last step is checking your websites. Make sure everything works normally and running smooth as usual.

 

Common Trouble #1: Website Not Accessible, 500 Error.

In case you get 500 error while accessing your website, and found permission error on Nginx like this:

connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream

You can fix it by editing www.conf.

nano /etc/php-fpm.d/www.conf

Make sure you uncomment listen.owner and listen.group, then configure based on your nginx user.

listen.owner = nginx
listen.group = nginx

Restart PHP service

service php-fpm restart

 

Common Trouble 2: Session Permission Error

The other common problem which appeared after upgrading PHP version is session permission error. You may get this error warning in your Nginx log:

PHP Warning:  Unknown: open(/var/lib/php/session/sess_gtkbirlbhr7p6fmknh36abvdh5, O_RDWR) failed: Permission denied (13) in Unknown on line 0
PHP message: PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0" while reading upstream

You can fix PHP session permission error by change the folder owner of /var/lib/php/session to your web server user.

cd /var/lib/php
chown -R nginx:nginx session

 

That’s how I successfully upgrade my PHP version from PHP 5.3 to PHP 5.6 on my centOS 6.6 server. If you have questions about upgrade PHP version on CentOS server, please do let me know in the comment below.

SHARE
Febian
Entrepreneur. Content Producer. Founder Poin Asia, blog network with more than 2.5 million pageviews / month.

LEAVE A REPLY

Please enter your comment!
Please enter your name here