Bozhidar Slaveykov 1 year ago
parent
commit
7eebe1d912

+ 10 - 3
web/Modules/LetsEncrypt/Listeners/HostingAccountIsCreatedListener.php

@@ -2,6 +2,7 @@
 
 namespace Modules\LetsEncrypt\Listeners;
 
+use App\Actions\ApacheWebsiteApplySSLVirtualHost;
 use App\ApiClient;
 use App\Events\HostingAccountIsCreated;
 use App\FileManagerApi;
@@ -80,11 +81,17 @@ class HostingAccountIsCreatedListener
         $websiteSslCertificate->is_active = 1;
         $websiteSslCertificate->is_wildcard = 0;
         $websiteSslCertificate->is_auto_renew = 1;
+        $websiteSslCertificate->provider = 'letsencrypt';
         $websiteSslCertificate->save();
+        
+        $applySSLVirtualHost = new ApacheWebsiteApplySSLVirtualHost();
+        $applySSLVirtualHost->setDomain($event->model->domain);
+        $applySSLVirtualHost->setDomainRoot($event->model->domain_root);
+        $applySSLVirtualHost->setSslCertificateFilePath($sslCertificateFilePath);
+        $applySSLVirtualHost->setSslCertificateKeyFilePath($sslCertificateKeyFilePath);
+        $applySSLVirtualHost->setSslCertificateChainFilePath($sslCertificateChainFilePath);
+        $applySSLVirtualHost->handle();
 
-        dd($sslCertificateFileContent);
-
-        dd(1);
 
     }
 }

+ 61 - 0
web/app/Actions/ApacheWebsiteApplySSLVirtualHost.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Actions;
+
+use App\FileManagerApi;
+use App\ShellApi;
+
+class ApacheWebsiteApplySSLVirtualHost
+{
+    public $domain;
+    public $domainRoot;
+    public $sslCertificateFilePath;
+    public $sslCertificateKeyFilePath;
+    public $sslCertificateChainFilePath;
+
+    public function setSslCertificateFilePath($sslCertificateFilePath)
+    {
+        $this->sslCertificateFilePath = $sslCertificateFilePath;
+    }
+
+    public function setSslCertificateKeyFilePath($sslCertificateKeyFilePath)
+    {
+        $this->sslCertificateKeyFilePath = $sslCertificateKeyFilePath;
+    }
+
+    public function setSslCertificateChainFilePath($sslCertificateChainFilePath)
+    {
+        $this->sslCertificateChainFilePath = $sslCertificateChainFilePath;
+    }
+
+    public function setDomain($domain)
+    {
+        $this->domain = $domain;
+    }
+
+    public function setDomainRoot($domainRoot)
+    {
+        $this->domainRoot = $domainRoot;
+    }
+
+    public function handle()
+    {
+        $settings = [
+            'port'=> 443,
+            'domain' => $this->domain,
+            'domainRoot' => '/var/www/'.$this->domain,
+            'group' => 'www-data',
+            'sslCertificateFilePath' => $this->sslCertificateFilePath,
+            'sslCertificateKeyFilePath' => $this->sslCertificateKeyFilePath,
+            'sslCertificateChainFilePath' => $this->sslCertificateChainFilePath,
+        ];
+        $apache2SSLSample = view('actions.samples.ubuntu.apache2-ssl-conf',$settings)->render();
+
+        $fileManagerApi = new FileManagerApi();
+        $fileManagerApi->filePutContents('/etc/apache2/sites-available/'.$this->domain.'-ssl.conf', $apache2SSLSample);
+        $fileManagerApi->symlink('/etc/apache2/sites-available/'.$this->domain.'-ssl.conf', '/etc/apache2/sites-enabled/'.$this->domain.'-ssl.conf');
+
+        ShellApi::exec('service apache2 restart');
+
+    }
+}

+ 5 - 0
web/app/FileManagerApi.php

@@ -14,6 +14,11 @@ class FileManagerApi
         return ShellApi::exec('cat ' . $file);
     }
 
+    public function symlink($source, $destination)
+    {
+        ShellApi::exec('ln -s ' . $source . ' ' . $destination);
+    }
+
     public function filePutContents($file, $data)
     {
         $tempfileName = md5($file . time() . rand(111, 999));

+ 3 - 0
web/database/migrations/2024_04_01_073021_create_website_ssl_certificates_table.php

@@ -16,6 +16,8 @@ return new class extends Migration
 
             $table->string('domain');
 
+            $table->string('provider')->nullable();
+
             $table->integer('user_id')->nullable();
             $table->integer('is_active')->nullable();
             $table->integer('is_wildcard')->nullable();
@@ -30,6 +32,7 @@ return new class extends Migration
             $table->longText('private_key')->nullable();
             $table->longText('certificate_chain')->nullable();
 
+
             $table->timestamps();
         });
     }

+ 3 - 0
web/db-migrate.sh

@@ -0,0 +1,3 @@
+PHYRE_PHP=/usr/local/phyre/php/bin/php
+
+$PHYRE_PHP artisan migrate

+ 21 - 0
web/resources/views/actions/samples/ubuntu/apache2-ssl-conf.blade.php

@@ -0,0 +1,21 @@
+<IfModule mod_ssl.c>
+    <VirtualHost *:{{$port}}>
+
+        ServerName {{$domain}}
+        DocumentRoot {{$domainRoot}}/public_html
+        SetEnv APP_DOMAIN {{$domain}}
+
+        <Directory {{$domainRoot}}/public_html>
+        Options Indexes FollowSymLinks MultiViews
+        AllowOverride All
+        Require all granted
+
+        </Directory>
+
+        SSLCertificateFile {{$sslCertificateFilePath}}
+        SSLCertificateKeyFile {{$sslCertificateKeyFilePath}}
+        SSLCertificateChainFile {{$sslCertificateChainFilePath}}
+
+        Include /etc/letsencrypt/options-ssl-apache.conf
+    </VirtualHost>
+</IfModule>