Fix: split fpm-uri flag in socket and url flag (#5364)

fixes #5249

(cherry picked from commit 85621487e86a400fd650e6797804ebadc6c9ec92)
This commit is contained in:
Jan Schuppik 2025-05-06 11:35:10 +02:00 committed by Johannes Meyer
parent 67ff0546c0
commit cdc60466b1
4 changed files with 102 additions and 22 deletions

View File

@ -102,7 +102,11 @@ class ConfigCommand extends Command
* *
* --enable-fpm Enable FPM handler for Apache (Nginx is always enabled) * --enable-fpm Enable FPM handler for Apache (Nginx is always enabled)
* *
* --fpm-uri=<uri> Address or path where to pass requests to FPM [127.0.0.1:9000] * --fpm-url=<url> Address where to pass requests to FPM [127.0.0.1:9000]
*
* --fpm-uri=<uri> Alias for --fpm-url
*
* --fpm-socket-path=<socketpath> Socket path where to pass requests to FPM, overrides --fpm-url
* *
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2] * --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
* *
@ -120,9 +124,13 @@ class ConfigCommand extends Command
* icingacli setup config webserver apache \ * icingacli setup config webserver apache \
* --file=/etc/apache2/conf.d/icingaweb2.conf * --file=/etc/apache2/conf.d/icingaweb2.conf
* *
* icingacli setup config webserver apache \
* --file=/etc/apache2/conf.d/icingaweb2.conf
* --fpm-url=localhost:9000
*
* icingacli setup config webserver nginx \ * icingacli setup config webserver nginx \
* --root=/usr/share/icingaweb2/public \ * --root=/usr/share/icingaweb2/public \
* --fpm-uri=unix:/var/run/php5-fpm.sock * --fpm-socket-path=/var/run/php8.3-fpm.sock
*/ */
public function webserverAction() public function webserverAction()
{ {
@ -157,10 +165,18 @@ class ConfigCommand extends Command
$enableFpm = $this->params->shift('enable-fpm', $webserver->getEnableFpm()); $enableFpm = $this->params->shift('enable-fpm', $webserver->getEnableFpm());
$fpmUri = trim($this->params->get('fpm-uri', $webserver->getFpmUri())); $fpmSocketPath = trim($this->params->get('fpm-socket-path', $webserver->getFpmSocketPath()));
if (empty($fpmUri)) { $fpmUrl = trim($this->params->get('fpm-url', $webserver->getFpmUrl()));
if (empty($fpmUrl)) {
$fpmUrl = trim($this->params->get('fpm-uri', $webserver->getFpmUrl()));
}
if (empty($fpmSocketPath) && empty($fpmUrl)) {
$this->fail($this->translate( $this->fail($this->translate(
'The argument --fpm-uri expects an address or path where to pass requests to FPM' 'One of the arguments --fpm-socket-path or --fpm-url must be set to pass requests to FPM'
));
} elseif (!empty($fpmSocketPath) && !empty($fpmUrl)) {
$this->fail($this->translate(
'Only one of the arguments --fpm-socket-path or --fpm-url must be set to pass requests to FPM'
)); ));
} }
$webserver $webserver
@ -168,7 +184,8 @@ class ConfigCommand extends Command
->setConfigDir($configDir) ->setConfigDir($configDir)
->setUrlPath($urlPath) ->setUrlPath($urlPath)
->setEnableFpm($enableFpm) ->setEnableFpm($enableFpm)
->setFpmUri($fpmUri); ->setFpmUrl($fpmUrl)
->setFpmSocketPath($fpmSocketPath);
$config = $webserver->generate() . "\n"; $config = $webserver->generate() . "\n";
if (($file = $this->params->get('file')) !== null) { if (($file = $this->params->get('file')) !== null) {
if (file_exists($file) === true) { if (file_exists($file) === true) {

View File

@ -33,11 +33,25 @@ abstract class Webserver
protected $configDir; protected $configDir;
/** /**
* Address or path where to pass requests to FPM * Address where to pass requests to FPM
* *
* @var string * @var string
*/ */
protected $fpmUri; protected $fpmUrl;
/**
* Socket path where to pass requests to FPM
*
* @var string
*/
protected $fpmSocketPath;
/**
* FPM socket connection schema
*
* @var string
*/
protected $fpmSocketSchema = 'unix:';
/** /**
* Enable to pass requests to FPM * Enable to pass requests to FPM
@ -72,6 +86,7 @@ abstract class Webserver
public function generate() public function generate()
{ {
$template = $this->getTemplate(); $template = $this->getTemplate();
$fpmUri = $this->createFpmUri();
$searchTokens = array( $searchTokens = array(
'{urlPath}', '{urlPath}',
@ -85,7 +100,7 @@ abstract class Webserver
$this->getDocumentRoot(), $this->getDocumentRoot(),
preg_match('~/$~', $this->getUrlPath()) ? $this->getDocumentRoot() . '/' : $this->getDocumentRoot(), preg_match('~/$~', $this->getUrlPath()) ? $this->getDocumentRoot() . '/' : $this->getDocumentRoot(),
$this->getConfigDir(), $this->getConfigDir(),
$this->getFpmUri() $fpmUri
); );
$template = str_replace($searchTokens, $replaceTokens, $template); $template = str_replace($searchTokens, $replaceTokens, $template);
return $template; return $template;
@ -98,6 +113,13 @@ abstract class Webserver
*/ */
abstract protected function getTemplate(); abstract protected function getTemplate();
/**
* Creates the connection string for the respective web server
*
* @return string
*/
abstract protected function createFpmUri();
/** /**
* Set the URL path of Icinga Web 2 * Set the URL path of Icinga Web 2
* *
@ -208,25 +230,47 @@ abstract class Webserver
} }
/** /**
* Get the address or path where to pass requests to FPM * Get the address where to pass requests to FPM
* *
* @return string * @return string
*/ */
public function getFpmUri() public function getFpmUrl()
{ {
return $this->fpmUri; return $this->fpmUrl;
} }
/** /**
* Set the address or path where to pass requests to FPM * Set the address where to pass requests to FPM
* *
* @param string $uri * @param string $url
* *
* @return $this * @return $this
*/ */
public function setFpmUri($uri) public function setFpmUrl($url)
{ {
$this->fpmUri = (string) $uri; $this->fpmUrl = (string) $url;
return $this;
}
/**
* Get the socket path where to pass requests to FPM
*
* @return string
*/
public function getFpmSocketPath()
{
return $this->fpmSocketPath;
}
/**
* Set the socket path where to pass requests to FPM
*
* @return $this
*/
public function setFpmSocketPath($socketPath)
{
$this->fpmSocketPath = (string) $socketPath;
return $this; return $this;
} }

View File

@ -10,7 +10,21 @@ use Icinga\Module\Setup\Webserver;
*/ */
class Apache extends Webserver class Apache extends Webserver
{ {
protected $fpmUri = '127.0.0.1:9000'; protected $fpmUrl = '127.0.0.1:9000';
protected $fpmUrlSchema = 'fcgi://';
protected function createFpmUri()
{
$apacheFpmUri = "";
if (empty($this->fpmSocketPath)) {
$apacheFpmUri = $this->fpmUrlSchema . $this->fpmUrl;
} else {
$apacheFpmUri = $this->fpmSocketSchema . $this->fpmSocketPath . '|' . $this->fpmUrlSchema . 'localhost';
}
return $apacheFpmUri;
}
protected function getTemplate() protected function getTemplate()
{ {
@ -23,7 +37,7 @@ Alias {urlPath} "{aliasDocumentRoot}"
# # Forward PHP requests to FPM # # Forward PHP requests to FPM
# SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 # SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
# <LocationMatch "^{urlPath}/(.*\.php)$"> # <LocationMatch "^{urlPath}/(.*\.php)$">
# ProxyPassMatch "fcgi://{fpmUri}/{documentRoot}/$1" # ProxyPassMatch "{fpmUri}/{documentRoot}/$1"
# </LocationMatch> # </LocationMatch>
#</IfVersion> #</IfVersion>
@ -71,7 +85,7 @@ Alias {urlPath} "{aliasDocumentRoot}"
# # Forward PHP requests to FPM # # Forward PHP requests to FPM
# SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 # SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
# <FilesMatch "\.php$"> # <FilesMatch "\.php$">
# SetHandler "proxy:fcgi://{fpmUri}" # SetHandler "proxy:{fpmUri}"
# ErrorDocument 503 {urlPath}/error_unavailable.html # ErrorDocument 503 {urlPath}/error_unavailable.html
# </FilesMatch> # </FilesMatch>
# </IfVersion> # </IfVersion>
@ -85,7 +99,7 @@ Alias {urlPath} "{aliasDocumentRoot}"
# Forward PHP requests to FPM # Forward PHP requests to FPM
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
<LocationMatch "^{urlPath}/(.*\.php)$"> <LocationMatch "^{urlPath}/(.*\.php)$">
ProxyPassMatch "fcgi://{fpmUri}/{documentRoot}/$1" ProxyPassMatch "{fpmUri}/{documentRoot}/$1"
</LocationMatch> </LocationMatch>
</IfVersion> </IfVersion>
@ -131,7 +145,7 @@ Alias {urlPath} "{aliasDocumentRoot}"
# Forward PHP requests to FPM # Forward PHP requests to FPM
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
<FilesMatch "\.php$"> <FilesMatch "\.php$">
SetHandler "proxy:fcgi://{fpmUri}" SetHandler "proxy:{fpmUri}"
ErrorDocument 503 {urlPath}/error_unavailable.html ErrorDocument 503 {urlPath}/error_unavailable.html
</FilesMatch> </FilesMatch>
</IfVersion> </IfVersion>

View File

@ -10,10 +10,15 @@ use Icinga\Module\Setup\Webserver;
*/ */
class Nginx extends Webserver class Nginx extends Webserver
{ {
protected $fpmUri = '127.0.0.1:9000'; protected $fpmUrl = '127.0.0.1:9000';
protected $enableFpm = true; protected $enableFpm = true;
protected function createFpmUri()
{
return empty($this->fpmSocketPath) ? $this->fpmUrl : $this->fpmSocketSchema . $this->fpmSocketPath;
}
protected function getTemplate() protected function getTemplate()
{ {
return <<<'EOD' return <<<'EOD'