Tools Blog Learn Quizzes Smile API Log In / Sign Up
Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

You can read this article in: English :: Español :: русский

How to install PHP 7 on a cPanel/WHM driven server without breaking everything

October 17, 2016 Difficulty: 40 / 50 Tweet

Update: Easy Apache 4 works now... so unless there's a reason why you would want to stick with EA3, here's how to upgrade:

Easy Apache 4 comes with 2 great features - PHP 7 out of the box and the ability to choose whichever version of PHP you want to run per virtual host.

To install EA4 run the command below as root:
/scripts/migrate_ea3_to_ea4 --run

Before you update, please check your /etc/yum.conf file and make sure it has plugins=1 in it. Otherwise the build will fail.

If anything goes wrong during the upgrade process you can always go back with /scripts/migrate_ea3_to_ea4 --revert --run

After the build has finished (it takes a while), go into WHM and provision this version: "All PHP Options + OpCache"

If you still need to use Easy Apache 3, you can follow along with the tutorial below from 2015.

Hi there,

In this tutorial I want to share my experience with installing PHP7 on one of my WHM/cPanel driven servers on which I had multiple websites that simply couldn't run a version of PHP that is from this century.

I needed to have PHP 7 up and running for some websites and leave the old ones with the old version of PHP (5.3 I think).

Also I needed to do things in such a way that people would stil be able to update anything in cPanel/WHM ... basically make everything completely separate.

For background, you should know that I tested this on CENTOS 6.7 x86_64 kvm – main with WHM 11.52.1 (build 2).

After some research I had a plan in place so I went straight ahead and compiled PHP 7 from source. Here's how:

        cd /usr/local/
        mkdir php7.0
        cd php7.0
        tar -xvf php-7.0.1.tar.gz
        cd php-7.0.1

        # don't forget to run ./configure --help to see all available options first
        ./configure  --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-libxml --enable-mbstring --enable-pdo=shared --enable-sockets --enable-zip --prefix=/usr/local/php70  --with-curl=/opt/curlssl/ --with-freetype-dir=/usr --with-gd --with-gettext --with-imap=/opt/php_with_imap_client/ --with-imap-ssl=/usr --with-jpeg-dir=/usr --with-kerberos --with-libdir=lib64 --with-libxml-dir=/opt/xml2/ --with-mcrypt=/opt/libmcrypt/ --with-mysqli --with-openssl=/usr --with-openssl-dir=/usr --with-pcre-regex=/opt/pcre --with-pdo-mysql=shared --with-pdo-sqlite=shared --with-pic --with-png-dir=/usr --with-xpm-dir=/usr --with-zlib --with-zlib-dir=/usr

        make test
        make install

        cd /usr/local/php70/bin
        ./php -v

If everything worked fine for you, running ./php -v in the /usr/local/php70/bin folder should give you something like this:

        PHP 7.0.1 (cli) (built: Dec 18 2015 03:53:53) ( NTS )
        Copyright (c) 1997-2015 The PHP Group
        Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

Now let's do some basic configuration on our php.ini file

I only added a few basic extensions here and I relied on the "pre-made" php.ini sample file for production.

        cp -f /usr/local/php7.0/php-7.0.1/php.ini-production /usr/local/php70/lib/php.ini

        echo "" >> /usr/local/php70/lib/php.ini
        echo "" >> /usr/local/php70/lib/php.ini
        echo "" >> /usr/local/php70/lib/php.ini

        # if you need more specific configuration just open up the php.ini file end edit it.

At this point you can delete the /usr/local/php7.0 folder (the source code)

Now let's set up Apache

We need to create a configuration file for apache in order to be able to add the php7 handler so, to do that, open up a new file: /usr/local/apache/conf/php7.conf and paste the code below. Don't forget to modify the "Directory" path regex to suit your needs.

        vi /usr/local/apache/conf/php7.conf
        <IfModule mod_suphp.c>
            <Directory ~ "home/user/public_html/">
                AddType application/x-httpd-php7 .php
                suPHP_AddHandler application/x-httpd-php7

Now let's set up suPHP (assuming that's what you are running in WHM)

To create a new handler in supPHP ... open up suphp.conf: /opt/suphp/etc/suphp.conf. Then add the new handler using the code below, at the end of the handlers list.

        ;Handler for php-scripts
        #... existing handlers are here ... put yours below them

Now let's add our handlers into the EasyApache/cPanel/WHM configuration without breaking anything.

There are two options here. You either go into WHM and edit the post_virtualhost_global.conf file from there or you just run: vi /usr/local/apache/conf/includes/post_virtualhost_global.conf. Add the line below in that file and you should be all done.

        Include /usr/local/apache/conf/php7.conf

Now restart Apache and enjoy!

        service httpd restart 

Update: I noticed people are interested in this, so I also created a screencast while making the changes on one of my servers.

Let me know how this worked for you in the comments section below.

Update: I received a lot of emails/comments from people that had trouble setting this up. So here's a recap:

  1. In WHM --- do you have suExec and suPHP enabled as the default handler in Easy Apache ?
  2. The php7.conf file in /etc/httpd/conf/php7.conf should be kind-of like this:

                <IfModule mod_suphp.c>
                        <Directory ~ "home/something/something/">
                                AddType application/x-httpd-php7 .php
                                suPHP_AddHandler application/x-httpd-php7

    a) The ~ is for a Regular Expression so make sure it works.

    b) The IfModule only loads the handler if mod_suphp is enabled ... so if it doesn't load it is most likely not enabled. Try removing the IF statement.

  3. The ​/etc/httpd/conf/includes/post_virtualhost_global.conf should have the link to the above as the very last include: Include /usr/local/apache/conf/php7.conf

  4. Make sure that /opt/suphp/etc/suphp.conf has this line: application/x-httpd-php7="php:/usr/local/php70/bin/php-cgi"

  5. Attention! If you are running Easy Apache 4, suphp.conf is located in /etc/suphp.conf so add your handler there.

  6. * If the above doesn't help... I won't be able to help with more instructions unless I actually look at the server.

comments powered by Disqus