📜 ⬆️ ⬇️

PHP Version Switching Module for ISPmanager

Immediately reservation: you can use the CL switch, but only for CL.
This article will discuss how to make users able to choose the PHP version on servers with no CL.
It will be about switching versions of PHP with PHP as fastCGI.

In connection with the rapid release of new versions of PHP, the task has become to enable users to select the desired version of PHP for their account.
As usual, there is a set of operating systems: FreeBSD, Centos. But the module with minor modifications works on all operating systems and CL as well.

First, we will install the necessary PHP versions on the server and install all the necessary PHP extensions.

For FreeBSD
If we have a server with php5.2 installed long ago
We update ports
cd /usr/ports/lang/ php53 make PREFIX=/usr/local/php53 PHPBASE =/usr/local/php53 install 

')
PREFIX - we explicitly prescribe where to build PHP.
PHPBASE - specify the base directory of PHP.
After installing php, you need to install extensions (extensions):
 cd /usr/ports/lang/php53-extensions make PREFIX=/usr/local/php53 PHPBASE=/usr/local/php53 install 


During the assembly of extensions, if the system lacks any dependencies, it will install them, but not in standard directories, but everything will be there in / usr / local / php53, if you notice such applications, then they need to be demolished and installed in the usual way.
In fact, it is better to install each extension separately; this is of course long and unpleasant, but easier with updates.
After installing the ports, in principle, everything is ready! To check whether everything works normally we execute:
 /usr/local/php5.3/bin/php-cgi -v 

will show the php version

 /usr/local/php 5.3/bin/php-cgi -m 

show all installed extensions, if there are no errors, then everything has become normal.

After that we put all the necessary PHP versions in the folders we need.

Perl module
The module itself must be in
 cd /usr/local/ispmgr/addon 


Let's call it test.pl
 touch test.pl chmod +x test.pl chmod 700 test.pl 


As for the module, you can write a lot more correctly, but it was done in order to work and not for beauty.
To use the module, you will need to set your variables (what and where you are on the server):
  #   my $defhomedir = "/hosting"; #  php-cgi  5.2 my $v52 = "#!/usr/local/php/52/bin/php-cgi"; #  php-cgi  5.3 (      ) my $v53 = "#!/usr/local/bin/php-cgi"; #  php-cgi  5.4 my $v54 = "#!/usr/local/php/54/bin/php-cgi"; #  php-cgi  5.5 my $v55 = "#!/usr/local/php/55/bin/php-cgi"; #    my $wrapper = "".$defhomedir."/php-bin/"."$user"."/php"; #print "(".$wrapper.")"; open my $wrapperfile, '<', $wrapper; my $usercurrentphpwrapper = <$wrapperfile>; $usercurrentphpwrapper =~ s/\R//g; #print "(".$usercurrentphpwrapper.")"; close $wrapperfile; 


May add:
 #  php-cgi  5.6 my $v56 = "#!/usr/local/php/56/bin/php-cgi" 

