From 18dfe257fb77a9511f8c84cafb0ced39a5cbf342 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Tue, 14 May 2024 13:15:26 +0300 Subject: [PATCH] update --- web/app/Filament/Pages/Settings/Settings.php | 28 ++++++------- web/app/MasterDomain.php | 42 ++++++++------------ web/app/VirtualHosts/ApacheBuild.php | 27 +++++++++++++ 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/web/app/Filament/Pages/Settings/Settings.php b/web/app/Filament/Pages/Settings/Settings.php index 754cb76..4d9bb58 100644 --- a/web/app/Filament/Pages/Settings/Settings.php +++ b/web/app/Filament/Pages/Settings/Settings.php @@ -42,23 +42,21 @@ class Settings extends BaseSettings // Restart supervisor shell_exec('service supervisor restart'); - - if ($oldMasterDomain != setting('general.master_domain')) { - // Make master domain virtual host - $masterDomain = new MasterDomain(); - $masterDomain->configureVirtualHost(); - } - - $wildcardDomain = setting('general.wildcard_domain'); - if ($oldWildcardDomain != $wildcardDomain) { - // Make wildcard domain virtual host - $masterDomain = new MasterDomain(); - $masterDomain->domain = $wildcardDomain; - $masterDomain->configureVirtualHost(); - } - file_put_contents('/var/www/html/index.html', setting('general.master_domain_page_html')); + $rebuildApache = false; + if ($oldMasterDomain != setting('general.master_domain')) { + $rebuildApache = true; + } + if ($oldWildcardDomain != setting('general.wildcard_domain')) { + $rebuildApache = true; + } + if ($rebuildApache) { + $apacheBuild = new \App\VirtualHosts\ApacheBuild(); + $apacheBuild->fixPermissions(); + $apacheBuild->build(); + } + } public function schema(): array|Closure diff --git a/web/app/MasterDomain.php b/web/app/MasterDomain.php index 9aac920..7ce6bb4 100644 --- a/web/app/MasterDomain.php +++ b/web/app/MasterDomain.php @@ -32,7 +32,7 @@ class MasterDomain } - public function configureVirtualHost() + public function configureVirtualHost($fixPermissions = false) { // check is valid domain if (!filter_var($this->domain, FILTER_VALIDATE_DOMAIN)) { @@ -49,14 +49,11 @@ class MasterDomain $apacheBaseConfig = $apacheVirtualHostBuilder->buildConfig(); - shell_exec('mkdir -p /var/www/logs/apache2'); - shell_exec('touch /var/www/logs/apache2/bytes.log'); - shell_exec('touch /var/www/logs/apache2/access.log'); - shell_exec('touch /var/www/logs/apache2/error.log'); - - if (!empty($apacheBaseConfig)) { - file_put_contents('/etc/apache2/sites-available/zzz-'.$this->domain.'.conf', $apacheBaseConfig); - shell_exec('ln -s /etc/apache2/sites-available/zzz-'.$this->domain.'.conf /etc/apache2/sites-enabled/zzz-'.$this->domain.'.conf'); + if ($fixPermissions) { + shell_exec('mkdir -p /var/www/logs/apache2'); + shell_exec('touch /var/www/logs/apache2/bytes.log'); + shell_exec('touch /var/www/logs/apache2/access.log'); + shell_exec('touch /var/www/logs/apache2/error.log'); } // Install SSL @@ -88,6 +85,8 @@ class MasterDomain } } + $apacheBaseConfigWithSSL = null; + if ($findDomainSSLCertificate) { $certsFolderName = $findDomainSSLCertificate->domain; @@ -122,27 +121,12 @@ class MasterDomain $apacheVirtualHostBuilder->setSSLCertificateChainFile($sslCertificateChainFile); $apacheBaseConfigWithSSL = $apacheVirtualHostBuilder->buildConfig(); - if (!empty($apacheBaseConfigWithSSL)) { - // Add SSL options conf file - $apache2SSLOptionsSample = view('actions.samples.ubuntu.apache2-ssl-options-conf')->render(); - $apache2SSLOptionsFilePath = '/etc/apache2/phyre/options-ssl-apache.conf'; - - if (!file_exists($apache2SSLOptionsFilePath)) { - if (!is_dir('/etc/apache2/phyre')) { - mkdir('/etc/apache2/phyre'); - } - file_put_contents($apache2SSLOptionsFilePath, $apache2SSLOptionsSample); - } - - file_put_contents('/etc/apache2/sites-available/zzz-'.$this->domain.'-ssl.conf', $apacheBaseConfigWithSSL); - shell_exec('ln -s /etc/apache2/sites-available/zzz-'.$this->domain.'-ssl.conf /etc/apache2/sites-enabled/zzz-'.$this->domain.'-ssl.conf'); - - } } // End install SSL + if ($fixPermissions) { $domainIndexFile = $this->domainPublic . '/index.html'; if (file_exists($domainIndexFile)) { $domainIndexFileContent = file_get_contents($domainIndexFile); @@ -154,6 +138,12 @@ class MasterDomain shell_exec('chown -R www-data:www-data ' . $this->domainPublic); shell_exec('chmod -R 755 ' . $this->domainPublic); - shell_exec('systemctl restart apache2'); + } + + return [ + 'apacheBaseConfig' => $apacheBaseConfig, + 'apacheBaseConfigWithSSL' => $apacheBaseConfigWithSSL ?? null + ]; + } } diff --git a/web/app/VirtualHosts/ApacheBuild.php b/web/app/VirtualHosts/ApacheBuild.php index b12f55f..a095a51 100644 --- a/web/app/VirtualHosts/ApacheBuild.php +++ b/web/app/VirtualHosts/ApacheBuild.php @@ -2,6 +2,7 @@ namespace App\VirtualHosts; +use App\MasterDomain; use App\Models\Domain; class ApacheBuild @@ -94,6 +95,32 @@ IncludeOptional conf-enabled/*.conf } } + if (!empty(setting('general.master_domain'))) { + // Make master domain virtual host + $masterDomain = new MasterDomain(); + $domainVirtualHost = $masterDomain->configureVirtualHost($this->fixPermissions); + if (isset($domainVirtualHost['apacheBaseConfig'])) { + $virtualHostMerged .= $domainVirtualHost['apacheBaseConfig'] . "\n\n"; + } + if (isset($domainVirtualHost['apacheBaseConfigWithSSL'])) { + $virtualHostMerged .= $domainVirtualHost['apacheBaseConfigWithSSL'] . "\n\n"; + } + } + + $wildcardDomain = setting('general.wildcard_domain'); + if (!empty($wildcardDomain)) { + // Make wildcard domain virtual host + $masterDomain = new MasterDomain(); + $masterDomain->domain = $wildcardDomain; + $domainVirtualHost = $masterDomain->configureVirtualHost($this->fixPermissions); + if (isset($domainVirtualHost['apacheBaseConfig'])) { + $virtualHostMerged .= $domainVirtualHost['apacheBaseConfig'] . "\n\n"; + } + if (isset($domainVirtualHost['apacheBaseConfigWithSSL'])) { + $virtualHostMerged .= $domainVirtualHost['apacheBaseConfigWithSSL'] . "\n\n"; + } + } + file_put_contents('/etc/apache2/apache2.conf', $virtualHostMerged); shell_exec('systemctl reload apache2');