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 PHP.net.

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

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

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

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

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 PHP.net for the latest one an just the commands as needed, to do this we use this command

Next we do some rebuild checks with

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)

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

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

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

Then add this in it and save the file

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

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

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

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

Make the directory were our pid file will go

Then enable the server and start it with

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 http://www.shaunfreeman.name/installing-nginx-in-centos-7/ then you can upload your OpenCart and install it too.

Hope this helped and worked for you.

  • tcowin

    TIL that I need to restart php-fpm to get php.ini changes to propagate through to the server. HTH someone… 🙂

  • Pingback: Installing PHP 7 on Ubuntu 16.04 | Shaun Freeman()

  • Ren Höek

    Thanks for the guide.

    I am getting errors when I do the rebuild check.

    I am setting up a web server with ISPConfig3 (v3.1) and PHP7 as an additional version. But when I do the rebuild check I get the following errors…

    [root@web php-src]# ./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
    configure: WARNING: you should use –build, –host, –target
    configure: WARNING: invalid host type:
    checking for grep that handles long lines and -e… /usr/bin/grep
    checking for egrep… /usr/bin/grep -E
    checking for a sed that does not truncate output… /usr/bin/sed
    checking build system type… config.sub: missing argument
    Try `config.sub –help’ for more information.
    configure: error: /bin/sh ./config.sub failed
    [root@web php-src]# –with-zlib
    -bash: –with-zlib: command not found

    I am totally new to this and anything outside of the YUM command is almost completely foreign to me, so any help would be greatly appreciated.

    • WeOggy

      Use this line:
      ./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 –with-zlib

  • Ignacio Gutiérrez T.

    Copy php.ini line is wrong. Use:
    cp ./php.ini-production /usr/local/php7/etc/php.ini

    • Isn’t that’s exactly what’s written there? sudo cp -v ./php.ini-production /usr/local/php7/lib/php.ini

      • Ignacio Gutiérrez T.

        Config file path is /usr/local/php/etc php.ini file should be /usr/local/php/etc/php.ini