This tutorial explains how to install different PHP versions on your CentOS 6,7 operating systems based server.
Prerequisites: Additional repositories (EPEL, IUS) will be required to install the latest versions.
1. Install PHP on your CentOS 6, 7 system.
sudo yum install php -y
Note: This will install the default version, which is available for your CentOS system. In our case the default version is 5.4. However, you will most likely want to install a newer version, such as 5.6, 7.0 or 7.1 on your system.
PHP 5.6 installation:
sudo yum install php56u -y
PHP 7.0 installation:
sudo yum install php70u -y
PHP 7.1 installation:
sudo yum install php71u -y
Note: You will most likely need to install additional PHP extensions for your applications.
For example, you can use this tutorial to install some popular extensions.
2. Restart Apache2 webserver for changes to take affect.
sudo systemctl restart httpd.service
3. How to check your current PHP version?
sudo php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Another method to check your current PHP version is to create the
info.php file in your webroot (
/var/www/html/ by default) directory.
info.php file using
nano or your favorite text editor.
sudo nano /var/www/html/info.php
Write the following line in the file and exit out of the file:
<?php phpinfo(); ?>
Now open the info.php file in your web browser:
4. Remove PHP and related packages from your system:
sudo yum remove php*
Note: Make sure to double check the list of to be uninstalled packages in order not to uninstall too much.