Latest PHP7 on CentOS7

Running Webmin/Virtualmin with latest PHP7 on CentOS7

There comes a moment in life when you have to upgrade your software to a newer, major, version. Depending on the software you use, this process can be quick as a button press or you have to struggle to search and document all the breaking changes and adapt your code.

These days I’ve been dealing with this kind of process and it’s about the highly anticipated PHP7.

 

To make it clear, I haven’t coded in PHP for more than 2 years, but I use software (like this blog built on WordPress) that is written with it.

What was wrong with current version of PHP (5.4.16) and WordPress? Nothing! WordPress can run with PHP above 5.2.4, but I have recenlty installed a complete software development platform crafted with love for the open source community by Phacility.

Phabricator needs at least PHP 5.5.0 to enable some performance features, but works with 5.4.16 just fine.  I highly recommend you have a look over Phabricator; it comes with a ton of features incorporated within a single platform. It is a good free alternative to Jira + Confluence + BitBucket + Slack and more!

 

Virtualmin multiple PHP versions

If your system has more PHP versions, Virtualmin can detect them and let you choose the one you need for each hosted server.

My solution was to install an additional PHP version, so I chose to install the latest version from 5.6.x series. The installation and the recognition by Virtualmin went smooth and I could use it for my Phabricator instance. At this point, I had the globally installed PHP (5.4.16) and the additional version (5.6.x) provided by rh-php56 package.

 

By default, newly created Virtualmin servers will use the highest available version (you can find this setting in Virtualmin – Server Templates – Default Settings – Apache website section).

Problem: even though the Phabricator instance was created to use the 5.6.x version (highest available), the configuration from /home/phab.mydomain.com/etc/php5.6/php.ini wasn’t copied from this version (/etc/opt/rh/rh-php56/php.ini), but from the global version (/etc/php.ini).

Solution: manually copy the right configuration file.

 

I think that you have already guessed: I don’t want to do this for all my newly created Virtualmin servers. Two good options here:

  1. Open an issue on Virtualmin forums and wait for the fix.
  2. Upgrade global PHP version to 5.6.x

Of course, the fastest way for resolving this issue was the second option, but then I thought why not changing it a bit? So I jumped into the future and decided to use the latest PHP version.

 

Upgrading PHP 5.4.x to PHP 7.x on CentOS7

Before doing this, make sure that all the software you use on your server is fully compatible with PHP7!
BACKUP FIRST! Before moving further with the upgrade process, please make a full backup of your server or create a new instance from a clone of it and test there first. You're the one accountable for every action and result.

Let’s make some assumptions first:

  1. You have CentOS7 up-to-date (v7.3.1611)
  2. You have Webmin/Virtualmin running an Apache server up-to-date (v1.831/v5.06).

 

The upgrade.

Read every step carefully and don’t hurry!

[1] Enter Virtualmin and stop Apache server.

[2] Add IUS repository and uninstall current PHP version. Each command requires user input, so pay attention to the screen options.

[dummy@host ~]$ sudo rpm -ivh https://centos7.iuscommunity.org/ius-release.rpm
....
[dummy@host ~]$ sudo yum remove php-cli mod_php php-common
....

Removing the current PHP version is a critical step, so before going further or thinking of running the clear command, search for the list of packages you are removing and save them somewhere. It should look like this (dependencies may vary):

Removing:
php                             x86_64            5.4.16-42.el7               @base            4.4 M
php-cli                         x86_64            5.4.16-42.el7               @base            8.8 M
php-common                      x86_64            5.4.16-42.el7               @base            3.8 M
Removing for dependencies:
php-devel                       x86_64            5.4.16-42.el7               @base            4.0 M
php-gd                          x86_64            5.4.16-42.el7               @base            342 k
php-imap                        x86_64            5.4.16-7.el7                @epel            105 k
php-mbstring                    x86_64            5.4.16-42.el7               @base            1.3 M
php-mysql                       x86_64            5.4.16-42.el7               @base            232 k
php-odbc                        x86_64            5.4.16-42.el7               @base             97 k

 

[3] Install PHP 7.x with the same dependencies that were previously installed.

You can identify the latest version from IUS repository by entering here and looking for php7Xu-pkgname; at the time of writing the latest major version is 7.1, so you’ll see that I have used php71u packages.

I have organized these packages by their priority and also linked them to their corresponding package from the old version (this is the reasoning behind saving the list of packages we removed):

# Mandatory
-----------
mod_php71u => php
php71u-cli => php-cli
php71u-common => php-common
# Renamed packages
------------------
php71u-opcache => php-pecl-zendopcache
php71u-mysqlnd => php-mysql
pear1u => php-pear
# Not available packages
------------------------
php71u-pecl-memcache => php-pecl-memcache

The rest of the packages are the same, but the prefix (php71u instead of php). This analysis is made for my list of removed packages, yours might be different, so please do your research before proceeding.

 

