How To install Apache and PHP from source on CentOS 6
To install Apache and PHP from source on CentOS 6
Apache is a web server used to server web page content over HTTP and HTTPS protocol. For the easy installation of Apache web server. PHP is a server scripting language used for the development of software applications with the help of internet. It is a powerful tool to create dynamic contents for interaction with databases.
To install Apache and PHP from source
At the end of this tutorial you will be accustomed to the following things.
- Download, compile and install Apache HTTP Server, APR and APR-Util
- Download, compile and install PHP
- Configure Apache to leverage the new PHP installation
First, you need to install epel-release by making use of the following command. It installs all the required packages for the installation of Apache.
[root@linuxhelp ~]# yum install epel-release -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.vbctv.in
* extras: mirrors.vonline.vn
* updates: mirrors.viethosting.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:6-8 will be installed
--> Finished Dependency Resolution
.
.
.
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : epel-release-6-8.noarch 1/1
Verifying : epel-release-6-8.noarch 1/1
Installed:
epel-release.noarch 0:6-8
Complete!
Once it is done, you need to install few other development tools as well.
[root@linuxhelp ~]# yum groupinstall " Development Tools" -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
* base: centos.myfahim.com
* epel: mirror.ehost.vn
* extras: centos.myfahim.com
* updates: centos.myfahim.com
epel/group_gz | 150 kB 00:00
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package patch-2.6-6.el6.x86_64 already installed and latest version
Package 1:pkgconfig-0.23-9.1.el6.x86_64 already installed and latest version
Package gettext-0.17-18.el6.x86_64 already installed and latest version
Package elfutils-0.164-2.el6.x86_64 already installed and latest version
Package cvs-1.11.23-16.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package binutils.x86_64 0:2.20.51.0.2-5.46.el6 will be updated
---> Package binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1 will be an update
---> Package bison.x86_64 0:2.4.1-5.el6 will be installed
---> Package byacc.x86_64 0:1.9.20070509-7.el6 will be installed
.
.
.
Dependency Installed:
cloog-ppl.x86_64 0:0.15.7-1.2.el6 cpp.x86_64 0:4.4.7-18.el6 gettext-devel.x86_64 0:0.17-18.el6 gettext-libs.x86_64 0:0.17-18.el6
kernel-devel.x86_64 0:2.6.32-696.13.2.el6 libgcj.x86_64 0:4.4.7-18.el6 libgfortran.x86_64 0:4.4.7-18.el6 libstdc++-devel.x86_64 0:4.4.7-18.el6
mpfr.x86_64 0:2.4.1-6.el6 perl-Compress-Raw-Zlib.x86_64 1:2.021-144.el6 perl-Compress-Zlib.x86_64 0:2.021-144.el6 perl-Error.noarch 1:0.17015-4.el6
perl-Git.noarch 0:1.7.1-9.el6_9 perl-HTML-Parser.x86_64 0:3.64-2.el6 perl-HTML-Tagset.noarch 0:3.20-4.el6 perl-IO-Compress-Base.x86_64 0:2.021-144.el6
perl-IO-Compress-Zlib.x86_64 0:2.021-144.el6 perl-URI.noarch 0:1.40-2.el6 perl-XML-Parser.x86_64 0:2.36-7.el6 perl-libwww-perl.noarch 0:5.833-5.el6
ppl.x86_64 0:0.10.2-11.el6 systemtap-client.x86_64 0:2.9-7.el6 systemtap-devel.x86_64 0:2.9-7.el6
Updated:
binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1
Complete!
Now install pcre and pcre-devel packages which is also required for Apache installation by running the following command.
[root@linuxhelp ~]# yum install expat-devel pcre pcre-devel openssl-devel -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: centos.myfahim.com
* epel: epel.scopesky.iq
* extras: centos.myfahim.com
* updates: centos.myfahim.com
Package pcre-7.8-7.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package expat-devel.x86_64 0:2.0.1-13.el6_8 will be installed
---> Package openssl-devel.x86_64 0:1.0.1e-57.el6 will be installed
--> Processing Dependency: zlib-devel for package: openssl-devel-1.0.1e-57.el6.x86_64
--> Processing Dependency: krb5-devel for package: openssl-devel-1.0.1e-57.el6.x86_64
---> Package pcre-devel.x86_64 0:7.8-7.el6 will be installed
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.10.3-65.el6 will be installed
.
.
.
Installed:
expat-devel.x86_64 0:2.0.1-13.el6_8 openssl-devel.x86_64 0:1.0.1e-57.el6 pcre-devel.x86_64 0:7.8-7.el6
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-65.el6 libcom_err-devel.x86_64 0:1.41.12-23.el6 libselinux-devel.x86_64 0:2.0.94-7.el6 libsepol-devel.x86_64 0:2.0.41-4.el6
zlib-devel.x86_64 0:1.2.3-29.el6
Complete!
And now, download the tar package of Apache version 2.4 along with apr and apr-util using wget command in the following manner.
[root@linuxhelp ~]# wget https://github.com/apache/httpd/archive/2.4.28.tar.gz -O httpd-2.4.28.tar.gz --2017-10-11 05:59:10-- https://github.com/apache/httpd/archive/2.4.28.tar.gz Resolving github.com... 192.30.255.112, 192.30.255.113 Connecting to github.com|192.30.255.112|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/apache/httpd/tar.gz/2.4.28 [following] --2017-10-11 05:59:11-- https://codeload.github.com/apache/httpd/tar.gz/2.4.28 Resolving codeload.github.com... 192.30.255.120, 192.30.255.121 Connecting to codeload.github.com|192.30.255.120|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/x-gzip] Saving to: “ httpd-2.4.28.tar.gz” [ < => ] 11,489,746 814K/s in 36s 2017-10-11 05:59:49 (314 KB/s) - “ httpd-2.4.28.tar.gz” saved [11489746] [root@linuxhelp ~]# wget https://github.com/apache/apr/archive/1.6.2.tar.gz -O apr-1.6.2.tar.gz --2017-10-11 05:59:54-- https://github.com/apache/apr/archive/1.6.2.tar.gz Resolving github.com... 192.30.255.113, 192.30.255.112 Connecting to github.com|192.30.255.113|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/apache/apr/tar.gz/1.6.2 [following] --2017-10-11 05:59:55-- https://codeload.github.com/apache/apr/tar.gz/1.6.2 Resolving codeload.github.com... 192.30.255.120, 192.30.255.121 Connecting to codeload.github.com|192.30.255.120|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/x-gzip] Saving to: “ apr-1.6.2.tar.gz” [ < => ] 730,808 351K/s in 2.0s 2017-10-11 05:59:58 (351 KB/s) - “ apr-1.6.2.tar.gz” saved [730808]
[root@linuxhelp ~]# wget https://github.com/apache/apr-util/archive/1.6.0.tar.gz -O apr-util-1.6.0.tar.gz
--2017-10-11 06:00:09-- https://github.com/apache/apr-util/archive/1.6.0.tar.gz
Resolving github.com... 192.30.255.112, 192.30.255.113
Connecting to github.com|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/apache/apr-util/tar.gz/1.6.0 [following]
--2017-10-11 06:00:11-- https://codeload.github.com/apache/apr-util/tar.gz/1.6.0
Resolving codeload.github.com... 192.30.255.121, 192.30.255.120
Connecting to codeload.github.com|192.30.255.121|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: “ apr-util-1.6.0.tar.gz”
[ < => ] 434,431 130K/s in 3.3s
2017-10-11 06:00:15 (130 KB/s) - “ apr-util-1.6.0.tar.gz” saved [434431]
Once all the three versions are downloaded, you need to extract those tar packages by making use of the following command.
[root@linuxhelp ~]# tar -xzf httpd-2.4.28.tar.gz
[root@linuxhelp ~]# tar -xzf apr-1.6.2.tar.gz
[root@linuxhelp ~]# tar -xzf apr-util-1.6.0.tar.gz
Rename the extracted directory as per your convenience.
[root@linuxhelp ~]# mv apr-1.6.2 httpd-2.4.28/srclib/apr
[root@linuxhelp ~]# mv apr-util-1.6.0 httpd-2.4.28/srclib/apr-util
And then move inside the extracted directory as follows.
[root@linuxhelp ~]# cd httpd-2.4.28
And then build your configuration through the following command.
[root@linuxhelp httpd-2.4.28]# ./buildconf
found apr source: srclib/apr
found apr-util source: srclib/apr-util
rebuilding srclib/apr/configure
buildconf: checking installation...
buildconf: python version 2.6.6 (ok)
buildconf: autoconf version 2.63 (ok)
buildconf: libtool version 2.2.6b (ok)
.
.
.
rebuilding rpm spec file
copying build files
rebuilding include/ap_config_auto.h.in
rebuilding configure
rebuilding rpm spec file
fixing timestamps for ap_expr sources
Next, run the configure command as shown below. You can change the default Apache installation directory path, here we have mentioned /usr/local/apache2 directory path.
[root@linuxhelp httpd-2.4.28]# ./configure --enable-ssl --enable-so --with-mpm=event --with-included-apr --prefix=/usr/local/apache2
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
configuring package in srclib/apr now
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
.
.
.
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
configure: summary of build options:
Server Version: 2.4.28
Install prefix: /usr/local/apache2
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
LDFLAGS:
LIBS:
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
C preprocessor: gcc &ndash E
Once it is done, you shall proceed with the make and make install commands which completes the Apache installation.
[root@linuxhelp httpd-2.4.28]# make
Making all in srclib
make[1]: Entering directory `/root/httpd-2.4.28/srclib'
Making all in apr
make[2]: Entering directory `/root/httpd-2.4.28/srclib/apr'
make[3]: Entering directory `/root/httpd-2.4.28/srclib/apr'
/root/httpd-2.4.28/srclib/apr/build/mkdir.sh tools
/bin/sh /root/httpd-2.4.28/srclib/apr/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/httpd-2.4.28/srclib/ap
.
.
.
.
make[4]: Leaving directory `/root/httpd-2.4.28/modules/mappers'
make[3]: Leaving directory `/root/httpd-2.4.28/modules/mappers'
make[2]: Leaving directory `/root/httpd-2.4.28/modules'
make[2]: Entering directory `/root/httpd-2.4.28/support'
make[2]: Leaving directory `/root/httpd-2.4.28/support'
make[1]: Leaving directory `/root/httpd-2.4.28'
[root@linuxhelp httpd-2.4.28]# make install
Making install in srclib
make[1]: Entering directory `/root/httpd-2.4.28/srclib'
Making install in apr
make[2]: Entering directory `/root/httpd-2.4.28/srclib/apr'
make[3]: Entering directory `/root/httpd-2.4.28/srclib/apr'
make[3]: Nothing to be done for `local-all' .
make[3]: Leaving directory `/root/httpd-2.4.28/srclib/apr'
/root/httpd-2.4.28/srclib/apr/build/mkdir.sh /usr/local/apache2/lib /usr/local/apache2/bin /usr/local/apache2/build
/usr/local/apache2/lib/pkgconfig /usr/local/apache2/include
.
.
.
.
mkdir /usr/local/apache2/man
mkdir /usr/local/apache2/man/man1
mkdir /usr/local/apache2/man/man8
mkdir /usr/local/apache2/manual
make[1]: Leaving directory `/root/httpd-2.4.28'
Now the Apache is successfully installed on the server from it' s source code. Next run the httpd -v command to check Apache version. A error may appear as shown below.
[root@linuxhelp ~]# httpd -v
bash: httpd: command not found
To remove that error, create a HTTPD commad by running the following command.
[root@linuxhelp ~]# vim /etc/profile.d/httpd.sh
pathmunge /usr/local/apache2/bin
Create a script file for httpd command under /etc/profile.d/ directory. Enter the following line in the file and save it
[root@linuxhelp ~]# vim /etc/init.d/httpd
#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server. It is used to serve
# HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /etc/httpd/conf/httpd.conf
# Source function library.
. /etc/rc.d/init.d/functions
# See how we were called.
case " $1" in
start)
echo -n " Starting httpd: "
daemon /usr/local/apache2/bin/httpd -DSSL
echo
touch /var/lock/subsys/httpd
stop)
echo -n " Shutting down httpd: "
killproc httpd
echo
rm -f /var/lock/subsys/httpd
rm -f /var/run/httpd.pid
status)
status httpd
restart)
$0 stop
$0 start
reload)
echo -n " Reloading httpd: "
killproc httpd -HUP
echo
*)
echo " Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
After that, you should start and enable your apache service.
[root@linuxhelp ~]# service httpd start Starting httpd: [ OK ] [root@linuxhelp ~]# chkconfig httpd on
Move to your browser and type http://yourIP.
Now, let us install PHP 7.1 from its source, but before that, you need to install certain required packages and for that you need to run the following command.
[root@linuxhelp ~]# yum install libxml2-devel -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.vbctv.in
* epel: mirror.ehost.vn
* extras: mirrors.vonline.vn
* updates: mirrors.viethosting.com
Resolving Dependencies
--> Running transaction check
---> Package libxml2-devel.x86_64 0:2.7.6-21.el6_8.1 will be installed
--> Finished Dependency Resolution
.
.
.
.
Running Transaction
Installing : libxml2-devel-2.7.6-21.el6_8.1.x86_64 1/1
Verifying : libxml2-devel-2.7.6-21.el6_8.1.x86_64 1/1
Installed:
libxml2-devel.x86_64 0:2.7.6-21.el6_8.1
Complete!
You can now download the source package of PHP using wget command in the following manner.
[root@linuxhelp ~]# wget http://in1.php.net/distributions/php-7.1.9.tar.gz
--2017-11-16 14:03:38-- http://in1.php.net/distributions/php-7.1.9.tar.gz
Resolving in1.php.net... 103.21.58.218
Connecting to in1.php.net|103.21.58.218|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19428408 (19M) [application/x-gzip]
Saving to: “ php-7.1.9.tar.gz”
100%[===================================================================================================================> ] 19,428,408 1.10M/s in 17s
2017-11-16 14:03:55 (1.10 MB/s) - “ php-7.1.9.tar.gz” saved [19428408/19428408]
Extract the downloaded package as follows.
[root@linuxhelp ~]# tar -xzf php-7.1.9.tar.gz
And then move inside the extracted directory by making use of the following command.
[root@linuxhelp ~]# cd php-7.1.9
Once it is done, you need to make the following configuration.
[root@linuxhelp php-7.1.9]# ./configure --with-apxs2=/usr/local/apache2/bin/apxs
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
.
.
.
.
Thank you for using PHP.
config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
And then, execute make and make install commands which prepares and completes the installation process.
[root@linuxhelp php-7.1.9]# make
/bin/sh /root/php-7.1.9/libtool --silent --preserve-dup-deps --mode=compile /root/php-7.1.9/meta_ccld -Iext/date/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1 -Iext/date/ -I/root/php-7.1.9/ext/date/ -DPHP_ATOM_INC -I/root/php-7.1.9/include -I/root/php-7.1.9/main -I/root/php-7.1.9 -I/root/php-7.1.9/ext/date/lib -I/usr/include/libxml2 -I/root/php-7.1.9/ext/sqlite3/libsqlite -I/root/php-7.1.9/TSRM -I/root/php-7.1.9/Zend -D_REENTRANT -I/usr/include -g -O2 -fvisibility=hidden -pthread -DZTS -DZEND_SIGNALS -c /root/php-7.1.9/ext/date/php_date.c -o ext/date/php_date.lo
/bin/sh /root/php-7.1.9/libtool --silent --preserve-dup-deps --mode=compile /root/php-7.1.9/meta_ccld -Iext/date/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1 -Iext/date/ -I/root/php-7.1.9/ext/date/ -DPHP_ATOM_INC -I/root/php-7.1.9/include -I/root/php-7.1.9/main -I/root/php-7.1.9 -I/root/php-7.1.9/ext/date/lib -I/usr/include/libxml2 -I/root/php-7.1.9/ext/sqlite3/libsqlite -I/root/php-7.1.9/TSRM -I/root/php-7.1.9/Zend -D_REENTRANT -I/usr/include
.
.
.
.
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP' s phar extension be enabled.
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
phar.inc
Build complete.
[root@linuxhelp php-7.1.9]# make install
Installing PHP SAPI module: apache2handler
/usr/local/apache2/build/instdso.sh SH_LIBTOOL=' /usr/local/apache2/build/libtool' libphp7.la /usr/local/apache2/modules
/usr/local/apache2/build/libtool --mode=install install libphp7.la /usr/local/apache2/modules/
libtool: install: install .libs/libphp7.so /usr/local/apache2/modules/libphp7.so
libtool: install: install .libs/libphp7.lai /usr/local/apache2/modules/libphp7.la
libtool: install: warning: remember to run `libtool --finish /root/php-7.1.9/libs'
chmod 755 /usr/local/apache2/modules/libphp7.so
[activating module `php7' in /usr/local/apache2/conf/httpd.conf]
.
.
.
.
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/root/php-7.1.9/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers: /usr/local/include/php/ext/pdo/
To check the Version of the PHP you' ve just installed.
[root@linuxhelp php-7.1.9]# php -v
PHP 7.1.9 (cli) (built: Nov 16 2017 14:10:32) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
And then, grep the libs directory. Move the .so file to the Apache root document location.
[root@linuxhelp php-7.1.9]# ls -l | grep libs
drwxr-xr-x 2 root root 4096 Nov 16 14:10 libs
[root@linuxhelp php-7.1.9]# cd libs
[root@linuxhelp libs]# ls -l
total 30200
-rw-r--r-- 1 root root 1082 Nov 16 14:10 libphp7.la
-rwxr-xr-x 1 root root 30917077 Nov 16 14:10 libphp7.so
Create a .conf file for Apache, and open the file using the vim editor.
[root@linuxhelp httpd-2.4.28]# vim /usr/local/apache2/conf/httpd.conf
And in that file, make the following modifications.
[root@linuxhelp httpd-2.4.28]# cd /usr/local/apache2/htdocs/
[root@linuxhelp htdocs]# vim info.php
< ?php
phpinfo()
?>
And then, you need to restart your Apache service.
[root@linuxhelp htdocs]# service httpd restart
Switch to your browser and type http://yourIP /info.php. You will see the following details.
&emsp
Comments ( 1 )
/etc/init.d/httpd: line 23: syntax error near unexpected token `)'
/etc/init.d/httpd: line 23: `stop)'
I got this error while starting apache