And accordingly processing for the version:
 elsif ($usercurrentphpwrapper eq $v56) { $usercurrentphpver = "5.6"; #print 'OK 56'; } 

and
 elsif ($newphpver == 5.6) { system ("chflags noschg $defhomedir/php-bin/$user/php"); system ("echo '#!/usr/local/php/56/bin/php-cgi' > $defhomedir/php-bin/$user/php"); system ("chflags schg $defhomedir/php-bin/$user/php"); system ("pkill -9 -u $user"); $newphpver1 = 5600; #$usercurrentphpver = "5.6"; #print 'OK 56'; } 


Module code itself:
 #!/usr/bin/perl use CGI; use LWP::UserAgent; use CGI::Carp qw(fatalsToBrowser); use LWP::UserAgent; use Time::localtime; #take a time sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d_%02d-%02d-%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } #end take a time #isp get data my $Q = new CGI; open DATA, "<var/user.phpver"; my @rows = <DATA>; close DATA; my $user = $ENV{"REMOTE_USER"}; if ($Q->param("sok")) { open DATA, ">var/user.phpver"; foreach $row (@rows) { my @elems = split / /, $row, 2; printf DATA "%s", $row if ($elems[0] ne $user); } printf DATA "%s %s\n", $user, $Q->param("phpver"); close DATA; print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc/>\n"; } else { print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<doc>\n"; foreach $row (@rows) { chomp($row); my @elems = split / /, $row, 2; if ($elems[0] eq $user) { $elems[1] =~ s/\R//g; print "<phpver>$elems[1]</phpver>\n"; last; } } #     print "<slist name=\"phpver\">\n"; open WLIST, "<etc/phpver.list"; my @wlist = <WLIST>; close WLIST; foreach $phpver (@wlist) { my @elems = split / /, $phpver, 2; $elems[0] =~ s/\R//g; print "<msg>$elems[0]</msg>\n"; } print "</slist>\n"; print "</doc>\n"; } # END isp get data #   my $defhomedir = "/hosting"; #  php-cgi  5.2 my $v52 = "#!/usr/local/php/52/bin/php-cgi"; #  php-cgi  5.3 (      ) my $v53 = "#!/usr/local/bin/php-cgi"; #  php-cgi  5.4 my $v54 = "#!/usr/local/php/54/bin/php-cgi"; #  php-cgi  5.5 my $v55 = "#!/usr/local/php/55/bin/php-cgi"; #    my $wrapper = "".$defhomedir."/php-bin/"."$user"."/php"; #print "(".$wrapper.")"; open my $wrapperfile, '<', $wrapper; my $usercurrentphpwrapper = <$wrapperfile>; $usercurrentphpwrapper =~ s/\R//g; #print "(".$usercurrentphpwrapper.")"; close $wrapperfile; if ($usercurrentphpwrapper eq $v53) { $usercurrentphpver = "5.3"; #print 'OK 53'; } elsif ($usercurrentphpwrapper eq $v52) { #print 'OK 52'; $usercurrentphpver = "5.2"; } elsif ($usercurrentphpwrapper eq $v54) { $usercurrentphpver = "5.4"; #print 'OK 54'; } elsif ($usercurrentphpwrapper eq $v55) { $usercurrentphpver = "5.5"; #print 'OK 55'; } #   my $newphpver = $Q->param("phpver"); #$newphpver =~ s/\R//g; #      !!!!     pkill -9 -u $user         my $disuser1 = "USER"; my $disuser2 = "root"; if ( $user eq $disuser1 ) { $www111 = "off - USER"; system ("echo 'restricted user USER' >> /usr/local/ispmgr/var/phpver.change"); } elsif ( $user eq $disuser2 ) { $www111 = "off - root"; system ("echo 'restricted user root' >> /usr/local/ispmgr/var/phpver.change"); } #   elsif ($newphpver == 5.3) { $newphpver1 = 5300; #  system ("chflags noschg $defhomedir/php-bin/$user/php"); #         PHP system ("echo '#!/usr/local/bin/php-cgi' > $defhomedir/php-bin/$user/php"); #   system ("chflags schg $defhomedir/php-bin/$user/php"); #    system ("pkill -9 -u $user"); #print 'OK 53'; } elsif ($newphpver == 5.2) { system ("chflags noschg $defhomedir/php-bin/$user/php"); system ("echo '#!/usr/local/php/52/bin/php-cgi' > $defhomedir/php-bin/$user/php"); system ("chflags schg $defhomedir/php-bin/$user/php"); system ("pkill -9 -u $user"); #print 'OK 52'; $newphpver1 = 5200; #$usercurrentphpver = "5.2"; } elsif ($newphpver == 5.4) { system ("chflags noschg $defhomedir/php-bin/$user/php"); system ("echo '#!/usr/local/php/54/bin/php-cgi' > $defhomedir/php-bin/$user/php"); system ("chflags schg $defhomedir/php-bin/$user/php"); system ("pkill -9 -u $user"); $newphpver1 = 5400; #$usercurrentphpver = "5.4"; #print 'OK 54'; } elsif ($newphpver == 5.5) { system ("chflags noschg $defhomedir/php-bin/$user/php"); system ("echo '#!/usr/local/php/55/bin/php-cgi' > $defhomedir/php-bin/$user/php"); system ("chflags schg $defhomedir/php-bin/$user/php"); system ("pkill -9 -u $user"); $newphpver1 = 5500; #$usercurrentphpver = "5.5"; #print 'OK 55'; } #    open (MyFile, ">>" , "var/phpver.change") ; print MyFile '[' . timestamp() . '] '.$user.' php was '.$usercurrentphpver.' become '.$newphpver."\n"; close MyFile; 


Next, you need to add the ability for users to switch versions (add an item in the account settings).
To folder
 cd /usr/local/ispmgr/etc 


Create an ispmgr_mod_phpver.xml file
 touch ispmgr_mod_phpver.xml chmod 700 ispmgr_mod_phpver.xml 


In it, in ISPmanager's “Settings” menu, a drop-down version switching menu appears:

 <?xml version="1.0" encoding="UTF-8"?> <mgrdata> <handler name="test.pl" type="cgi"> <event after="yes">usrparam</event> </handler> <metadata name="usrparam"> <form> <field name="phpver"> <select name="phpver"/> </field> </form> </metadata> <lang name="en"> <messages name="usrparam"> <msg name="5.3">5.3</msg> <msg name="5.2">5.2</msg> <msg name="5.1">5.1</msg> <msg name="4.4">4.4</msg> </messages> </lang> <lang name="ru"> <messages name="usrparam"> <msg name="phpver"> PHP </msg> <msg name="hint_phpver">  PHP.</msg> </messages> </lang> </mgrdata> 


Also in / usr / local / ispmgr / etc create a file
 touch phpver.list 

It lists the versions that we will use and installed on the server
 5.2 5.3 5.4 5.5 

Do not forget to set the carriage transfer (note the empty line under 5.5).
Create a file in:
 cd /usr/local/ispmgr/var/ 

Phpver.change file:
 touch phpver.change chmod 700 phpver.change 


This will drop the log changes users version.
We clean xml cache ISPmanager
 rm -rf /usr/local/ispmgr/var/.xmlcache 

Restart the ISPmanager panel:
 killall ispmgr 


Check on test user
 <? phpinfo(); ?> 


We go to ISPmanager and use. Also, do not forget to send users a newsletter about the availability of the new “buns”.
image
If you forgot to describe something - add.

About FreeBSD Flags in a Script
system ("chflags noschg $ defhomedir / php-bin / $ user / php");
system ("echo '#! / usr / local / php / 52 / bin / php-cgi'> $ defhomedir / php-bin / $ user / php");
system ("chflags schg $ defhomedir / php-bin / $ user / php");
ISPmanager in old versions of the wrapper wrote to the user in the home directory. As a result, if the user deleted everything, then apache when restarted went down, since the wrapper no longer exists, therefore we set the immutability flag.
For Centos chflags noschg, replace with chattr -i and, accordingly, chflags schg with chattr + i .

For CL if using alt-php instead of:
 system ("echo '#!/usr/local/php/52/bin/php-cgi' > $defhomedir/php-bin/$user/php"); 

You must specify:
 system ("cl-selector -s php -v $newphpver -u $user"); 

Source: https://habr.com/ru/post/235455/


All Articles