I'll share my thoughts on this matter, even though this subject is old.
Installing multiple PHP versions on a single machine IS possible, with acceptable level of dependency mess/conflicts, at least as long as PEAR is not involved, and standard PHP extensions are installed.
As mentioned on one of the previous messages, two compile-time defines are critical, PHPBASE and PREFIX. I'm using portupgrade to build the packages locally, therfore I use the following block on /usr/local/etc/pkgtools.conf, I'm sure similar options can be set for other package managers
INI:
MAKE_ARGS = {
'*/php70*' => 'PHPBASE=/opt/php70 PREFIX=/opt/php70',
'*/php71*' => 'PHPBASE=/opt/php71 PREFIX=/opt/php71',
'*/php72*' => 'PHPBASE=/opt/php72 PREFIX=/opt/php72',
'*/php73*' => 'PHPBASE=/opt/php73 PREFIX=/opt/php73',
'www/wordpress' => 'PHPBASE=/opt/php73',
'databases/phpmyadmin' => 'PHPBASE=/opt/php73 WITHOUT_PHP_DEPENDS=yes',
}
On exceptional circumstances, I had to override LOCALBASE as well during portupgrade, because the build system sometimes is trying to locate some dependencies under ${PHPBASE} instead of the default ${LOCALBASE}, /usr/local.
Allright, once all PHP versions required on the site are installed, then I start one PHP-FPM service for each PHP version. Each FPM instance I configure is using one pool per virtual host, for privilege separation and one dedicated TCP socket. There is a minor package conflict here, because the default service scripts use the same name, but I fix this by minimally changing the rc scripts placed under ${PHPBASE}/etc/rc.d/php-fpm, you can see the differences I applied for ver. 7.3:
Diff:
server:/usr/local/etc % diff -ruN /opt/php73/etc/rc.d/php-fpm /opt/php73/etc/rc.d/php73-fpm
--- /opt/php73/etc/rc.d/php-fpm 2019-11-25 22:34:18.984162000 +0200
+++ /opt/php73/etc/rc.d/php73-fpm 2018-12-19 22:14:52.193957000 +0200
@@ -3,41 +3,41 @@
# $FreeBSD: head/lang/php73/files/php-fpm.in 444558 2017-06-28 09:40:58Z tz $
#
-# PROVIDE: php-fpm
+# PROVIDE: php73-fpm
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable php-fpm:
-# php_fpm_enable="YES"
+# php73_fpm_enable="YES"
#
. /etc/rc.subr
-name="php_fpm"
-rcvar=php_fpm_enable
+name="php73_fpm"
+rcvar=php73_fpm_enable
-start_precmd="php_fpm_prestart"
-restart_precmd="php_fpm_checkconfig"
-reload_precmd="php_fpm_checkconfig"
-configtest_cmd="php_fpm_checkconfig"
+start_precmd="php73_fpm_prestart"
+restart_precmd="php73_fpm_checkconfig"
+reload_precmd="php73_fpm_checkconfig"
+configtest_cmd="php73_fpm_checkconfig"
load_rc_config "$name"
-: ${php_fpm_enable="NO"}
-: ${php_fpm_umask=""}
+: ${php73_fpm_enable="NO"}
+: ${php73_fpm_umask=""}
extra_commands="reload configtest logrotate"
command="/opt/php73/sbin/php-fpm"
-pidfile="/var/run/php-fpm.pid"
+pidfile="/var/run/php73-fpm.pid"
sig_stop="QUIT"
sig_reload="USR2"
-logrotate_cmd="php_fpm_logrotate"
+logrotate_cmd="php73_fpm_logrotate"
required_files="/opt/php73/etc/php-fpm.conf"
-php_fpm_logrotate() {
+php73_fpm_logrotate() {
if [ -z "$rc_pid" ]; then
_run_rc_notrunning
return 1
@@ -46,23 +46,23 @@
kill -USR1 $rc_pid
}
-php_fpm_checkconfig()
+php73_fpm_checkconfig()
{
echo "Performing sanity check on php-fpm configuration:"
eval ${command} -t
}
-php_fpm_prestart()
+php73_fpm_prestart()
{
- php_fpm_checkconfig
+ php73_fpm_checkconfig
checkconfig=$?
if [ $checkconfig -ne 0 ]; then
return $checkconfig
fi
- if [ ! -z "$php_fpm_umask" ]; then
- echo "Setting umask to: ${php_fpm_umask}"
- umask $php_fpm_umask
+ if [ ! -z "$php73_fpm_umask" ]; then
+ echo "Setting umask to: ${php73_fpm_umask}"
+ umask $php73_fpm_umask
fi
}
The next change is made in /etc/rc.conf, to include by default the new startup scripts directories and to preserve the standard service control logic:
Code:
local_startup="/usr/local/etc/rc.d /opt/php71/etc/rc.d /opt/php73/etc/rc.d"
From now on, switching PHP versions is a snap, directly from Apache 2.4 virtualhost config file (pay attention to proxy_fcgi module, make sure to enable it, otherwise this won't work). Read the general guide here
https://cwiki.apache.org/confluence/display/httpd/PHP-FPM, then tune your configuration:
From now on, the possibilities are endless, besides running a vost with a specific PHP version, you can even run various parts of the same vhost with different versions, based on Apache 2.4 rules match.
For example, the configuration snippet below will allow you to run: /app1 using php 7.2, /app2 using PHP 7.3, and ther remaining content using PHP 7.4, assuming there are at least 3 pools defined on ports 9720 (PHP 7.2) and 9730 (PHP 7.3) and 7.4 running on port 9740
Apache config:
ProxyPassMatch ^/app1/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9720/path/to/your/php72/app1/$1
ProxyPassMatch ^/app2/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9730/path/to/your/php73/app2/$1
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9740/path/to/your/other/apps/htdocs/$1