Skip to content

Commit

Permalink
Allow PHP version to be selected from many when in FPM mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jcameron committed Aug 24, 2020
1 parent 8ab4b97 commit 8d30bc9
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions virtual_feature.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ sub feature_get_web_php_mode
sub feature_save_web_php_mode
{
my ($d, $mode) = @_;
my $tmpl = &virtual_server::get_template($d->{'template'});
my $server = &find_domain_server($d);
my $oldmode = &feature_get_web_php_mode($d) || "";
if ($oldmode eq "fpm" && $mode ne "fpm") {
Expand All @@ -1151,6 +1152,19 @@ sub feature_save_web_php_mode
}
elsif ($mode eq "fpm" && $oldmode ne "fpm") {
# Setup FPM pool
if (!$d->{'php_fpm_version'}) {
# Work out the default FPM version from the template
my @avail = &virtual_server::list_available_php_versions(
$d, "fpm");
@avail || &error("No FPM versions found!");
my $fpm;
if ($tmpl->{'web_phpver'}) {
($fpm) = grep { $_->[0] eq $tmpl->{'web_phpver'} }
@avail;
}
$fpm ||= $avail[0];
$d->{'php_fpm_version'} = $fpm->[0];
}
&virtual_server::create_php_fpm_pool($d);
$port = $d->{'php_fpm_port'} ||
&virtual_server::get_php_fpm_socket_file($d);
Expand Down Expand Up @@ -1181,7 +1195,7 @@ sub feature_list_web_php_directories
my $mode = &feature_get_web_php_mode($d);
my @avail = &virtual_server::list_available_php_versions($d, $mode);
if ($mode eq 'fcgid') {
# Can only run the PHP verson of the php-cgi command
# Map from the PHP FPM binary to the version number
my ($defver) = &get_domain_php_version();
my $phpcmd = &find_php_fcgi_server($d);
if ($phpcmd) {
Expand All @@ -1196,10 +1210,16 @@ sub feature_list_web_php_directories
'version' => $defver } );
}
elsif ($mode eq 'fpm') {
# Can only run the PHP version for the FPM server
# Find the FPM version installed that matches the version in use
my $ver = $d->{'php_fpm_version'} || $avail[0]->[0];
my ($a) = grep { $_->[0] eq $ver } @avail;
if (!$a) {
# Selected version doesn't exist .. assume first one
$a = $avail[0];
}
if (@avail) {
return ( { 'dir' => &virtual_server::public_html_dir($d),
'version' => $avail[0]->[0],
'version' => $a->[0],
'mode' => $mode } );
}
else {
Expand All @@ -1219,31 +1239,38 @@ sub feature_save_web_php_directory
my $mode = &feature_get_web_php_mode($d);
my @avail = &virtual_server::list_available_php_versions($d, $mode);
if ($mode eq "fpm") {
# Multiple FPM versions aren't supported yet, but we can at least
# use the actual version of FPM running
if ($avail[0]->[0] eq $ver) {
$d->{'nginx_php_version'} = $ver;
return undef;
# If the FPM version changed, just reset up
if (!$d->{'php_fpm_version'}) {
# Assume currently on first version
$d->{'php_fpm_version'} = $avail[0]->[0];
}
if ($ver ne $d->{'php_fpm_version'}) {
&virtual_server::delete_php_fpm_pool($d);
$d->{'php_fpm_version'} = $ver;
&virtual_server::save_domain($d);
&virtual_server::create_php_fpm_pool($d);
}
return $text{'feat_ephpmode'};
}
else {
# Assume this is FCGId mode

# Get the current version
my $phpcmd = &find_php_fcgi_server($d);
my $defver;
if ($phpcmd) {
foreach my $vers (@avail) {
if ($vers->[1] && $vers->[1] eq $phpcmd) {
$defver = $vers->[0];
# Get the current version
my $phpcmd = &find_php_fcgi_server($d);
my $defver;
if ($phpcmd) {
foreach my $vers (@avail) {
if ($vers->[1] && $vers->[1] eq $phpcmd) {
$defver = $vers->[0];
}
}
}
}

# Change if needed
if ($defver ne $ver) {
$d->{'nginx_php_version'} = $ver;
&delete_php_fcgi_server($d);
&setup_php_fcgi_server($d);
# Change if needed
if ($defver ne $ver) {
$d->{'nginx_php_version'} = $ver;
&delete_php_fcgi_server($d);
&setup_php_fcgi_server($d);
}
}

return undef;
Expand Down

5 comments on commit 8d30bc9

@iliajie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with multiple virtual-servers and PHP versions. All is working great!. I will tag a new release.

Thanks Jamie!

@iliajie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jamie, I see a bug here - if PHP version is changed from PHP Versions page and execution mode is set to FCGI, then after changing PHP version it fails with an error 502 Bad Gateway. Meanwhile, FPM mode works just fine. Could you double check what an issue could be?

@jcameron
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work OK for me. Which PHP version are you seeing this problem for, and on which Linux distro?

@iliajie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was happening on Ubuntu 20.04 GPL. I made clean reinstall to double check and now it's also fine on my side too. I don't know exactly what was that or how to reproduce it. If it happens again, I will let you know.

@jcameron
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did just checked in a fix to detect the fast where PHP is installed but the PHP CGI package needed to run a fastcgi server is missing.

Please sign in to comment.