How to install PHP on CentOS 6, 7?

How to install PHP on CentOS 6, 7?

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.

  • EPEL repository – tutorial here.
  • IUS repository – tutorial here.

 

1. Install PHP on your CentOS 6, 7 system.

Command: 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:

Command: sudo yum install php56u -y

PHP 7.0 installation:

Command: sudo yum install php70u -y

PHP 7.1 installation:

Command: 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.

Command: sudo systemctl restart httpd.service

 

3. How to check your current PHP version?

Command: sudo php -v

Output:

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. 

Create the info.php file using nano or your favorite text editor.

Command: 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: http://<your_server_ip_address_or_domain>/info.php

 

4. Remove PHP and related packages from your system:

Command: sudo yum remove php*

Note: Make sure to double check the list of to be uninstalled packages in order not to uninstall too much.

 

3 Comments

  1. Pingback: How to install Wordpress on your CentOS or Ubuntu server? - Tiny Tumbleweed

  2. Pingback: How to change PHP memory limit in php.ini file? - Tiny Tumbleweed

  3. Pingback: How to setup phpMyAdmin on Centos 7? - Tiny Tumbleweed

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.