/** * Manages php fpm via augeas and monit * * @param $meta * @throws \Exception */ protected function manageFpm($meta) { if (empty($meta['fpm']['port']) && empty($meta['_old']['fpm']['port'])) { return; } if ($meta['installed'] && !empty($meta['fpm']['port'])) { $this->output->writeInfo("Checking fpm"); } else { $this->output->writeInfo("Removing fpm"); } $fs = Util::getFilesystem(); $monit_conf_file = "/etc/monit/conf.d/php-{$meta['_build']}-fpm.conf"; $apache_conf_file = "/etc/apache2/php/php-{$meta['_build']}-fpm.conf"; if ($meta['installed'] && empty($meta['fpm']['port']) && !empty($meta['_old']['fpm']['port'])) { $this->stopFpm($meta['_build']); } if (!$meta['installed'] || empty($meta['fpm']['port'])) { if (file_exists($monit_conf_file)) { $this->output->writeInfo("Removing monit php-fpm config file '{$monit_conf_file}'"); $fs->remove($monit_conf_file); } if (file_exists($apache_conf_file)) { $this->output->writeInfo("Removing apache php-fpm config file '{$apache_conf_file}'"); $fs->remove($apache_conf_file); } return; } $fpm_config_file = $meta['_path'] . '/etc/php-fpm.conf'; $fpm_config_updated_1 = Util::augeas('PHP', $fpm_config_file, 'www/listen', '127.0.0.1:' . $meta['fpm']['port']); $fpm_config_updated_2 = Util::augeas('PHP', $fpm_config_file, 'www/user', 'www-data'); $fpm_config_updated_3 = Util::augeas('PHP', $fpm_config_file, 'www/group', 'www-data'); if (0 === $fpm_config_updated_1 || 0 === $fpm_config_updated_2 || 0 === $fpm_config_updated_3) { $this->output->writeError("Failed to configure fpm config file '{$fpm_config_file}'"); } else { if (2 === $fpm_config_updated_1 || 2 === $fpm_config_updated_2 || 2 === $fpm_config_updated_3) { $this->output->writeInfo("Configured fpm"); } } $monit_conf_template = Util::renderTemplate('monit/conf.d/php-fpm.conf.php', ['build' => $meta['_build'], 'port' => $meta['fpm']['port']]); if (!file_exists($monit_conf_file) || md5($monit_conf_template) !== md5_file($monit_conf_file)) { $this->output->writeInfo("Writing monit php-fpm config file '{$monit_conf_file}'"); $fs->write($monit_conf_file, $monit_conf_template, 'root'); } $apache_conf_template = Util::renderTemplate('apache/php/php-fpm.conf.php', ['build' => $meta['_build'], 'port' => $meta['fpm']['port']]); if (!file_exists($apache_conf_file) || md5($apache_conf_template) !== md5_file($apache_conf_file)) { $this->output->writeInfo("Writing apache php-fpm config file '{$apache_conf_file}'"); $fs->write($apache_conf_file, $apache_conf_template, 'root'); } if (!isset($meta['fpm']['autostart']) || $meta['fpm']['autostart']) { ServiceManager::addService("php-{$meta['_build']}-fpm"); } }
/** * Manages project vhost * * @param $action * @param $id * @param $project * @throws \Exception */ protected function processVhost($action, $id, $project) { static $verbs; if (null === $verbs) { $verbs = ['delete' => ['Removing', 'remove'], 'modify' => ['Updating', 'update'], 'create' => ['Writing', 'write'], 'skip' => ['Skipping']]; $verbs['check'] = $verbs['modify']; } $vhost = array_merge(['docroot' => '${dir}', 'servername' => $id, 'options' => ['Indexes', 'FollowSymLinks'], 'override' => ['All'], 'directoryindex' => '', 'ssl' => false, 'ssl_cert' => '/etc/ssl/certs/ssl-cert-snakeoil.pem', 'ssl_key' => '/etc/ssl/private/ssl-cert-snakeoil.key', 'ssl_certs_dir' => '/etc/ssl/certs', 'php-fpm' => ''], $project['vhost']); $vhost_file = "/etc/apache2/sites-enabled/{$id}.conf"; $vhost_ssl_file = "/etc/apache2/sites-enabled/{$id}-ssl.conf"; if ($action == 'skip') { if (!file_exists($vhost_file) || $vhost['ssl'] && !file_exists($vhost_ssl_file)) { $action = 'create'; } else { $this->output->writeDebug("{$verbs[$action][0]} apache vhost for '{$id}'"); if ($vhost['ssl']) { $this->output->writeDebug("{$verbs[$action][0]} apache ssl vhost for '{$id}'"); } return; } } $fs = Util::getFilesystem(); if ($action == 'delete') { if (file_exists($vhost_file)) { $this->output->writeInfo("{$verbs[$action][0]} apache vhost for '{$id}'"); $fs->remove($vhost_file); } if (file_exists($vhost_ssl_file)) { $this->output->writeInfo("{$verbs[$action][0]} apache ssl vhost for '{$id}'"); $fs->remove($vhost_ssl_file); } return; } if (!$vhost['ssl'] && file_exists($vhost_ssl_file)) { $this->output->writeInfo("{$verbs['delete'][0]} apache ssl vhost for '{$id}'"); $fs->remove($vhost_ssl_file); } // Defauly vhost directory if (empty($vhost['directories'])) { $vhost['directories'] = [['provider' => 'directory', 'path' => $vhost['docroot'], 'options' => $vhost['options'], 'allow_override' => $vhost['override'], 'directoryindex' => $vhost['directoryindex'], 'require' => 'all granted']]; } if (!empty($project['php'])) { $vhost = $this->__setVhostFpmInclude($id, $project, $vhost); } foreach ($vhost['directories'] as $key => $dir) { if (empty($dir['path']) || empty($dir['provider'])) { unset($vhost['directories'][$key]); continue; } if (!preg_match('(directory|location|files)', $dir['provider'])) { $dir['provider'] = 'directory'; } $vhost['directories'][$key]['provider'] = ucfirst(str_replace('match', 'Match', $dir['provider'])); } $vhost['serveradmin'] = "admin@{$vhost['servername']}"; $vhost['port'] = '80'; $vhost['access_log'] = "/var/log/apache2/vhost-{$id}.access.log"; $vhost['error_log'] = "/var/log/apache2/vhost-{$id}.error.log"; $vhost = $this->__replaceVhostVars($vhost, $project['_path']); $vhost_file_content = Util::renderTemplate('apache/vhost.php', ['vhost' => $vhost, '_project_id' => $id, '_project_file_path' => $project['_path']]); $vhost_file_save = false; if (!file_exists($vhost_file) || md5($vhost_file_content) !== md5_file($vhost_file)) { $vhost_file_save = true; } if ($vhost_file_save) { $this->output->writeInfo("{$verbs[$action][0]} apache vhost file for '{$id}'"); $fs->write($vhost_file, $vhost_file_content, 'root'); } else { $this->output->writeDebug("{$verbs['skip'][0]} {$verbs[$action][0]} apache vhost for '{$id}'"); } if ($vhost['ssl']) { $vhost_ssl = $vhost; $vhost_ssl['port'] = '443'; $vhost_ssl['access_log'] = "/var/log/apache2/vhost-{$id}-ssl.access.log"; $vhost_ssl['error_log'] = "/var/log/apache2/vhost-{$id}-ssl.error.log"; $vhost_ssl_file_content = Util::renderTemplate('apache/vhost.php', ['vhost' => $vhost_ssl, '_project_id' => $id, '_project_file_path' => $project['_path']]); $vhost_ssl_file_save = false; if (!file_exists($vhost_ssl_file) || md5($vhost_ssl_file_content) !== md5_file($vhost_ssl_file)) { $vhost_ssl_file_save = true; } if ($vhost_ssl_file_save) { $this->output->writeInfo("{$verbs[$action][0]} apache ssl vhost file for '{$id}'"); $fs->write($vhost_ssl_file, $vhost_ssl_file_content, 'root'); } else { $this->output->writeDebug("{$verbs['skip'][0]} {$verbs[$action][0]} apache ssl vhost for '{$id}'"); } } }