# Multiple version of php.



## bryn1u (Feb 6, 2017)

Hello,

Im looking for same tutorials about installing multiple diffrent versions of php. For example 5.6, 7.0 and 7.1. Every tutorial found is very old and the process installation based on php from source. Can't i install from ports ? Someone has experience with it and could share a knowledge ? I would be gratefull.

Regards,


----------



## SirDice (Feb 6, 2017)

You can't install them next to each other, they conflict.


----------



## bryn1u (Feb 6, 2017)

hmm, nginx+fastcgi it will not resolve the problem ?


----------



## ShelLuser (Feb 6, 2017)

Only using a Jail (virtual system) will resolve this. As SirDice said: the packages themselves conflict, they get installed to the same location so they cannot co-exist on one system.


----------



## bryn1u (Feb 6, 2017)

ShelLuser 
What do u mean virtualsystem ? 1 jail 1 php version ? 
Could you check this solutions http://www.ondrejsimek.com/blog/running-multiple-php-versions-is-so-easy-with-fastcgi/
Installed into diffrent locations.


----------



## ShelLuser (Feb 6, 2017)

bryn1u said:


> ShelLuser
> What do u mean virtualsystem ? 1 jail 1 php version ?


Yeah, that's what I would do. Especially because using older PHP versions can always become a nasty security risk (depending on the version being used of course).

The example you're showing is of course also possible, however you'd be working outside the package system (so not using Ports or packages but manual installations). Although you could utilize parts of the Ports system. For example using `# make extract` is enough to extract the vanilla source tree, which you could then use to manually install it to a different specific location.

But it is bad practice. Using older (obsolete) PHP versions can come with serious security risks.


----------



## bryn1u (Feb 6, 2017)

I will not install old version php, it was only example, couldn't find newer tutorial.


----------



## Remington (Feb 7, 2017)

You can manually compile and install php directly from php.net without using ports.  I have 3 different php versions in the same environment with no problems.


----------



## swix (Jun 15, 2017)

Old thread, but just in case, here is something I used to do a few years ago, using ports:

cd /usr/ports/lang/php55
make config
make PREFIX=/usr/local/php55 PHPBASE=/usr/local/php55 install clean DISABLE_CONFLICTS=1
make PREFIX=/usr/local/php55 PHPBASE=/usr/local/php55 install clean DISABLE_CONFLICTS=1
cd /usr/ports/lang/php55-extensions/
make config

And then Apache+FCGIWrapper.

I was looking for a cleaner way to do it (to have php 5.6 + 7.1 on the same system), but it seems the situation is the same here ? It's a bit sad when you compare to some linux distributions where you can have different concurrent versions really easily.   In the mean time, if you (bryn1u) have found a better solution, please let us know 

Regards.


----------



## wattie (Jun 20, 2017)

The DirectAdmin control panel comes preconfigured to allow two versions of PHP; however it does not use the ports.


----------



## wattie (Jul 21, 2017)

Quote from another forum:

"BTW, alternative PHP versions can be easily build from ports if you add something like this into /etc/make.conf:
Code:

