Installing PHP 5.6.x and 7.0.x with Ubuntu 14.04 and Virtualmin 5
Updated with new information regarding having PHP 5.6.x and PHP7.0.x work with the creation of new sites
This tutorial shows you how to install PHP 5.6.x and PHP 7.0.x side by side on Ubuntu 14.04 with Virtualmin using FCGI and assumes that you already have Virtualmin configured on a server. Please read through this entire tutorial before starting.
Note: As always backup your servers or sites. I accept no liability for loss of data, problems, or damage caused to your server by following this tutorial. This worked for me but – use at your own risk.
Adding the PPA
The first thing you’ll need to do is add the ppa that lets you install PHP 5.6.x and PHP 7.0.x side by side. The following ppa allows both PHP 5.6.x and PHP 7.0.x to run side-by-side. Type or paste the following:
sudo add-apt-repository ppa:ondrej/php
Once you enter the above command You should see something similar to the following when you add the repository.
Co-installable PHP versions: PHP 5.6, PHP 7.0 and batteries included. You can get more information about the packages at https://deb.sury.org For PHP 5.6 use: ppa:ondrej/php5-5.6 For PHP 5.5 use: ppa:ondrej/php5 For PHP 5.4 use: ppa:ondrej/php5-oldstable BUGS&FEATURES: This PPA now has a issue tracker: https://deb.sury.org/pages/bugreporting.html PLEASE READ: If you like my work and want to give me a little motivation, please consider donating: https://deb.sury.org/pages/donate.html WARNING: add-apt-repository is broken with non-UTF-8 locales, see https://github.com/oerdnj/deb.sury.org/issues/56 for workaround: # apt-get install -y language-pack-en-base # LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php More info: https://launchpad.net/~ondrej/+archive/ubuntu/php Press [ENTER] to continue or ctrl-c to cancel adding it
Press enter when prompted or ctrl-c if you change your mind. If you press enter the server should respond with the following output:
gpg: keyring `/tmp/tmp6l1myhl1/secring.gpg' created gpg: keyring `/tmp/tmp6l1myhl1/pubring.gpg' created gpg: requesting key E5267A6C from hkp server keyserver.ubuntu.com gpg: /tmp/tmp6l1myhl1/trustdb.gpg: trustdb created gpg: key E5267A6C: public key "Launchpad PPA for Ondřej Surý" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1) OK
Next you will need to update and upgrade the system before we continue. This should ensure that you have the latest Webmin, Usermin and Virtualmin if you haven’t already updated them. I’m going to use aptitude because I want to easily see what the options are for any conflicts. Type or paste the following two lines.
sudo aptitude update sudo aptitude full-upgrade
You should hopefully see something more or less like the following, depending on if we’ve updated recently we may only see the bit about new packages and removing packages without any upgrades listed.
Current status: 10 updates [+5], 1622 new [+199]. The following NEW packages will be installed: libmemcached11{a} libqdbm14{a} libssl1.0.2{a} php-cli{a} php-common{a} php-readline{a} php7.0-cli{ab} php7.0-common{ab} php7.0-json{a} php7.0-opcache{a} php7.0-readline{a} The following packages will be upgraded: libgnutls-openssl27 libgnutls26 libmemcachedutil2 libpng12-0 libssl-dev libssl-doc openssl php-pear usermin webmin-virtual-server 10 packages upgraded, 11 newly installed, 0 to remove and 0 not upgraded. Need to get 26.2 MB of archives. After unpacking 25.0 MB will be used. The following packages have unmet dependencies: php7.0-cli : Breaks: php5-cli (< 5.6.16+dfsg-4~) but 5.5.9+dfsg-1ubuntu4.14 is installed. php7.0-common : Conflicts: php5-common (< 5.6.16+dfsg-4~) but 5.5.9+dfsg-1ubuntu4.14 is installed. The following actions will resolve these dependencies: Remove the following packages: 1) php-pear Keep the following packages at their current version: 2) php-cli [Not Installed] 3) php-readline [Not Installed] 4) php7.0-cli [Not Installed] 5) php7.0-common [Not Installed] 6) php7.0-json [Not Installed] 7) php7.0-opcache [Not Installed] 8) php7.0-readline [Not Installed] Accept this solution? [Y/n/q/?]
Go ahead and accept this solution, PHP will still be at the existing version (PHP 5.5.9) but you’ll change that in the next section.
Installing PHP 5.6.x and PHP 7.0.x
Once you’ve updated your repositories and upgraded your system to ensure you have the latest version of Virtualmin you are ready to install PHP 5.6.x and PHP 7.0.x. Type or paste the next code section and press enter. This will remove and replace the default PHP 5.5.9 packages that come with Ubuntu 14.04 with the versions from the ppa.
sudo aptitude install pkg-php-tools php5.6 php5.6-cgi php5.6-cli php5.6-dbg php5.6-common php5.6-curl php5.6-gd php5.6-imap php5.6-intl php5.6-mysql php5.6-pspell php5.6-sqlite3 php5.6-tidy php5.6-opcache php5.6-json php5.6-bz2 php5.6-mcrypt php5.6-readline php5.6-xmlrpc php5.6-enchant php5.6-xsl php-all-dev php7.0 php7.0-cgi php7.0-cli php7.0-common php7.0-dbg php7.0-curl php7.0-enchant php7.0-gd php7.0-imap php7.0-intl php7.0-ldap php7.0-mcrypt php7.0-readline php7.0-pspell php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-json php7.0-sqlite3 php7.0-mysql php7.0-opcache php7.0-bz2 libapache2-mod-php7.0
Check and accept the solution offered unless it looks like it will break something else in your system. Again, this will remove and replace the default PHP 5.5.9 packages. Once the system has completed installing and configuring PHP check to see if you have a new php-cli running with the following command:
php -v
Following these instructions should yield PHP 7.0.x on the command line and you should see the following (depending on PHP version):
HP 7.0.2-1+deb.sury.org~trusty+1 (cli) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
Configuring files and paths for Virtualmin
Next you will need to move some files into place for Virtualmin to have access to php.ini files per server. This may be necessary for you depending on how you want manage you sites. If you don’t need individual php.ini files you can skip down to Detected php-cgi versions below.
Virtual Server php.ini files
Copy php.ini files to home directories and remember to change to the name of the virtualmin home/directory and user.
sudo mkdir /home//etc/php5.6 sudo mkdir /home//etc/php7 sudo mkdir /home//etc/php7.0 sudo cp /etc/php/5.6/cgi/php.ini /home//etc/php5.6/ sudo cp /etc/php/7.0/cgi/php.ini /home//etc/php7/ sudo cp /etc/php/7.0/cgi/php.ini /home//etc/php7.0/ sudo chown -R . /home//etc
If you have many sites and sub-sites you might want a script that can loop through the home directory and update as required. I’ve provided a script, vmin-inis.sh, that can do that but as always check the scripts and verify the path for your system and update the script as needed.
If you want to use the script instead of typing the above commands make it executable and go ahead and run it.
chmod +x vmin-inis.sh sudo ./vmin-inis.sh
Detected php-cgi versions
For some reason the detected version of PHP are either not install correctly or Virtualmin detects them incorrectly. So in order for Virtualmin to work with PHP 5.6.x and PHP 7.0.x you will need to correct the location of where the php-cgi executables are. Instead of fixing them in the /home//fcgi-bin/*.fcgi files you will need to fix them on the file system. The reason for this is that Virtualmin will change the files back to what it detects and then you will start getting 500 errors. Type or paste the following to set the files correctly.
cd /usr/bin sudo mv -f php5.6 php5.6.old sudo mv -f php7.0 php7.0.old sudo ln -s php-cgi5.6 php5.6 sudo ln -s php-cgi7.0 php7.0
This may need to be done again after updating PHP 5.6.x and PHP 7.0.x unless the detection or installation is fixed in the future. Here’s a script, fcgi-fix.sh, that will move the phpX.X files Virtualmin points to to phpX.X.old and create symbolic links for phpX.X to php-cgiX.X instead which seems to work correctly script. You can download, extract, run this and that should be able to do the above and which you can run after updating PHP should the need arise.
chmod +x fcgi-fix.sh sudo ./fcgi-fix.sh
At this point almost everything is set but php-cgi will not quite work. What will happen at this point if you try to go to one of your site is that your web browser will try to download the php file. You’ll need to edit the apache configuration file to tell it to not handle php files.
nano /etc/apache2/mods-available/php7.0.conf
Configuring Apache for PHP
Comment out SetHandler application/x-httpd-php
and SetHandler application/x-httpd-php-source
. It should look like this when done.
<FilesMatch ".+\.ph(p[3457]?|t|tml)$"> # SetHandler application/x-httpd-php </FilesMatch> <FilesMatch ".+\.phps$"> # SetHandler application/x-httpd-php-source # Deny access to raw php sources by default # To re-enable it's recommended to enable access to the files # only in specific virtual host or directory Require all denied </FilesMatch> # Deny access to files without filename (e.g. '.php') <FilesMatch "^\.ph(p[3457]?|t|tml|ps)$"> Require all denied </FilesMatch> # Running PHP scripts in user directories is disabled by default # # To re-enable PHP in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. <IfModule mod_userdir.c> <Directory /home/*/public_html> php_admin_flag engine Off </Directory> </IfModule>
After updating the php7.0.conf you must restart apache.
service apache2 restart
If you see this message when restarting apache you will need to update your /etc/hosts file with the ip address and domain name. I don’t think this is an issue with the install as I think there was a problem with my /etc/hosts file not having the domain added when Virtualmin was installed but I am providing this information just in case.
AH00557: apache2: apr_sockaddr_info_get() failed for
Finalizing Virtualmin Configuration
Now, make the switch or fix existing PHP 5.5.9 sites to use one of the new versions of PHP you installed. Once PHP 5.6.x and PHP 7.0.x are installed we will need to ensure that Virtualmin can see them both. First go to our Virtualmin Server -> Virtualmin -> System Settings -> Re-Check Configuration. Once that completes we should be able to see PHP 5.6.x and PHP 7.0.x as options.
Then go to Virtualmin -> Server Configuration -> PHP Versions and select 5.6.x or 7.0.x as required for the Default HTML directory then click Save Versions. That virtual host should now be using the selected version PHP.
Check your sites to make sure they work as expected. I’ve had a few WordPress sites that have plugins that are not compatible with PHP 7.0.x and started throwing 500 errors but work fine when reverted to PHP 5.6.x.
s far as the creation of new sites goes you will need to edit you server templates to accomodate the new version of PHP. To do that go to Virtualmin –> System Setting Server Templates and edit each of the templates. When editing the templates at the Edit template section choose Apache website then scroll down until you see Template for PHP 5.6/7.0/7 configuration file and change from default to the following respectively:
Option | Setting |
---|---|
Template for PHP 5.6 configuration file | /etc/php/5.6/cgi/php.ini |
Template for PHP 7.0 configuration file | /etc/php/7.0/cgi/php.ini |
Template for PHP 7 configuration file | /etc/php/7.0/cgi/php.ini |
Sources
https://www.virtualmin.com/documentation
https://askubuntu.com/questions/705880/how-to-install-php-7
https://launchpad.net/~ondrej/+archive/ubuntu/php
doc_denis says
sorry for the disturbance, i’m launch new virtualmin installation in ubuntu 16.04 for use php7.
thank-you
doc_denis says
hello, after this command : “service apache2 reload” i have this message :
SuexecUserGroup configured, but suEXEC is disabled: Invalid owner or file mode for /usr/lib/apache2/suexec
Thank you for your help
Dan Zappone says
You should be able to run
a2enmod suexec
and thebservice apache2 reload
to enable it.doc_denis says
Hello, I tried without success. “a2enmod suexec”
I reinstall the server in Ubuntu 16.04 and installed virtualmin
thank you for your message
Michel Doens says
On the commandline php -v gives the cgi php verison ( not the cli version) which causes errors when using composer. Seems something with the symbolic link workarounds?
Dan Zappone says
Thanks Michel! I need to do a little testing but I think the latest version of the PPA no longer requires the FCGI fix to work correctly. I’ll update the tutorial accordingly.
J-P says
Hi Dan
Your tutorial helped me solve a PHP issue on my Centos7+php56+php7+Atomic Secured Linux+Virtualmin5+dedicated MySQL server with multiple MySQL instances setup which had me pulling my hair out for the last 8 hours or so.
Thank you!
J-P
Dan Zappone says
Glad it helped!
Dan Zappone says
Now with spell check! I had a few typos which I corrected along with wrong source link.