e Learning

How to Install PHP 7 on CentOS 7 With Apache Web Server

In This tutorial we are going to learn how to Install PHP 7 on CentOS 7 Linux Operating System.

PHP 7 still does not come with the official CentOS 7 Software Repository, So we have to either use a Third party repository for PHP 7 or we can build PHP 7 from the source. In This tutorial we are going to Learn Both Methods.

First, we will install PHP 7 using a third party repository, then we will see how to build PHP 7 from the source. And we are using Apache HTTPD Server as our Web Server for CentOS 7 PHP.

How to Install PHP 7 on CentOS 7 With Apache Web Server

Install PHP 7 using Webtatic Repository

The repository we are going to use is called webtatic repository which provides the latest PHP 7 for CentOS 7. So let’s see how it is done.

Step One

Install and Configure Apache Web Server

First step is to Install and Configure Apache Web Server on CentOS 7.

yum install httpd

systemctl start httpd

systemctl enable httpd

Then Add Following Firewalld rules to Allow http Service.

firewall-cmd –permanent –add-service=http

firewall-cmd –permanent –add-service=https

firewall-cmd –reload

Step Two

Add webtatic repository for CentOS 7

Webtatic repository depends on the fedora epel repository, So we will add both webtatic and epel repositories.

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Then run the yum repolist command to make sure repositories been enabled.

yum repolist

Step Three

Install PHP 7 Using yum install Command

First, run yum search command as follows to find which version of PHP 7 is available to Install.

yum search php | grep 7

grep search php version cnetos 7

As you can see in our case PHP package called php70w is now available to install.

yum install php70w php70w-mysql

And finally restart the Apache HTTPD Server using systemctl command.

systemctl restart httpd

Now if you type php -v on the command line you will get the PHP 7 version you installed.

find php version centos 7

To Test the PHP Installation create a .php file with phpinfo() function inside the /var/www/html folder and access via web browser.

Build PHP 7 from Source in CentOS 7

Now lets see how to build and compile PHP 7 from source. We are going to download latest PHP 7 package from the official php.net website and install on CentOS 7. So Follow along.

Step One

Install Apache and Apache APXS

yum install httpd httpd-devel

systemctl start httpd

systemctl enable httpd

Add Firewall Rules

firewall-cmd –permanent –add-service=http

firewall-cmd –permanent –add-service=https

firewall-cmd –reload

Step Two

Install Dependencies

Install following packages that need to compile PHP 7 on CentOS 7.

yum install bison libxml2-devel gcc libtool bzip2

rpm -Uvh http://mirror.yandex.ru/fedora/russianfedora/russianfedora/free/el/releases/7/Everything/x86_64/os/re2c-0.13.5-7.el7.R.x86_64.rpm

Step three

Download PHP 7

Go to Following URL https://secure.php.net/downloads.php and download the latest PHP 7 bz2 or gz installer to your CentOS 7 server.

Step Four

Extract the tar file

Create a new directory called /usr/src/php7 and extract the php 7 tar file to the /usr/src/php7 directory.

mkdir -p /usr/src/php7

tar -jxvf php-7.0.6.tar.bz2 -C /usr/src/php7/

If you downloaded the gz PHP package, then use the -zxvf flag with tar command as follows.

tar -zxvf php-7.0.6.tar.bz2 -C /usr/src/php7/

Step Five

Compile and Install PHP 7

Now move to the folder we extract inside /usr/src/php7/ directory.

cd /usr/src/php7/php-7.0.6/

And run the configuration script with following options as follows.

./configure –with-apxs2=/usr/bin/apxs –prefix=/usr/local/php7 –with-config-file-path=/etc/php7/ –with-mysqli

Then execute the make and make install commands to install PHP 7 on CentOS 7.

make

make install

Also run following two commands to set permissions

libtool –finish /usr/src/php7/php-7.0.6/libs

chmod 755 /usr/lib64/httpd/modules/libphp7.so

Step Six

Create php.ini file for PHP 7

mkdir /etc/php7

cp php.ini-development /etc/php7/php.ini

Step Seven

Create the Apache configuration File PHP 7

touch /etc/httpd/conf.d/php7.conf

Then Open the php7.conf with a text editor and add the following lines.

<FilesMatch \.php$>

SetHandler application/x-httpd-php

</FilesMatch>

Save the configuration file and restart the Apache Web Server.

systemctl restart httpd

And we are done. You can get the details of your installation using the phpinfo() function.

Build PHP 7 from Source in CentOS 7

Ok, This is the end of this tutorial. We learned how to install PHP 7 on CentOS 7 using Webtatic Repository and we also learn how to build PHP 7 from source on CentOS 7 Linux Server.