Knowing the packages we need, the install command must include the mandatory packages and the rest:

[dummy@host ~]$ sudo yum install mod_php71u php71u-cli php71u-common [....]
....
Installing:
mod_php71u                     x86_64           7.1.1-1.ius.centos7              ius           3.0 M
pear1u                         noarch           1:1.10.1-2.ius.centos7           ius           349 k
php71u-cli                     x86_64           7.1.1-1.ius.centos7              ius           4.5 M
php71u-common                  x86_64           7.1.1-1.ius.centos7              ius           1.1 M
php71u-devel                   x86_64           7.1.1-1.ius.centos7              ius           1.2 M
php71u-gd                      x86_64           7.1.1-1.ius.centos7              ius           171 k
php71u-imap                    x86_64           7.1.1-1.ius.centos7              ius            86 k
php71u-mbstring                x86_64           7.1.1-1.ius.centos7              ius           580 k
php71u-mysqlnd                 x86_64           7.1.1-1.ius.centos7              ius           231 k
php71u-odbc                    x86_64           7.1.1-1.ius.centos7              ius            87 k

Depending on the packages you install, the output will be different.

 

[5] Check your new PHP version!

[dummy@host ~]$ php -v
PHP 7.1.1 (cli) (built: Jan 18 2017 13:45:30) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.1, Copyright (c) 1999-2017, by Zend Technologies

 

[6] Configure Apache server to use PHP7 module.

There are a lot of topics about PHP7 on an Apache server that doesn’t run the *.php files and instead offers them to be downloaded – this also happened to me when I was trying to access WordPress admin page or the homepage of Phabricator.

Because we have a custom package for PHP7, the module name is not mod_php7.c, but mod_php71u.c. The solution is this simple:

[dummy@host ~]$ sudo vim /etc/httpd/conf.d/php.conf

… search for:

 # mod_php options
<IfModule  mod_php7.c>

… replace with:

# mod_php options
<IfModule  mod_php71u.c>

 

[7] Back on Virtualmin.

Since the server setup of PHP7 is done, the only thing that we need is to load the new version in our hosting software and use it!

From Virtualmin dashboard, go to System Settings – Re-Check Configuration and let it run. If the output includes these two lines:

The following PHP versions are available : 7.1.1 (/bin/php-cgi)
PHP versions have changed to 7.1 since last check. Regenerating any missing php.ini files.

… that means we’re all good!

 

[8] Start Apache server!

 

Install PHP 5.6.x as secondary version (optional)

If for any reason you still need a PHP version from the 5.x series, you can install the latest 5.6.x available alongside the global PHP7 verson.

To achieve this, we’ll use the rh-php56 I’ve mentioned before. Briefly, this is how to do it:

[dummy@host ~]$ sudo yum install centos-release-scl
....
[dummy@host ~]$ sudo yum install rh-php56
....
[dummy@host ~]$ /opt/rh/rh-php56/root/bin/php --version
PHP 5.6.25 (cli) (built: Oct 21 2016 18:00:07) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Now, we have both PHP7 and PHP5 installed on our server. For more information about rh-php56 check PHP 5.6 – Software Collections page.

Installation directory is /opt/rh/rh-php56/ and the configuration file can be found at /etc/opt/rh/rh-php56/php.ini.

 

Enable multiple PHP version in Virtualmin.

We’re going to repeat a step we did before for PHP7: Virtualmin – System Settings – Re-Check Configuration. Generated output should contain the following two lines:

The following PHP versions are available : 5.6.25 (/opt/rh/rh-php56/root/usr/bin/php-cgi), 7.1.1 (/bin/php-cgi)
PHP versions have changed to 5.6, 7.1 since last check. Regenerating any missing php.ini files.

Now, from a server’s menu, open Services and you will see the: PHP 5.6 Configuration and PHP 7.1 Configuration items.

Also, from the Server Configuration – PHP Versions you should be able to choose the version you want for Default HTML directory.

 

Edit multiple PHP global configurations from Webmin.

When loading multiple PHP versions in Virtualmin, their global configuration files are not added in the Webmin interface and you have only the main configuration file from /etc/php.ini.

From Webmin panel, go to Others – PHP Configuration and click on Module Config icon from the top-left corner and add the path to the PHP 5.6 configuration file.

The result should look like this:

PHP Configuration - Adding ini files
Webmin PHP Configuration – Adding ini files
PHP Configuration Dashboard
Webmin PHP Configuration Dashboard

 

Note #1: please remember the problem with the configuration file for PHP 5.6 not being copied from the correct location. It still happens. On your newly created Virtualmin server the configuration PHP file for 5.6 version will be the one from PHP7 (/etc/php.ini).

Note #2: in case you don’t want to upgrade your CentOS7 version but still want to use PHP7, then you can install it as an additional PHP version as we did with 5.6. For the rh-php7 package check PHP 7.0 – Software Collection page.

Leave a Reply