1

I've installed both version php in my server, 5.6 and 7.0. I can switch between versions, but I'd like to set php version by vhost. Default version is php 5.6. Below is example my vhost:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/var/www
    PHPINIDir /etc/php/7.0/cli/
    <Directory "var/www">
        AllowOverride all
        DirectoryIndex index.php
    </Directory>
</VirtualHost>.

phpinfo() is still displaying version 5.6, but loaded configuration file is from php 7.0. Im using Debian 8.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
M. Badovsky
  • 99
  • 3
  • 10
  • I doubt it's possible, you should use a solution like Docker, see this link for example: http://stackoverflow.com/questions/32195108/docker-run-apache-on-host-and-container-for-different-websites – AntoineB Aug 01 '16 at 13:12
  • Thanks, but i mean set this in vhost and simultaneusly working php5 & php7. Is it possible? – M. Badovsky Aug 01 '16 at 13:32
  • You want to switch php version in one virtual host or have several virtual hosts with different php versions? And what php engine you want to use for it - mod_php, php-fpm or php-cgi/fastcgi? – Michael Zarubin Aug 01 '16 at 13:49
  • Definitely severel vhosts in different version. I've installed mod_php and cgi. – M. Badovsky Aug 01 '16 at 14:04

1 Answers1

0

First, there are no way to enable two different mod_php in apache, so I describe the way to configure php-fastcgi via mod_fcgid and SUexec. Also I assume that you install php7.0 from dotteb.

First, you need to need to install required packages:

# apt-get install libapache2-mod-fcgid php5-cgi php7.0-cgi apache2-suexec

Then we need enable required apache modules and disable not required:

 # a2enmod suexec
 # a2dismod php5
 # a2dismod php7.0

After that create a users and virtual hosts directories:

# useradd site1 -s /sbin/nologin -d /var/www/html/site1 -m
# useradd site2 -s /sbin/nologin -d /var/www/html/site2 -m

PHP scripts will be executed from these users. So its recommended to own all site data with this user and group.

Then we create a php runners - we cannot just use /usr/bin/php5-cgi, SUExec will forbid it due to security reasons: script runners should be resides inside SUExec document root, which can be shown by running suexec -V in line AP_DOC_ROOT

# /usr/lib/apache2/suexec -V
 -D AP_DOC_ROOT="/var/www"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="www-data"
 -D AP_LOG_EXEC="/var/log/apache2/suexec.log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=100
 -D AP_USERDIR_SUFFIX="public_html"

Also SUexec will check permission on script runner, so we place this runner into subdirectory cgi-bin (you can change this name to any other, it's just convention) of site owner home directory:

# mkdir -p /var/www/html/site1/cgi-bin /var/www/html/site2/cgi-bin

And then create the runner for first site (with php5):

# cat <<-EOF >/var/www/html/site1/cgi-bin/php
#!/bin/sh
exec /usr/lib/cgi-bin/php5
EOF

You also can specify additional environment parameters ( by exporting environment variables such as PHP_FCGI_MAX_REQUESTS) for php or specific php.ini

And then for second site (with php7):

# cat <<-EOF >/var/www/html/site2/cgi-bin/php
#!/bin/sh
exec /usr/lib/cgi-bin/php7.0
EOF

Set executable flag on script runners:

# chmod 755 /var/www/html/site2/cgi-bin/php /var/www/html/site1/cgi-bin/php

document root and test scripts:

# mkdir -p /var/www/html/site1/data /var/www/html/site2/data
# echo -e "<?php\nphpinfo();" > /var/www/html/site1/data/test.php
# echo -e "<?php\nphpinfo();" > /var/www/html/site2/data/test.php

Set ownership for files in sites:

# chown -R site1:site1 /var/www/html/site1
# chown -R site2:site2 /var/www/html/site2

Finally we create virtual hosts for both sites, first for site1:

# cat <<EOF > /etc/apache2/sites-available/site1.conf
<VirtualHost *:80>
    ServerName site1.example.com
    DocumentRoot "/var/www/html/site1.data"
    <IfModule mod_fcgid.c>
         SuexecUserGroup site1 site1
         <Directory /var/www/html/site1/data>
              Options +ExecCGI
              AllowOverride All
              AddHandler fcgid-script .php
              FCGIWrapper /var/www/html/site1/cgi-bin/php .php
              Order allow,deny
              Allow from all
         </Directory>
   </IfModule>
</VirtualHost>
EOF

and then for site2

# cat <<EOF > /etc/apache2/sites-available/site2.conf
<VirtualHost *:80>
    ServerName site2.example.com
    DocumentRoot "/var/www/html/site2/data"
    <IfModule mod_fcgid.c>
         SuexecUserGroup site2 site2
         <Directory /var/www/html/site2/data>
              Options +ExecCGI
              AllowOverride All
              AddHandler fcgid-script .php
              FCGIWrapper /var/www/html/site2/cgi-bin/php .php
              Order allow,deny
              Allow from all
         </Directory>
   </IfModule>
</VirtualHost>
EOF

Then just enable sites by a2ensite site1 and a2ensite site2, and finally restart apache: systemctl restart apache2. If all is ok - apache should be restarted successfully and you can check that this hosts has different version of php via browser or just curl curl -s -H 'Host: site1.example.com' http://localhost/test.php

If something going wrong, you always can check the output of systemctl status apache (in case if apache doesn't start), or /var/log/apache2/suexec.log (in case if you see 500 Internal server error), or /var/log/apache2/error.log (in case generic error).

Hope this will help

Michael Zarubin
  • 366
  • 1
  • 4
  • Thank you for rich describe. I was configured all according up, but suexec.log say (8)Exec format error: exec failed (php). I exam files, but all seems to be correct – M. Badovsky Aug 03 '16 at 12:18