Skip to content

Commit

Permalink
Fixed pool configuration for multiple php version.
Browse files Browse the repository at this point in the history
  • Loading branch information
eppak committed Sep 16, 2021
1 parent cfc7ba9 commit b52b18e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
7 changes: 6 additions & 1 deletion src/Stages/V1804/CheckServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class CheckServices extends StageBase implements Stage
'nginx' => 'Nginx is not running',
'redis' => 'Redis is not running',
'supervisor' => 'Supervisror is not running',
'php' . APP_PANEL_PHP_VERSION . '-fpm' => 'PHP FPM is not running',
'unattended-upgrades' => 'Security updates not enabled'
];

Expand All @@ -35,6 +34,12 @@ class CheckServices extends StageBase implements Stage
public function run(Environment $env): bool
{
try {
$versions = $env->getArray('php_versions');

foreach($versions as $version) {
$this->services["php{$version}-fpm"] = "PHP FPM {$version} is not running";
}

foreach ($this->services as $service => $error) {
if (!$this->active($service, $error)) {
return false;
Expand Down
69 changes: 43 additions & 26 deletions src/Stages/V1804/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,46 @@ public function run(Environment $env): bool
try {
$this->version($env->getArray('php_versions'));

if (
!$this->write(
"/etc/php/{$php}/fpm/conf.d/sculptor.ini",
$this->template('php.ini'),
'Cannot write ini configuration'
)
) {
return false;
}

foreach ([ APP_PANEL_HTTP_USER, APP_PANEL_HTTP_PANEL ] as $user) {
if (!$this->pool($php, $user)) {
return false;
}
}
$this->agent($php);

if (!$this->restart("php{$php}-fpm")) {
$this->internal = 'Cannot restart service';

return false;
}
$this->command(['update-alternatives', '--set', 'php', '/usr/bin/php' . APP_PANEL_PHP_VERSION]);

return true;
} catch (Exception $e) {
$this->internal = $e->getMessage();
Log::error($e->getMessage());

return false;
}
}

/**
* @param string $php
* @return void
*/
private function agent(string $php): void
{
if (
!$this->write(
"/etc/php/{$php}/fpm/conf.d/sculptor.ini",
$this->template('php.ini'),
'Cannot write ini configuration'
)
) {
throw new Exception("Cannot write php{$php} for sculptor agent");
}

foreach ([ APP_PANEL_HTTP_USER, APP_PANEL_HTTP_PANEL ] as $user) {
if (!$this->pool($php, $user)) {
throw new Exception("Cannot write php{$php} for user {$user}");
}
}

if (!$this->restart("php{$php}-fpm")) {
throw new Exception("Cannot restart php{$php} service");
}
}

/**
* @param string $php
* @param string $user
Expand All @@ -79,6 +89,7 @@ private function pool(string $php, string $user): bool
{
$pool = $this->replaceTemplate('php-pool.conf')
->replace("{USER}", $user)
->replace("{PHP}", $php)
->value();

if (!$this->write("/etc/php/{$php}/fpm/pool.d/{$user}.conf", $pool, "Cannot write pool configuration {$user}")) {
Expand All @@ -88,7 +99,11 @@ private function pool(string $php, string $user): bool
return true;
}

private function version(array $versions): bool
/**
* @param array $versions
* @return void
*/
private function version(array $versions): void
{
foreach ($versions as $version) {
$modules = collect($this->modules)
Expand All @@ -98,12 +113,14 @@ private function version(array $versions): bool

$this->command(collect(['apt-get', '-y', 'install'])->concat($modules)->toArray());

$this->pool($version, APP_PANEL_HTTP_USER);
}

$this->command(['update-alternatives', '--set', 'php', '/usr/bin/php' . APP_PANEL_PHP_VERSION]);
if (!$this->pool($version, APP_PANEL_HTTP_USER)) {
throw new Exception("Cannot restart php{$version} service");
}

return true;
if (!$this->restart("php{$version}-fpm")) {
throw new Exception("Cannot restart php{$version} service");
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/php-pool.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[{USER}]
user = {USER}
group = {USER}
listen = /run/php/php7.4-fpm-{USER}.sock
listen = /run/php/php{PHP}-fpm-{USER}.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
Expand Down

0 comments on commit b52b18e

Please sign in to comment.