Compiling PHP 7 on CentOS

Compiling PHP 7 on CentOS

Continuing on this mini series of updates to CentOS brings me to the one, PHP. In this post I will show you how to compile PHP 7 and enable it with php-fpm.

PHP 7 is just awesome and I have been using it in production now for about a month and the performance increases are just out of here! Why not checkout the new features on

The reasons I am compiling PHP instead of installing from the package repositories is I like control over where I put the binaries and also it's not in the repos yet and not to mention as PHP developers we have forgotten how to do this and it's good to know. So let's get down to action first we need all the dependencies to compile source code  so in your terminal

sudo yum install git gcc gcc-c++ libxml2-devel pkgconfig openssl-devel bzip2-devel curl-devel libpng-devel libjpeg-devel libXpm-devel freetype-devel gmp-devel libmcrypt-devel mariadb-devel aspell-devel recode-devel autoconf bison re2c libicu-devel

This will install the development packages and libraries will will need. next we make the directory where we are going to put PHP I normally choose '/usr/local/php7', instead of making a directory called 'php' I append the version number I am installing doing this will makes things easier if we want different versions of PHP installed so type

sudo mkdir /usr/local/php7

Now we need the PHP source code I get it directly from the PHP Github repository doing so means I know where the code is from but also I can quickly switch versions to compile against and also get the latest release easier so make sure you are in the root of your home directory with cd ~ then in the terminal type

git clone

This will download the source code and put in the directory '~/php-src'. Once done change your working directory to this by

cd php-src

Next we next to checkout the version we want. WE do this because we want a stable release and not the unstable development branch so at the time of writing this the latest was 7.0.2 checkout for the latest one an just the commands as needed, to do this we use this command

git checkout PHP-7.0.2

Next we do some rebuild checks with

./buildconf --force
./configure --prefix=/usr/local/php7 \
    --with-config-file-path=/usr/local/php7/etc \
    --with-config-file-scan-dir=/usr/local/php7/etc/conf.d \
    --enable-bcmath \
    --with-bz2 \
    --with-curl \
    --enable-filter \
    --enable-fpm \
    --with-gd \
    --enable-gd-native-ttf \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --enable-intl \
    --enable-mbstring \
    --with-mcrypt \
    --enable-mysqlnd \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-pdo-sqlite \
    --disable-phpdbg \
    --disable-phpdbg-webhelper \
    --enable-opcache \
    --with-openssl \
    --enable-simplexml \
    --with-sqlite3 \
    --enable-xmlreader \
    --enable-xmlwriter \
    --enable-zip \ 

This will configure the php build with these basic extensions if you need others you will need to add with not forgetting to install the development packages so you can compile PHP against them see the PHP manual for more details. Once this has done we will have to compile PHP so to do this we use the 'make' command when doing this we will use the '-j' switch to specify how many cpu cores to use when compiling, if you have more than one cpu this will greatly speed things up, without specifying the 'make' will use just one core so as I have two cores I use (increase your number for more)

make -j2

On my server this took about 5½ minutes, the time this takes will depend on the speed and number of your cpus so go and make a coffee and relax.

When your have finished building PHP we have to install it so we use

sudo make install

If you ever have to recompile PHP you issue the command make clean and redo the last three steps.

Now we need to configure PHP to use FPM and add ini options so in your terminal add these commands

sudo mkdir /usr/local/php7/etc/conf.d
sudo cp -v ./php.ini-production /usr/local/php7/lib/php.ini
sudo cp -v ./sapi/fpm/www.conf /usr/local/php7/etc/php-fpm.d/www.conf
sudo cp -v ./sapi/fpm/php-fpm.conf /usr/local/php7/etc/php-fpm.conf

This copies the config files to our php7 directory and now want PHP to use opcache so we make the ini file for by

sudo nano /usr/local/php7/etc/conf.d/modules.ini

Then add this in it and save the file

# Zend OPcache

We also need to edit the FPM pool to run php-fpm as our user from the last post so

sudo nano /usr/local/php7/etc/php-fpm.d/www.conf

and change these lines to suit the user you configured mine is 'cents' so my lines would be

user = centos
group = centos

listen = /var/run/php-fpm.sock

listen.owner = nginx = nginx

Now we have don't that we will a symlink the system sbin to out php-fpm binary

sudo ln -s /usr/local/php7/sbin/php-fpm /usr/sbin/php-fpm

Now make a server so we can start it at boot time so make and open the file sudo nano /usr/lib/systemd/system/php-fpm.service then put this into it

Description=The PHP FastCGI Process Manager

ExecStart=/usr/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID


Make the directory were our pid file will go

sudo mkdir /run/php-fpm

Then enable the server and start it with

chkconfig --levels 235 php-fpm on
systemctl start php-fpm

And that should be it. You can check it by putting a php file in your web root with in it


Now navigate to this file in your browser and you should see info on your php installation, if not then you may need to check the logs for errors. And if you followed my last post on then you can upload your OpenCart and install it too.

Hope this helped and worked for you.

26/01/2016 11:27:00 Shaun Freeman Filed Under: Linux CentOS, Linux, PHP, php-fpm

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

Working on new twitter updates for Uthando CMS.

Shaun Freeman @Zendmaster

I liked a @YouTube video The Josephus Problem - Numberphile

Shaun Freeman @Zendmaster

I liked a @YouTube video Blown Like the Wind

Shaun Freeman @Zendmaster

I liked a @YouTube video Patrick Stewart talks about meeting Sting on the set of DUNE (Funny to the EXTREME)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist Anonymous - This will Change Everything You Know... (2018-2019)