Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

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

Difficulty: 40 / 50 Tweet
php-7-cpanel2

Update: March 21 2018 - Now that Easy Apache 4 is everywhere let's see how to configure it

Since Easy Apache 4 with multi PHP support was released a while ago, this article continued to get lots of traction even if the information in it is no longer useful. In 2015, there was no way to run PHP 7 on a WHM server unless you compiled PHP yourself, but now everything is running smoothly with Easy Apache 4.

If for some misterious reason you don't have EA4 yet, you can install it by running 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

Now let's have a look at how to provision the needed PHP version with Easy Apache 4:

Once you have your desired PHP version installed you should switch to PHP-FPM. Setting it up is very easy in recent versions of WHM - All you need to do is navigate to MultiPHP Manager, select the default system-wide PHP version (PHP 7.X) and then hit Convert All Accounts to PHP-FPM.

With PHP-FPM enabled, in case you want to change global configuration variables, log in via ssh and put your configuration settings in this file: vim /var/cpanel/ApachePHPFPM/system_pool_defaults.yaml. After changing the .yaml fill you will need to rebuild the configuration by running /usr/local/cpanel/scripts/php_fpm_config --rebuild.

Below there's an example configuration file but the full list of variables and possible directives is here.


    _is_present: 1
    pm_max_children: 60
    pm_max_requests: 30
    pm_process_idle_timeout: 10
    /* default php configuration value: */
    php_admin_value_disable_functions: exec
    /*  custom php configuration value: */
    php_admin_value_upload_max_filesize: { name: 'php_admin_value[upload_max_filesize]', value: 64M }

Easy Apache 4 also allows per-domain configuration for PHP-FPM. To make sure settings for one domain will be persistent and will not affect other domains running on the same server you'll need to edit the domain specific configuration file: /var/cpanel/userdata/[username]/[domain].[tld].php-fpm.yaml. Once modified, to rebuild the FPM configuration for only one domain just run: /scripts/php_fpm_config --rebuild --domain=[domain].[tld]

That's it. Now you know how to enable and configure PHP 7.X FPM on a WHM server.

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
        wget http://php.net/distributions/php-7.0.1.tar.gz
        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
        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 "extension=pdo.so" >> /usr/local/php70/lib/php.ini
        echo "extension=pdo_mysql.so" >> /usr/local/php70/lib/php.ini
        echo "zend_extension=opcache.so" >> /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
            </Directory>
        </IfModule>
    

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
        application/x-httpd-php7="php:/usr/local/php70/bin/php-cgi"
    

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
                        </Directory>
                </IfModule>
            

    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