Apache and PHP 7 installation on CentOS 7 and RHEL 7

In this tutorial, I am going to show you how to install Apache and PHP 7 on CentOS7 and RHRL7. So let’s start step by step Apache and PHP Installation. This tutorial will very helpful for you if you want to deploy your PHP application with Apache web server.

Step 1:- Setup Yum Repository

First of all, we need to enable EPEL repositories on our CentOS 7 server. You can see epel installation in below:-

[[email protected] ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.qOp7Nj: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:epel-release-7-11 ################################# [100%]
[[email protected] ~]#

Step 2:- Install Apache Server

After successfully EPEL enable on our CentOS7 server. Then we can install Apache and PHP packages on our server. We can use below command to install latest PHP and HTTPD service in our server.

[[email protected] ~]# yum -y install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: del-mirrors.extreme-ix.org
* epel: del-mirrors.extreme-ix.org
* extras: del-mirrors.extreme-ix.org
* updates: del-mirrors.extreme-ix.org
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-80.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
httpd x86_64 2.4.6-80.el7.centos base 2.7 M

Transaction Summary
========================================================================================================================================================================
Install 1 Package

Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
No Presto metadata available for base
httpd-2.4.6-80.el7.centos.x86_64.rpm | 2.7 MB 00:00:16
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-80.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-80.el7.centos.x86_64 1/1

Installed:
httpd.x86_64 0:2.4.6-80.el7.centos

Complete!
[[email protected] ~]#

Step 3:- PHP Installation

Now we need to install required PHP modules for our PHP application. Using below command we can install some mostly used PHP modules on our server.

We can install PHP 7 using below these commands:-

[[email protected] ~]# cd ~
[[email protected] ~]# curl 'https://setup.ius.io/' -o setup-ius.sh
[[email protected] ~]# bash setup-ius.sh
[[email protected] ~]# yum remove php-cli mod_php php-common
[[email protected] ~]# yum -y install mod_php70u php70u-cli php70u-mysqlnd php70u-devel php70u-gd php70u-mcrypt php70u-mbstring php70u-xml php70u-pear

You can find below output of above commands:-

[[email protected] ~]# cd ~
[[email protected] ~]# curl 'https://setup.ius.io/' -o setup-ius.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1914 100 1914 0 0 235 0 0:00:08 0:00:08 --:--:-- 414
[[email protected] ~]#
[[email protected] ~]# bash setup-ius.sh
detected CentOS 7
EL 7 is supported
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: del-mirrors.extreme-ix.org
* epel: del-mirrors.extreme-ix.org
* extras: del-mirrors.extreme-ix.org
* updates: del-mirrors.extreme-ix.org
Package epel-release-7-11.noarch already installed and latest version
Nothing to do
Loaded plugins: fastestmirror, langpacks
ius-release.rpm | 8.1 kB 00:00:00
Examining /var/tmp/yum-root-DYTTJA/ius-release.rpm: ius-release-1.0-15.ius.centos7.noarch
Marking /var/tmp/yum-root-DYTTJA/ius-release.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:1.0-15.ius.centos7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
ius-release noarch 1.0-15.ius.centos7 /ius-release 8.5 k

Transaction Summary
========================================================================================================================================================================
Install 1 Package

Total size: 8.5 k
Installed size: 8.5 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ius-release-1.0-15.ius.centos7.noarch 1/1
Verifying : ius-release-1.0-15.ius.centos7.noarch 1/1

Installed:
ius-release.noarch 0:1.0-15.ius.centos7

Complete!
[[email protected] ~]#
[[email protected] ~]# yum remove php-cli mod_php php-common
Loaded plugins: fastestmirror, langpacks
No Match for argument: php-cli
No Match for argument: mod_php
No Match for argument: php-common
No Packages marked for removal
[[email protected] ~]#
[[email protected] ~]# yum -y install mod_php70u php70u-cli php70u-mysqlnd php70u-devel php70u-gd php70u-mcrypt php70u-mbstring php70u-xml php70u-pear
Loaded plugins: fastestmirror, langpacks
ius | 2.3 kB 00:00:00
ius/x86_64/primary_db | 250 kB 00:00:04
Loading mirror speeds from cached hostfile
* base: del-mirrors.extreme-ix.org
* epel: del-mirrors.extreme-ix.org
* extras: del-mirrors.extreme-ix.org
* ius: hkg.mirror.rackspace.com
* updates: del-mirrors.extreme-ix.org
Resolving Dependencies
--> Running transaction check
---> Package mod_php70u.x86_64 0:7.0.30-1.ius.centos7 will be installed
--> Processing Dependency: php-common(x86-64) = 7.0.30-1.ius.centos7 for package: mod_php70u-7.0.30-1.ius.centos7.x86_64
---> Package php70u-cli.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-devel.x86_64 0:7.0.30-1.ius.centos7 will be installed
--> Processing Dependency: autoconf for package: php70u-devel-7.0.30-1.ius.centos7.x86_64
--> Processing Dependency: automake for package: php70u-devel-7.0.30-1.ius.centos7.x86_64
---> Package php70u-gd.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-mbstring.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-mcrypt.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-mysqlnd.x86_64 0:7.0.30-1.ius.centos7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.0.30-1.ius.centos7 for package: php70u-mysqlnd-7.0.30-1.ius.centos7.x86_64
---> Package php70u-pear.noarch 1:1.10.5-1.ius.centos7 will be installed
--> Processing Dependency: php70u-posix for package: 1:php70u-pear-1.10.5-1.ius.centos7.noarch
---> Package php70u-xml.x86_64 0:7.0.30-1.ius.centos7 will be installed
--> Running transaction check
---> Package autoconf.noarch 0:2.69-11.el7 will be installed
--> Processing Dependency: m4 >= 1.4.14 for package: autoconf-2.69-11.el7.noarch
--> Processing Dependency: perl(Data::Dumper) for package: autoconf-2.69-11.el7.noarch
---> Package automake.noarch 0:1.13.4-3.el7 will be installed
--> Processing Dependency: perl(Thread::Queue) for package: automake-1.13.4-3.el7.noarch
--> Processing Dependency: perl(TAP::Parser) for package: automake-1.13.4-3.el7.noarch
---> Package php70u-common.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-pdo.x86_64 0:7.0.30-1.ius.centos7 will be installed
---> Package php70u-process.x86_64 0:7.0.30-1.ius.centos7 will be installed
--> Running transaction check
---> Package m4.x86_64 0:1.4.16-10.el7 will be installed
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
---> Package perl-Test-Harness.noarch 0:3.28-3.el7 will be installed
---> Package perl-Thread-Queue.noarch 0:3.02-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
mod_php70u x86_64 7.0.30-1.ius.centos7 ius 2.9 M
php70u-cli x86_64 7.0.30-1.ius.centos7 ius 4.3 M
php70u-devel x86_64 7.0.30-1.ius.centos7 ius 1.1 M
php70u-gd x86_64 7.0.30-1.ius.centos7 ius 174 k
php70u-mbstring x86_64 7.0.30-1.ius.centos7 ius 579 k
php70u-mcrypt x86_64 7.0.30-1.ius.centos7 ius 64 k
php70u-mysqlnd x86_64 7.0.30-1.ius.centos7 ius 243 k
php70u-pear noarch 1:1.10.5-1.ius.centos7 ius 352 k
php70u-xml x86_64 7.0.30-1.ius.centos7 ius 212 k
Installing for dependencies:
autoconf noarch 2.69-11.el7 base 701 k
automake noarch 1.13.4-3.el7 base 679 k
m4 x86_64 1.4.16-10.el7 base 256 k
perl-Data-Dumper x86_64 2.145-3.el7 base 47 k
perl-Test-Harness noarch 3.28-3.el7 base 302 k
perl-Thread-Queue noarch 3.02-2.el7 base 17 k
php70u-common x86_64 7.0.30-1.ius.centos7 ius 1.1 M
php70u-pdo x86_64 7.0.30-1.ius.centos7 ius 129 k
php70u-process x86_64 7.0.30-1.ius.centos7 ius 84 k

Transaction Summary
========================================================================================================================================================================
Install 9 Packages (+9 Dependent packages)

Total download size: 13 M
Installed size: 53 M
Downloading packages:
(1/18): automake-1.13.4-3.el7.noarch.rpm | 679 kB 00:00:33
(2/18): mod_php70u-7.0.30-1.ius.centos7.x86_64.rpm | 2.9 MB 00:00:09
(3/18): m4-1.4.16-10.el7.x86_64.rpm | 256 kB 00:00:42
(4/18): perl-Data-Dumper-2.145-3.el7.x86_64.rpm | 47 kB 00:00:01
(5/18): autoconf-2.69-11.el7.noarch.rpm | 701 kB 00:01:22
(6/18): perl-Thread-Queue-3.02-2.el7.noarch.rpm | 17 kB 00:00:00
(7/18): php70u-gd-7.0.30-1.ius.centos7.x86_64.rpm | 174 kB 00:00:02
(8/18): php70u-mbstring-7.0.30-1.ius.centos7.x86_64.rpm | 579 kB 00:00:03
(9/18): php70u-devel-7.0.30-1.ius.centos7.x86_64.rpm | 1.1 MB 00:00:06
(10/18): php70u-mcrypt-7.0.30-1.ius.centos7.x86_64.rpm | 64 kB 00:00:01
(11/18): perl-Test-Harness-3.28-3.el7.noarch.rpm | 302 kB 00:00:11
(12/18): php70u-pdo-7.0.30-1.ius.centos7.x86_64.rpm | 129 kB 00:00:00
(13/18): php70u-process-7.0.30-1.ius.centos7.x86_64.rpm | 84 kB 00:00:00
(14/18): php70u-xml-7.0.30-1.ius.centos7.x86_64.rpm | 212 kB 00:00:01
(15/18): php70u-mysqlnd-7.0.30-1.ius.centos7.x86_64.rpm | 243 kB 00:00:04
(16/18): php70u-common-7.0.30-1.ius.centos7.x86_64.rpm | 1.1 MB 00:00:12
(17/18): php70u-pear-1.10.5-1.ius.centos7.noarch.rpm | 352 kB 00:00:07
(18/18): php70u-cli-7.0.30-1.ius.centos7.x86_64.rpm | 4.3 MB 00:04:30
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 38 kB/s | 13 MB 00:05:53
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php70u-common-7.0.30-1.ius.centos7.x86_64 1/18
Installing : php70u-cli-7.0.30-1.ius.centos7.x86_64 2/18
Installing : php70u-process-7.0.30-1.ius.centos7.x86_64 3/18
Installing : php70u-xml-7.0.30-1.ius.centos7.x86_64 4/18
Installing : php70u-pdo-7.0.30-1.ius.centos7.x86_64 5/18
Installing : m4-1.4.16-10.el7.x86_64 6/18
Installing : perl-Data-Dumper-2.145-3.el7.x86_64 7/18
Installing : autoconf-2.69-11.el7.noarch 8/18
Installing : perl-Thread-Queue-3.02-2.el7.noarch 9/18
Installing : perl-Test-Harness-3.28-3.el7.noarch 10/18
Installing : automake-1.13.4-3.el7.noarch 11/18
Installing : php70u-devel-7.0.30-1.ius.centos7.x86_64 12/18
Installing : php70u-mysqlnd-7.0.30-1.ius.centos7.x86_64 13/18
Installing : 1:php70u-pear-1.10.5-1.ius.centos7.noarch 14/18
Installing : php70u-mcrypt-7.0.30-1.ius.centos7.x86_64 15/18
Installing : mod_php70u-7.0.30-1.ius.centos7.x86_64 16/18
Installing : php70u-gd-7.0.30-1.ius.centos7.x86_64 17/18
Installing : php70u-mbstring-7.0.30-1.ius.centos7.x86_64 18/18
Verifying : php70u-mcrypt-7.0.30-1.ius.centos7.x86_64 1/18
Verifying : php70u-common-7.0.30-1.ius.centos7.x86_64 2/18
Verifying : mod_php70u-7.0.30-1.ius.centos7.x86_64 3/18
Verifying : php70u-process-7.0.30-1.ius.centos7.x86_64 4/18
Verifying : autoconf-2.69-11.el7.noarch 5/18
Verifying : php70u-cli-7.0.30-1.ius.centos7.x86_64 6/18
Verifying : perl-Test-Harness-3.28-3.el7.noarch 7/18
Verifying : automake-1.13.4-3.el7.noarch 8/18
Verifying : perl-Thread-Queue-3.02-2.el7.noarch 9/18
Verifying : perl-Data-Dumper-2.145-3.el7.x86_64 10/18
Verifying : php70u-mysqlnd-7.0.30-1.ius.centos7.x86_64 11/18
Verifying : php70u-gd-7.0.30-1.ius.centos7.x86_64 12/18
Verifying : php70u-mbstring-7.0.30-1.ius.centos7.x86_64 13/18
Verifying : php70u-xml-7.0.30-1.ius.centos7.x86_64 14/18
Verifying : 1:php70u-pear-1.10.5-1.ius.centos7.noarch 15/18
Verifying : m4-1.4.16-10.el7.x86_64 16/18
Verifying : php70u-devel-7.0.30-1.ius.centos7.x86_64 17/18
Verifying : php70u-pdo-7.0.30-1.ius.centos7.x86_64 18/18

Installed:
mod_php70u.x86_64 0:7.0.30-1.ius.centos7 php70u-cli.x86_64 0:7.0.30-1.ius.centos7 php70u-devel.x86_64 0:7.0.30-1.ius.centos7
php70u-gd.x86_64 0:7.0.30-1.ius.centos7 php70u-mbstring.x86_64 0:7.0.30-1.ius.centos7 php70u-mcrypt.x86_64 0:7.0.30-1.ius.centos7
php70u-mysqlnd.x86_64 0:7.0.30-1.ius.centos7 php70u-pear.noarch 1:1.10.5-1.ius.centos7 php70u-xml.x86_64 0:7.0.30-1.ius.centos7

Dependency Installed:
autoconf.noarch 0:2.69-11.el7 automake.noarch 0:1.13.4-3.el7 m4.x86_64 0:1.4.16-10.el7
perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-Test-Harness.noarch 0:3.28-3.el7 perl-Thread-Queue.noarch 0:3.02-2.el7
php70u-common.x86_64 0:7.0.30-1.ius.centos7 php70u-pdo.x86_64 0:7.0.30-1.ius.centos7 php70u-process.x86_64 0:7.0.30-1.ius.centos7

Complete!
[[email protected] ~]#

After successfully PHP installation you can check PHP version using below commands:-

[[email protected] ~]# php -v
PHP 7.0.30 (cli) (built: Apr 26 2018 13:30:35) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
[[email protected] ~]#

Step 4:- Manage Apache Service

After successfully run above commands we have to installed Apache web server, PHP, and PHP required modules. Now we need to start our Apache server and also enable them to auto start on server boot. We can use below commands to enable or start our Apache server like this:-

[[email protected] ~]# systemctl start httpd.service
[[email protected] ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2018-06-02 13:49:33 CEST; 22s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 4479 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─4479 /usr/sbin/httpd -DFOREGROUND
├─4480 /usr/sbin/httpd -DFOREGROUND
├─4481 /usr/sbin/httpd -DFOREGROUND
├─4482 /usr/sbin/httpd -DFOREGROUND
├─4483 /usr/sbin/httpd -DFOREGROUND
└─4484 /usr/sbin/httpd -DFOREGROUND

Jun 02 13:49:33 urclouds.urclouds.local systemd[1]: Starting The Apache HTTP Server...
Jun 02 13:49:33 urclouds.urclouds.local systemd[1]: Started The Apache HTTP Server.
[[email protected] ~]#

You can see above our Apache server is successfully started. Now we need to check our PHP application with Apache server. So let’s verify our PHP service are working properly or not.

Step 5:- Verify PHP application with Apache web server

We need to create simple “phpinfo.php” PHP file in this path /var/www/html/ to verify PHP enabled. This file will be providing status information on php information. That means you PHP application is working with Apache web server. So let’s create phpinfo.php file like below:-

[[email protected] ~]# cat /var/www/html/phpinfo.php
<?php
phpinfo();
?>
[[email protected] ~]#

After file creation we need to go browser and type your Apache server IP like below:-
http://apache-server-IP/phpinfo.php

Then you can find php information. That means your PHP service are working properly with apache server.

That ‘s all, Now we have completed PHP and Apache server installation and configuration on CentOS 7.

6 thoughts on “Apache and PHP 7 installation on CentOS 7 and RHEL 7

  1. Someone necessarily lend a hand to make severely articles I
    might state. That is the first time I frequented your website page
    and to this point? I amazed with the research you made to create this actual put up extraordinary.
    Fantastic activity!

  2. Would you please clarify some things? Your ideas should be accepted everywhere…however..I’d add a few points that work with this. Sincerely- ty for reading.

  3. What a stuff of un-ambiguity and preserveness of precious knowledge concerning unpredicted feelings.

  4. This paragraph will assist the internet people for building up new weblog or
    even a blog from start to end.

Leave a Reply

Your email address will not be published. Required fields are marked *