A few days ago, PHP.net released the official PHP 7 downloads on its website. In this tutorial, we’ll see how to first install PHP normally with a lower version, then see how to install PHP 7 in such a way that it integrates with Apache and is capable of displaying PHP pages.
Installing the “Base” PHP Module in CentOS
If you’ve just set up your CentOS server with the EPEL repos, then you can install PHP using this simple command:
yum install php
And then restart Apache using:
To find the root directory of your Apache server in which you can place files, type the following command:
grep -i 'DocumentRoot' /etc/httpd/conf/httpd.conf
This will show you an output like this:
From this, we can see that the document root is in: “/var/www/html”. So we place a PHP file inside that folder containing a basic script like this:
<?php phpinfo(); ?>
And when we access it via the given filename, we get an output like this:
So now we have a basic PHP running on our system. The only problem is that it’s dreadfully out of date and we’re using a version that is not supported. PHP 7 is the latest engine with a host of improvements and speed increases. However, it’s not available with the default CentOS/RHEL 7 repos and so we need to get it from an external source.
Removing all PHP Packages
To install a new version of PHP, let’s first remove all PHP related packages using the following command:
yum remove php*
This will clear your existing PHP configuration and prepare you for the next version.
PHP 7 Repos and Apache Integration
Even if we install PHP 7.0+, it’s important that we’re able to access it via the name “php” and not something like “php70”. This is because Apache relies on the “php” command in order to function properly. For example, there’s a repo called “Webtatic” that allows you to install PHP 7.0. So let’s say you remove all previous PHP versions and install the “php70” package, this is what you get if you try and view the previous PHP page:
As you can see, the page is not rendering. Why? Because even though PHP 7 is installed on the server, the command to access it is “php70” and not just “php”. This is why it’s important to choose the right repo. For this purpose in order to achieve seamless integration with Apache, we’ll use the REMI repo instead.
The REMI repo allows you to install the latest versions of the software that use the same names as the old versions. So after installing PHP 7, Apache will be able to pick it up because it’s still accessible using the command “php”.
Installing the REMI Repo
To install the REMI repo, type the following while inside a temporary folder:
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm rpm -Uvh remi-release-7*.rpm
Installing PHP 7 using the REMI Repo
REMI comes with a bunch of disabled sources by default. One of them contains the “php” command version of PHP 7.0. We can get a list of disabled repos by typing in the following:
yum repolist disabled
This will give us an output like this:
The one we’re interested in is “remi-php70”. We want to install PHP using this disabled repository. For any yum installation, we can temporarily enable a disabled repo by using the “–enablerepo=[reponame]” flag. So to install PHP 7 using the disabled repo, we type in the following:
yum --enablerepo=remi-php70 install php
Before confirmation, check and see whether the version of PHP being installed is indeed 7, and that it’s coming from the REMI repo:
You can see that the command is indeed “php” under the “Installing” column and not something like “php70”. So after confirmation, we can verify that we have PHP 7 using the standard “php -v” command:
Now restart Apache to reload the new PHP 7 module:
And now, our “phpinfo” page will render properly:
And we’re done! Once again, this works because we were able to install PHP 7 in a way that allows us to continue using the “php” command instead of something else. It also maintains compatibility with any other scripts or programs you’ve installed that use the “php” command. They will all seamlessly integrate with PHP 7.0 on your CentOS/RHEL server.
Is your website slow?
Enter its URL below to find out now:
- PREVIOUS ENTRY: Linux Script to Add User and Password Expiry with Conditional Checks
- NEXT ENTRY: How to Install suPHP on CentOS 7