Have you tried running latest Apache 2.4 with PHP 7.0 on CentOS 6? Last time i ran into trouble when a customer asked me to update Apache from 2.2.X to 2.4.X running on CentOS 6 with addition of keeping the PHP operational. I’ve checked third party repositories for Apache 2.4 RPMs so i would not have to bother with building it myself and found it quickly. I’ve also found different PHP versions in the same repositories and thought to myself “there, there, i’m almost done!”.
But things got complicated and none of the PHP versions available would run with Apache 2.4.X i’ve installed. Been searching the web for over an hour, testing it out without success and that’s why i’ve decided to write a post on Running latest Apache 2.4 with PHP 7.0 on CentOS 6 to help my readers get by this faster than me 🙂
Procedure: First we need to build latest Apache 2.4 RPMs, once that done we can install Apache 2.4 and last but not least we can compile PHP 7.0 and make it work with our Apache instance.
Environment: I’m working on up2date fresh CentOS release 6.7 (Final) minimal installation with SELinux and IPTables enabled and running.
Running latest Apache 2.4 with PHP 7.0 on CentOS 6
1. Download and Install RPMForge Repository
RPMForge repository is required to download and install all of the dependencies we need to build Apache 2.4 RPMs.
[root@localhost ~]# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
2. Satisfy Software Dependencies
We need to install additional software to satisfy dependencies and development libraries while building Apache 2.4 RPMs and compiling PHP.
[root@localhost ~]# yum install wget rpm-build autoconf zlib-devel libselinux-devel libuuid-devel pcre-devel openldap-devel lua-devel libxml2-devel openssl-devel postgresql-devel mysql-devel sqlite-devel freetds-devel unixODBC-devel nss-devel expat-devel db4-devel mailcap libtool doxygen bzip2-devel libcurl-devel libjpeg-turbo-devel libpng-devel libXpm-devel freetype-devel gmp-devel libicu-devel libmcpp-devel gcc-c++ libmcrypt-devel aspell-devel readline-devel libtidy-devel libxslt-devel
3. Download and Build distcache RPM
Distcache package is also a requirement and it’s quite hard to find so to make it easier you can download it from GeekPeek.Net.
[root@localhost ~]# wget https://geekpeek.net/wp-content/uploads/2016/04/distcache-1.4.5-23.src.rpm
Now build RPMs and install distcache and distcache-devel. Note that the newly build RPMs are located in different folder.
[root@localhost ~]# rpmbuild --rebuild distcache-1.4.5-23.src.rpm [root@localhost ~]# rpm -ivh /root/rpmbuild/RPMS/x86_64/distcache-1.4.5-23.x86_64.rpm /root/rpmbuild/RPMS/x86_64/distcache-devel-1.4.5-23.x86_64.rpm
4. Download Latest Apache 2.4.X and Apr Sources
Current latest available version of Apache is 2.4.20.
[root@localhost ~]# mkdir apache24 [root@localhost ~]# cd apache24/ [root@localhost apache24]# wget http://www.apache.si/httpd/httpd-2.4.20.tar.bz2
We also need to download Apache Portable Runtime and Utility sources.
[root@localhost apache24]# wget http://www.apache.si/apr/apr-1.5.2.tar.bz2 [root@localhost apache24]# wget http://www.apache.si/apr/apr-util-1.5.4.tar.bz2
5. Build and Install Apache Portable Runtime and Utils RPMs
Firstly we need to build APR RPM and install apr and apr-devel packages
[root@localhost apache24]# rpmbuild -tb apr-1.5.2.tar.bz2 [root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/apr-1.5.2-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-devel-1.5.2-1.x86_64.rpm
Now we can proceed to build APR UTIL RPM and install apr-util, apr-util-devel and apr-util-ldap.
[root@localhost apache24]# rpmbuild -tb apr-util-1.5.4.tar.bz2 [root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/apr-util-1.5.4-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-util-devel-1.5.4-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-util-ldap-1.5.4-1.x86_64.rpm
6. Build and Install Apache 2.4 RPMs
We can now proceed and build Apache 2.4.20 RPMs and afterwards install Apache.
[root@localhost apache24]# rpmbuild -tb httpd-2.4.20.tar.bz2
The process built quite a number of Apache RPMs:
Wrote: /root/rpmbuild/RPMS/x86_64/httpd-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/httpd-devel-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/httpd-manual-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/httpd-tools-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/mod_authnz_ldap-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/mod_lua-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/mod_proxy_html-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/mod_socache_dc-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/mod_ssl-2.4.20-1.x86_64.rpm Wrote: /root/rpmbuild/RPMS/x86_64/httpd-debuginfo-2.4.20-1.x86_64.rpm
I will install all of them, you can choose just the ones you need.
[root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/httpd-* /root/rpmbuild/RPMS/x86_64/mod_*
7. Start Apache 2.4.20
Firstly allow access to this machine on port 80 TCP by adding the following line to /etc/sysconfig/iptables and reloading iptables service:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Now we can start Apache for the first time.
[root@localhost apache24]# /etc/init.d/httpd start Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message [ OK ]
We can see that Apache start produced a warning since “ServerName” directive is not set, but Apache 2.4.20 started up successfully and is running.
[root@localhost apache24]# /etc/init.d/httpd status httpd (pid 14317) is running...
We are one step closer to running the latest Apache 2.4 with PHP 7.0 on CentOS 6 🙂 ..continuing with PHP 7.0 installation.
8. Download and Unpack PHP 7
Download the latest version of PHP 7 and unpack it.
[root@localhost apache24]# cd .. [root@localhost ~]# mkdir php7 [root@localhost ~]# cd php7 [root@localhost php7]# wget -O php-7.0.5.tar.gz http://de1.php.net/get/php-7.0.5.tar.gz/from/this/mirror [root@localhost php7]# tar -xvzf php-7.0.5.tar.gz
9. Copy php.ini, Configure, Make and Install PHP
Move to the unpacked PHP directory, copy php.ini-production to /usr/local/lib directory and run configure on extracted php.
[root@localhost php7]# cd php-7.0.5 [root@localhost php-7.0.5]# cp php.ini-production /usr/local/lib/php.ini [root@localhost php-7.0.5]# ./configure --with-libdir=lib64 --prefix=/usr/local --with-layout=PHP --with-pear --with-apxs2 --enable-calendar --enable-bcmath --with-gmp --enable-exif --with-mcrypt --with-mhash --with-zlib --with-bz2 --enable-zip --enable-ftp --enable-mbstring --with-iconv --enable-intl --with-icu-dir=/usr --with-gettext --with-pspell --enable-sockets --with-openssl --with-curl --with-gd --enable-gd-native-ttf --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-freetype-dir=/usr --with-libxml-dir=/usr --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-soap --with-xmlrpc --with-xsl --with-tidy=/usr --with-readline --enable-pcntl --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-shmop --enable-maintainer-zts
Once configure is done “make test” (not obligatory) and “make install”. Note that both these actions might take a while…
[root@localhost php7]# make test [root@localhost php7]# make install
10. AddHandler for .php Files
We need to instruct PHP to handle files with .php extension and pass them to PHP interpreter. We can achieve this by adding two lines to global Apache config file with the commands below:
[root@localhost php-7.0.5]# echo "AddHandler php7-script .php" >> /etc/httpd/conf/httpd.conf [root@localhost php-7.0.5]# echo "AddType text/html .php" >> /etc/httpd/conf/httpd.conf
11. Create PHP Info File
Create a new file in /var/www/html directory called info.php and add the following contents in it:
<?php phpinfo(); ?>
12. Restart Apache
We need to restart or reload Apache for configuration changes to become active.
[root@localhost php-7.0.5]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message [ OK ]
13. Browse PHP Info File
To confirm everything works as expected open up the following URL in your browser: http://ip_address/info.php
Hopefully your server is in the same network and you will be able to access it. Change the ip_address with IP address of your server and PHP info page should open up holding all of the PHP information: