Installing software in Linux using a package manager is easy as typing "yum install the-name-of-the-thing". But sometimes you want to make deep customizations or make multiple installations of the same software (different PHP or Apache httpd versions for example). Then the only thing you can do is install the software from its source, and this is not that easy. In the following lines you will learn how to install PHP (with PHP-FPM and OPcache) and Apache from source code.
Installation steps
The steps are the same for both PHP and Apache httpd.
- Download the archive containing the source and extract it to some directory
- Install dependencies (if any)
- Configure the installer (enable or disable features)
- Run the installer
- Configure the software
What to expect at the end
After executing the following commands, you will have a fresh PHP installation, a running PHP-FPM daemon and an Apache HTTP server that will be executing PHP files with the help from the PHP-FPM daemon (Apache will proxy PHP files to PHP-FPM).
Prerequisites
Compiling PHP and Apache httpd requires a compiler. Let's install it.
yum install gccLet's create a user that will be used by Apache and PHP and the dir where our files will be located
mkdir /var/www
groupadd www-group
useradd -d /var/www -g www-group -s /bin/false www-user
chown www-user:www-group /var/wwwCreate the dir where the source will be downloaded and extracted (it may already exists).
mkdir /usr/local/src/Installing PHP from source
Create the dir where PHP will be installed
mkdir /opt/php-7.0.4Download the PHP source and extract it to the target directory
wget http://us.php.net/get/php-7.0.4.tar.bz2/from/this/mirror -O /usr/local/src/php-7.0.4.tar.bz2
tar jxf /usr/local/src/php-7.0.4.tar.bz2 -C /usr/local/srcInstall some dependencies
yum install libxml2 libxml2-develChange the current directory
cd /usr/local/src/php-7.0.4Configure the installer
./configure --prefix=/opt/php-7.0.4 --enable-fpm --enable-opcacheBuild and install the software
make && make installCreate the configuration files (or copy the recommended ones) to their proper places
cp /usr/local/src/php-7.0.4/php.ini-production /opt/php-7.0.4/lib/php.ini
cp /opt/php-7.0.4/etc/php-fpm.conf.default /opt/php-7.0.4/etc/php-fpm.confIn the php.ini file (located at /opt/php-7.0.4/lib/php.ini) add the following code
zend_extension=/opt/php-7.0.4/lib/php/extensions/no-debug-non-zts-20151012/opcache.so
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.use_cwd = 0
opcache.validate_timestamps = 0
opcache.save_comments = 0
opcache.load_comments = 0
opcache.enable_file_override = 1In the php-fpm.conf file (located at /opt/php-7.0.4/etc/php-fpm.conf) make some changes.
- Remove the following line (it should be last in the file)
include=/opt/php-7.0.4/etc/php-fpm.d/*.conf- Add the following lines
pid = run/php-fpm.pid
[www]
user = www-user
group = www-group
listen = 127.0.0.1:8999
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4Start PHP-FPM
/opt/php-7.0.4/sbin/php-fpm --fpm-config /opt/php-7.0.4/etc/php-fpm.confYou can stop it later with the following command
pkill php-fpmIt's done! Now we have a PHP installation and a running PHP-FPM daemon. Let's install Apache httpd and point it towards PHP-FPM.
Installing Apache httpd from source
Create the dir where Apache will be installed
mkdir /opt/httpd-2.4.20Download the Apache source and extract it to the target directory
wget http://apache.cbox.biz//httpd/httpd-2.4.20.tar.bz2 -O /usr/local/src/httpd-2.4.20.tar.bz2
tar jxf /usr/local/src/httpd-2.4.20.tar.bz2 -C /usr/local/srcDownload some dependencies
wget http://apache.cbox.biz//apr/apr-1.5.2.tar.bz2 -O /usr/local/src/httpd-2.4.20/srclib/apr-1.5.2.tar.bz2
tar jxf /usr/local/src/httpd-2.4.20/srclib/apr-1.5.2.tar.bz2 -C /usr/local/src/httpd-2.4.20/srclib
mv /usr/local/src/httpd-2.4.20/srclib/apr-1.5.2 /usr/local/src/httpd-2.4.20/srclib/apr
wget http://apache.cbox.biz//apr/apr-util-1.5.4.tar.bz2 -O /usr/local/src/httpd-2.4.20/srclib/apr-util-1.5.4.tar.bz2
tar jxf /usr/local/src/httpd-2.4.20/srclib/apr-util-1.5.4.tar.bz2 -C /usr/local/src/httpd-2.4.20/srclib
mv /usr/local/src/httpd-2.4.20/srclib/apr-util-1.5.4 /usr/local/src/httpd-2.4.20/srclib/apr-util
yum install pcre-develChange the current directory
cd /usr/local/src/httpd-2.4.20/Configure the installer
./configure --prefix=/opt/httpd-2.4.20 --enable-soBuild and install the software
make && make installIn the httpd.conf file (located at /opt/httpd-2.4.20/conf/httpd.conf) make the following changes
- Enable (uncomment) these modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so- Change the user and the group
User www-user
Group www-group- Add the following line at the end
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:8999/var/www/$1Start Apache
/opt/httpd-2.4.20/bin/apachectl startYou can stop it later by running
/opt/httpd-2.4.20/bin/apachectl stopCreate a test file to test the installations
echo "<?php phpinfo();" >> /var/www/info.phpDone! We now have running Apache and PHP.

