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 gcc
Let'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/www
Create 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.4
Download 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/src
Install some dependencies
yum install libxml2 libxml2-devel
Change the current directory
cd /usr/local/src/php-7.0.4
Configure the installer
./configure --prefix=/opt/php-7.0.4 --enable-fpm --enable-opcache
Build and install the software
make && make install
Create 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.conf
In 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 = 1
In 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 = 4
Start PHP-FPM
/opt/php-7.0.4/sbin/php-fpm --fpm-config /opt/php-7.0.4/etc/php-fpm.conf
You can stop it later with the following command
pkill php-fpm
It'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.20
Download 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/src
Download 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-devel
Change the current directory
cd /usr/local/src/httpd-2.4.20/
Configure the installer
./configure --prefix=/opt/httpd-2.4.20 --enable-so
Build and install the software
make && make install
In 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/$1
Start Apache
/opt/httpd-2.4.20/bin/apachectl start
You can stop it later by running
/opt/httpd-2.4.20/bin/apachectl stop
Create a test file to test the installations
echo "<?php phpinfo();" >> /var/www/info.php
Done! We now have running Apache and PHP.