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