PHP_ALT=php52 php53 php55 php70 php71
.for port in ${PHP_ALT}
.if ${.CURDIR:M*/ports*/*/${port}*}
   PREFIX=/usr/local/${port}
    PHPBASE=/usr/local/${port}
    DISABLE_VULNERABILITIES=yes
.endif
.endfor"


----------



## rainer_d (Oct 9, 2017)

wattie said:


> Quote from another forum:
> 
> "BTW, alternative PHP versions can be easily build from ports if you add something like this into /etc/make.conf:
> Code:
> ...




Don't they still conflict?
I can build them all via poudriere (though not in the same repo).


----------



## SirDice (Oct 9, 2017)

rainer_d said:


> Don't they still conflict?


Yes. 

Besides that, the 'solution' offered is pretty bad. This may have worked in the past but nowadays the PHP version is set using DEFAULT_VERSIONS, see /usr/ports/Mk/bsd.default-versions.mk.


----------



## rainer_d (Oct 9, 2017)

I would love to be able to install different versions of PHP in parallel (and via pkg).

Of course, that would mean being able to compile and install different versions of pecl and pear modules in parallel, too.

But in the end, I only access them via FastCGI - I haven't used mod_php since php-fpm became an option of the port in PHP 5.3.

Sigh. How far away are flavors?
;-)


----------



## SirDice (Oct 9, 2017)

rainer_d said:


> I would love to be able to install different versions of PHP in parallel (and via pkg).


This is a good example for using a jail(8). Each jail can run its own PHP version.



rainer_d said:


> How far away are flavors?


I wouldn't hold my breath  waiting for them. Besides that, flavors still won't let you install 2 conflicting packages. It will only make it easier to change dependencies for packages.


----------



## rainer_d (Oct 9, 2017)

SirDice said:


> This is a good example for using a jail(8). Each jail can run its own PHP version.




Yes, but can I run PHP chrooted inside a jail? Can I nullfs-mount stuff inside a chroot inside a jail?

I run several hundred PHP-FPM pools on one server, each chrooted and each having various parts of the system nullfs-mounted into the chroot.
Each pool is also a distinct ZFS filesystem.

I don't want to run a jail for each pool - that would be insane and a nightmare to manage (IMO).


----------



## SirDice (Oct 9, 2017)

rainer_d said:


> Yes, but can I run PHP chrooted inside a jail?


It's a bit overkill because you're already running it inside a jail(8) but it should certainly be possible.



> Can I nullfs-mount stuff inside a chroot inside a jail?


Not from inside the jail but you can from the host.


----------



## bryn1u (Oct 9, 2017)

You can do it manually. For example mydevil.net has a 3 diffrent version of php. Server based on FreeBSD-11

```
PHP: [5.6: 0/3] [7.0: 0/3] [7.1: 0/3]
```
You can choose version by .htaccess for example. Each site based on diffrent version.
Look for centos tutorial. I think you can do it compiled from ports or from php site manually.  Please let me know if you can do something. I would be grateful. And yes, php version per jail is a nightmare for admin when so many users wants to use all version of php.


----------



## Nasrudin (Sep 12, 2019)

SirDice said:


> Yes.
> 
> Besides that, the 'solution' offered is pretty bad. This may have worked in the past but nowadays the PHP version is set using DEFAULT_VERSIONS, see /usr/ports/Mk/bsd.default-versions.mk.



I realize this is pretty old but can you expand on this, as I find myself wanting to do the exact same thing. Why is it bad?


----------



## SirDice (Sep 12, 2019)

Like I said, things have changed. Back in the day those if..then..else make.conf constructs were needed to make it work at all. Since then /usr/ports/Mk/bsd.default-versions.mk was created and the ports system itself got convenient "default" selectors. Those "old" make.conf constructs either don't work or set the wrong variables.


----------



## Nasrudin (Sep 12, 2019)

So I shouldn't try to kludge a prefix to each php version (e.g. php71 => /usr/local/php71) in make.conf at all? How else would I go about doing this?


----------



## SirDice (Sep 13, 2019)

Go back to the beginning, why do you need to run two different versions in the first place? What problem are you trying to solve?


----------



## rainer_d (Sep 13, 2019)

If you run multiple pools at the same time and want a different PHP version for some of them.
The problem with jails is that it becomes way too complicated easily, due to the need to mount and unmount homedirectories.
And you need the users in all jails, which leads to having to use LDAP, which is another thing that can and does go wrong.

If I could run PHP 7.2 and 7.3 at the same time, I'd just point the apache or nginx config at the socket and that's it.
I run all my PHPs chrooted and have various of the hosts' directories nullfs mounted to each homedirectory.


----------



## SirDice (Sep 13, 2019)

In my opinion you're trying to cram too much onto one big system. I'm more in favor of splitting things up over multiple, smaller, servers. Yes, it increases complexity slightly but you gain a lot of resilience (load-balancing, fault-tolerance). It also makes it easier to take one server out, update it and put it back, without having to take down everything. If you put all your eggs into one basket, as soon as the bottom falls out, you lose all your eggs.


----------



## rainer_d (Sep 13, 2019)

Well, we found that there is enough resiliency in a single server for our purposes. If you had multiple servers, you'd need a shared filesystem, which is NFS in the case of FreeBSD, which is also orders of magnitudes slower than a local filesystem, especially for serving PHP. Latency is very noticeable when serving files via http.
And when you have NFS, that NFS server is again a single point of failure.

Ravenports can apparently do it, but I could never get myself committing to that....


----------



## Remington (Sep 13, 2019)

Another option is to compile PHP sources from php.net and put them in basejail created by ezjail.  All jails will have access to different php versions without having to install in each jail.


----------



## wattie (Sep 13, 2019)

I am interested if the following will work:

FreeBSD 12 + OpenSSL 1.0.2 installed through ports
PHP 7.3 installed using the base OpenSSL (so it can be configured to use TLS 1.3)
PHP 5.6 installed using the OpenSSL from ports (so it can be... compiled) 

Anybody tried this?


----------



## CyberCr33p (Sep 13, 2019)

You can create a shell script to add the users accounts in the jail ( copy /etc/master.passwd and /etc/group to /home/jail/php56/etc directory ).

Then you can have another script inside jail and execute it using:

`jexec php56 /root/scripts/syncJail.sh`

Here is the script:


```
#!/usr/local/bin/bash

pwd_mkdb -p /etc/master.passwd

rm -fr /var/run/fastcgi/*

for username in `( grep "/home/www" /etc/passwd | awk '{split($0,a,":"); print a[1]}' )`
do
        mkdir /var/run/fastcgi/${username}
        chown ${username}:www /var/run/fastcgi/${username}
done

/usr/local/etc/rc.d/php-fpm restart >/dev/null 2>&1
```

And here is the /etc/fstab.php56 :


```
/home/www /home/jail/php56/home/www nullfs rw,noatime,nosuid 0 0
/tmp /home/jail/php56/tmp nullfs rw,noatime,nosuid 0 0
/tmpfs /home/jail/php56/tmpfs nullfs rw,noatime,nosuid 0 0
```

And the /etc/jail.conf :


```
mount.fstab = "/etc/fstab.${name}";
exec.start = "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown";
exec.clean;
mount.devfs;
path = "/home/jail/${name}";
allow.raw_sockets=1;

php56 {
        host.hostname = "php56";
        ip4.addr = xxx.xxx.xxx.xxx;
}
```

And in your web-server in vhost section you use the php socket from jail:


```
/home/jail/php56/var/run/fastcgi/username/username.socket
```


----------



## Nasrudin (Sep 16, 2019)

SirDice said:


> Go back to the beginning, why do you need to run two different versions in the first place? What problem are you trying to solve?





rainer_d said:


> If you run multiple pools at the same time and want a different PHP version for some of them.
> ...
> If I could run PHP 7.2 and 7.3 at the same time, I'd just point the apache or nginx config at the socket and that's it.



Almost exactly this idea. 
It might help the case for jails if I knew exactly the difference in resource usage between a jailed daemon (to me this is the duplicate OS on the disk and perhaps a process or two) and a regular PHP-FPM daemon. Right now, it seems to be that multiple PHP versions will ultimately be lighterweight than jails (since you need a duplicate OS per jail and hence per php version). I do realize that there is a potentially awesome (but slight) security benefit to using jails in production, but I'd really like the option to implement multiple versions of PHP in different ways. 

Perhaps, SirDice, this the implied search for rationale that you sought with your questions? 

I do recognize that altering the ports/poudriere systems to support multiple versions of PHP on the same box might be too hard to accomplish; sometimes implementation answers in the FreeBSD space are really based on this kind of idea. So, it would be nice if this were asserted when it is the underlying case ... as this is a rationale I can also accept. 

For my issues, there are two key use cases here:

- A development box which just needs to switch PHP versions on the fly for testing.
- A production box serving multiple sites which has legacy sites that cannot switch PHP versions "now".

So I'm still curious (whether it is perceived as a good idea by some or not) as to what is expected to happen if I do something like:


```
PHP_BUILDS=php70 php71 php72
.for port in ${PHP_ALT}
.if ${.CURDIR:M*/ports*/*/${port}*}
PREFIX=/usr/local/${port}
PHPBASE=/usr/local/${port}
DISABLE_VULNERABILITIES=yes
.endif
.endfor
```

Thanks in advance for any insight, including any "that's an absolutely horrible idea" comments.


----------



## Lamia (Sep 18, 2019)

Have you taken a look at the php-virtmanager (something like that) in ports?

How about running each in separate chroot/jail?


----------



## Nasrudin (Sep 18, 2019)

Lamia said:


> Have you taken a look at the php-virtmanager (something like that) in ports?



I have not seen anything like that in my ports tree. Can you give me an exact name?


----------



## Nasrudin (Sep 23, 2019)

Ok following up on this. I've been able to get this to work mostly with this code:


```
# PHP Makefile arm twisting follows.
p_PHP_FLAVORS=php71 php72 php73
p_GETFLAVOR=echo $$FLAVOR
p_FLAVOR=${p_GETFLAVOR:sh}

.for port in ${p_PHP_FLAVORS}

.if ${.CURDIR:M*/ports/*/${port}*}
PREFIX=/usr/local/${port}
PHPBASE=/usr/local/${port}
DISABLE_VULNERABILITIES=yes
.info set by CURDIR: ${.CURDIR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
.endif

.if ${p_FLAVOR} == ${port}
PREFIX=/usr/local/${port}
PHPBASE=/usr/local/${port}
DISABLE_VULNERABILITIES=yes
.info set by FLAVOR ${p_FLAVOR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
.endif

.endfor
```

I have one issue with devel/pear :


```
=======================<phase: stage          >============================
make: "/etc/make.conf" line 30: set by FLAVOR php71 -- sets PREFIX: /usr/local/php71 and PHPBASE: /usr/local/php71
===>  Staging for php71-pear-1.10.6
===>   Generating temporary packing list
env: /usr/local/bin/php: No such file or directory
*** Error code 127

Stop.
make: stopped in /usr/ports/devel/pear
```

I've no idea where to begin with this one, so it may be a while until I get this ironed out. Stay tuned.


----------



## Alexander Huemeyer (Sep 24, 2019)

I would just downloaded the sources form php.net and install to different locations accourding the php.net install advisories. Just make sure to track the php.net releasenotes for important updates.


----------



## Remington (Sep 24, 2019)

Alexander Huemeyer said:


> I would just downloaded the sources form php.net and install to different locations accourding the php.net install advisories. Just make sure to track the php.net releasenotes for important updates.



That's exactly what I did and I have 3 different versions since some php apps will not work with newer php version.  My php folders looks like this:

/usr/local/php/5.6
/usr/local/php/7.1
/usr/local/php/7.2

But I put them in /jail/basejail/usr/local/php/... so all of my jails will have access to it without having to compile or copy php files into each jail.  Of course I have to mount them to the jails in /etc/jail.conf


----------



## Nasrudin (Oct 10, 2019)

Yeah the problem with downloading sources is that it's outside the FreeBSD ports system. People have spent time patching PHP for FreeBSD and I'd like to not only take advantage of that but to keep taking advantage of that. 

Stay tuned...I think I have it working. I will post results here so someone can duplicate my work and see if it works for them.


----------



## Remington (Oct 11, 2019)

Nasrudin said:


> Yeah the problem with downloading sources is that it's outside the FreeBSD ports system. People have spent time patching PHP for FreeBSD and I'd like to not only take advantage of that but to keep taking advantage of that.
> 
> Stay tuned...I think I have it working. I will post results here so someone can duplicate my work and see if it works for them.



I didn't have to patch anything.  Once I had the setup done then its fairly simple to update.


----------



## An-tonio (Dec 17, 2019)

I already submitted a "bug" about this subject: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=224409

But maybe it's difficult actually or port maintainers don't have interest on it. This issue have blocked me to migrate some projects from debian to freebsd. For some clients I have to use controls panels like froxlor and they need to manage multiple php versions for different vhost.


----------



## rainer_d (Dec 18, 2019)

The problem with compiling your own PHP in a special location is that none of the ports that rely on PHP can use it.
So, in addition to PHP outside of pkg(8)-management, you will need to maintain the packages that depend on PHP also outside of pkg(8).
I could probably do that, but it would be a veritable nightmare to maintain IMO, especially in the long run and when you have this construct running on multiple servers. 
Maybe you can just package-up all of /usr/local/php* and replace it - but it's still not "clean" enough for my taste.
And usually, if something does not look "clean", it's usually a bad solution to begin with.

Also, each PHP branch has now a sufficiently low lifetime that it's actually a PITA to migrate users to new physical servers each time the then-current version drops out of the ports-tree...


----------



## Remington (Dec 18, 2019)

rainer_d said:


> The problem with compiling your own PHP in a special location is that none of the ports that rely on PHP can use it.
> So, in addition to PHP outside of pkg(8)-management, you will need to maintain the packages that depend on PHP also outside of pkg(8).
> I could probably do that, but it would be a veritable nightmare to maintain IMO, especially in the long run and when you have this construct running on multiple servers.
> Maybe you can just package-up all of /usr/local/php* and replace it - but it's still not "clean" enough for my taste.
> ...



True.  I have to select one PHP version to be the default version on the server BUT Nginx's website config can be configured to use specific PHP version.  I have couple of websites running PHP 5.6, 7.3, 7.4 in the same jail.


----------



## Nasrudin (Dec 27, 2019)

rainer_d said:


> The problem with compiling your own PHP in a special location is that none of the ports that rely on PHP can use it.
> So, in addition to PHP outside of pkg(8)-management, you will need to maintain the packages that depend on PHP also outside of pkg(8).
> ...



I believe I've addressed this by trying to detect ports that rely on PHP. There is only one misbehaving port, devel/pear, and that was addressed by this patch:


```
40c40
<     @cd ${WRKSRC} && ${SETENV} DESTDIR=${STAGEDIR} ${PREFIX}/bin/php -q ./go-pear
---
>     @cd ${WRKSRC} && ${SETENV} DESTDIR=${STAGEDIR} ${LOCALBASE}/bin/php -q ./go-pear
```

So far (we are still testing) this all works.  I build from poudriere. Pkg still knows what to do and how to install all of this. You can leave out the .info lines if you wish. 


```
# PHP Makefile arm twisting follows.
p_PHP_FLAVORS=php71 php72 php73 php74
p_GETFLAVOR=echo $$FLAVOR
p_FLAVOR=${p_GETFLAVOR:sh}

.for port in ${p_PHP_FLAVORS}

# Easy part. If your current directory begins with what's in PHP_ALT, you need these variables
.if ${.CURDIR:M*/ports/*/${port}*}
PREFIX=/usr/local/${port}
PHPBASE=/usr/local/${port}
DISABLE_VULNERABILITIES=yes
.info set by CURDIR: ${.CURDIR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
.endif

# Harder part, if we detect the flavor is set up via shell environment, you need these variables
.if ${p_FLAVOR} == ${port}
PREFIX=/usr/local/${port}
PHPBASE=/usr/local/${port}
DISABLE_VULNERABILITIES=yes
.info set by FLAVOR ${p_FLAVOR} -- sets PREFIX: ${PREFIX} and PHPBASE: ${PHPBASE}
.endif

.endfor
```

Any feedback or nits to pick are warmly appreciated. If someone who knows more of what they are doing can somehow get this behavior into the ports system under a knob (no I am not holding my breath lol) that would be awesome.


----------



## rainer_d (Dec 27, 2019)

Hi, 

where do you place that code?

Maybe you an submit a PR, so ports-maintainers can comment?


Thanks for you efforts!


----------



## ecazamir (Dec 30, 2019)

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


```
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:

```
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:

```
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


```
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
```


----------



## Nasrudin (Jan 20, 2020)

rainer_d said:


> where do you place that code?
> 
> Maybe you an submit a PR, so ports-maintainers can comment?
> 
> ...



I place said code in the equivalent of make.conf. (For people who use ports builder tools, that can be a number of places, for people who build ports manually that's in /etc/make.conf) I hesitate to submit a PR because a) I don't know exactly where to do that and b) I'm not convinced that I can convince anyone to take this as far as it may need to go to get it to work. ( I have been trying to convince people that convincing people of things is futile, but for some reason this isn't working so well...  )

BTW it seems pear works. This build has made it to open testing at my sites, which is good news.


----------



## rainer_d (Jan 20, 2020)

There is a PR on bugs.freebsd.org that links back to this thread, so it's kind of circular...


The current PHP maintainer closed that PR a while ago.

I will try to build it in my poudriere instance. Please update this thread with your efforts.


----------



## Nasrudin (Jan 20, 2020)

Completing the circle, did you mean PR 224409?


----------



## rainer_d (Jan 20, 2020)

Exactly.


----------



## Nasrudin (Jan 20, 2020)

Ok so I'll post what I have in that PR, since it's a common reference. I think that's better than opening a new PR as my solution is not strictly a 'problem'.


----------



## rainer_d (Feb 7, 2020)

OK, so this actually builds, except for ports that rely on PHP and expect its headers in /usr/local/include (like wordpress or prestashop) - and to build PEAR-ports, one would probably have to change every single one. I'll ignore that for the moment because I don't really need them (horde seems to be one of the main consumers of pear-ports).
Now, I've got to test this and see if I can get this to work in my setup.


----------



## rainer_d (Aug 3, 2020)

Nasrudin said:


> Ok so I'll post what I have in that PR, since it's a common reference. I think that's better than opening a new PR as my solution is not strictly a 'problem'.





Hi Nasrudin,

it seems that pear is now using pear.mk (from Uses) and your diff does not work anymore.

Do you have suggestions how to fix this (and all other pear ports)?


----------



## alfikmik (Apr 6, 2021)

what about jails with only `php` + `php-fpm`  on different ports + `mount nullfs` with php sourcefiles and from master setting php-fpm to according port, so You can choose php version for WWW choosing right php-fpm instance in web server settings for virtualhost


----------



## rainer_d (Apr 7, 2021)

I have hundreds of vhosts. 

As a matter of fact, I have it working with Nasrudin's patch and php73, php74 and php80.

I am nullfs-mounting all php-versions into the home-directories of all users at the same time. I only change the path to the php-fpm socket in the apache (+mod_fastcgi) config to the one I want for the relevant vhost.

This seems to work surprisingly well for now.


----------



## Nasrudin (Apr 13, 2021)

rainer_d said:


> Hi Nasrudin,
> 
> it seems that pear is now using pear.mk (from Uses) and your diff does not work anymore.
> 
> Do you have suggestions how to fix this (and all other pear ports)?


I fixed pear via this change to the port Makefile (apologies for the lack of diff here):


```
do-install:
        @cd ${WRKSRC} && ${SETENV} DESTDIR=${STAGEDIR} ${PHPBASE}/bin/php -q ./go-pear
```

Note the PHPBASE there is a replacement for what was there before.


----------



## rainer_d (Dec 19, 2022)

Hi Nasrudin - it seems with 2022Q4, your patch has stopped working.
It's not really respecting PHPBASE anymore.


----------



## alfikmik (Dec 20, 2022)

I think that is simplier to use jails with only php-fpm instance for different versions of PHP and redirecto to one of them by configuration of vhost in apache/nginx


----------



## rainer_d (Dec 20, 2022)

Unfortunately, it isn't.


----------



## alfikmik (Dec 20, 2022)

hyymmm

for example vhosts in apache...

I have 4 separate JAILS (on aliased loopback) with selected PHP version and php-fpm on board

second thing i have defined PHP backends (in apache conf) like:


```
Define PHP74 fcgi://10.20.30.74:9000
Define PHP80 fcgi://10.20.30.80:9000
Define PHP81 fcgi://10.20.30.81:9000
Define PHP82 fcgi://10.20.30.82:9000
```

and than i use it like this:


```
<Macro vHost $domain $dir $openbase $php>

<VirtualHost *:80>
    ServerAdmin webmaster@domain.tld
    ServerName $domain
    DirectoryIndex index.php
    ProxyFCGISetEnvIf "true" PHP_ADMIN_VALUE "open_basedir=$openbase:/tmp"
    ProxyPassMatch ^/(.*\.php)$ $php$dir/$1
    <Proxy "fcgi://localhost">
        ProxySet timeout=600
    </Proxy>
    DocumentRoot $dir
    <Directory $dir>
        Options All
        AllowOverride All
    </Directory>
    LogLevel notice
    LogLevel alert rewrite:trace1
    ErrorLog "/var/log/http.$domain.log"
    SetEnv LNG none
</VirtualHost>
</Macro>

Use vHost www1.domain.tld /vol/www1.domain.tld/public /vol/www1.domain.tld ${PHP81}
Use vHost www2.domain.tld /vol/www2.domain.tld/public /vol/www1.domain.tld ${PHP82}
```


----------



## rainer_d (Dec 20, 2022)

Yes. But if you had fifty or more pools in different PHP versions and needed to be able to switch between them, it would be a nightmare.
Plus, you would need to track the port-numbers somewhere, which I don't, as I use the socket.

Also, these php-fpm instances are all chrooted (which is much more secure than open_basedir)., which requires a sort-of special directory-layout.

Now, granted, these chroots are more or less setup like jails already - but they aren't running jails.

I haven't looked into jails for a very long time - maybe it could work. But it looks like it would require an even more complicated setup than it currently already is.


----------



## cracauer@ (Dec 22, 2022)

Have you considered installing the php versions into their own $PREFIX? Without the use of ports except for picking up patches?
- /opt/local/bin/php8
- /opt/local/bin/php7.2
- /opt/local/bin/php7,4

That way has the additional advantage that pkg or port upgrades would never touch you old versions that you want to stay old.


----------



## Nasrudin (Dec 23, 2022)

rainer_d said:


> Hi Nasrudin - it seems with 2022Q4, your patch has stopped working.
> It's not really respecting PHPBASE anymore.


Rest assured that I will take a look. However, I just spent a lot of time getting Q4 to work with LibreSSL, so getting to PHP may take a while.



alfikmik said:


> I think that is simplier to use jails with only php-fpm instance for different versions of PHP and redirecto to one of them by configuration of vhost in apache/nginx



For our use case, we do not need different versions running at the same time. We need to be able to throw a switch to look at a different version. Your method, while allowing multiple different versions to be running at the same time, has the potential to confuse.


----------



## rainer_d (Dec 23, 2022)

Thanks, Nasrudin - I do run various pools with different versions at the same time. I modified the startup-scripts so that they write their PIDs and logs in different places. I also change the names of the sockets. So it does work quite well, actually.

Compiling yourself with different PREFIX has been brought up in this thread (or elsewhere), but I really like the building and packaging of poudriere.
I will also take another look at the build-log. It's really a shame that there is no way to "officially" do something like this - but I guess the port-maintainers have other things to worry about.


----------

