Install required tools for compilation
In order to compile PHP from source you need to install few tools and libraries. First you need EPEL repository to be enabled. This repository contains more recent version of packages.
sudo yum install epel-release -y
Once you have it installed execute following command to install required packages
yum install autoconf libtool re2c bison libxml2-devel bzip2-devel libcurl-devel libpng-devel libicu-devel gcc-c++ libmcrypt-devel libwebp-devel libjpeg-devel openssl-devel libxslt-devel -y
Download and unpack PHP Source code
Next step is downloading PHP source code. Easiest option is to download it from GitHub PHP releases. Choose the version you would like to install. In my case it’s 7.2.3. Copy link to tar.gz archive and execute following commands:
curl -O -L https://github.com/php/php-src/archive/php-7.1.18.tar.gz tar -xvf php-7.1.18.tar.gz cd php-src-php-7.1.18/
It will download the archive from GitHub, unpack the sources and change working directory to unpacked sources.
Compile PHP
Now it’s time to compile PHP. First we need to build configure command. In order to do that execute following command:
./buildconf --force
Once configure command is created we can use it to configure PHP installation. This process will enable certain PHP extensions such as PDO, FPM, OPCache, GD library etc. If you need any libraries that are not provided here, you can execute ./configure –help option and check if there is something you need. Following command will install PHP with most common extensions:
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-freetype-dir=/usr/include/freetype2 --disable-short-tags --enable-xml --enable-cli --with-openssl --with-pcre-regex --with-pcre-jit --with-zlib --enable-bcmath --with-bz2 --with-curl --enable-exif --with-gd --enable-intl --with-mysqli --enable-pcntl --with-pdo-mysql --enable-soap --enable-sockets --with-xmlrpc --enable-zip --with-webp-dir --with-jpeg-dir --with-png-dir --enable-json --enable-hash --enable-mbstring --with-mcrypt --enable-libxml --with-libxml-dir --enable-ctype --enable-calendar --enable-dom --enable-fileinfo --with-mhash --with-incov --enable-opcache --enable-phar --enable-simplexml --with-xsl --with-pear
Apart from enabling extensions command above will also set where PHP will be installed. In my case it’s /usr/local/php location. If you will want to remove compiled PHP you will simply have to remove entire directory given under –prefix option.
Next it’s time to compile PHP. Please be aware that it takes few minutes:
make clean make make test
Install compiled PHP
Once PHP is compiled it is time to install it. Simply execute following command:
sudo make install
php.ini and OPCache configuration
Second thing is php.ini file. After installation php.ini file should located in /usr/local/php/lib. This is only the location. After compiling from source You won’t anything there so we need to copy it from uncompressed sources.
cd /usr/local/php/lib cp /tmp/php-src-php-7.1.18/php.ini-development ./php.ini vi php.ini
Add PHP to $PATH
We can do one more thing to make our life easier:) Add PHP executable to PATH, so we’ll be able to call php command from every directory.
echo 'pathmunge /usr/local/php/bin' > /etc/profile.d/php.sh
Execute such command, log out, log in and You’ll be able to execute:
php -v