Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VHost Templating using Twig #435

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Actions/Site/UpdateAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function update(Site $site, array $input): void

/** @var Webserver $webserver */
$webserver = $site->server->webserver()->handler();
$webserver->updateVHost($site, ! $site->hasSSL());
$webserver->updateVHost($site);

$site->save();
}
Expand Down
116 changes: 69 additions & 47 deletions app/SSH/Services/Webserver/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use App\Models\Ssl;
use App\SSH\HasScripts;
use Closure;
use Illuminate\Support\Str;
use Throwable;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class Nginx extends AbstractWebserver
{
Expand Down Expand Up @@ -54,39 +55,79 @@ public function uninstall(): void

public function createVHost(Site $site): void
{
$template = $this->getScript('nginx/vhost.config.twig');
$vhost = $this->renderTemplate($template, $site);

$this->service->server->ssh()->exec(
$this->getScript('nginx/create-vhost.sh', [
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $this->generateVhost($site),
'vhost' => $vhost,
]),
'create-vhost',
$site->id
);

$this->updateVHostTemplate($site, $template);
}

public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void
public function validateVHost(Site $site, string $vhost): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/update-vhost.sh', [
$this->getScript('nginx/validate-vhost.sh', [
'vhost' => $vhost,
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $vhost ?? $this->generateVhost($site, $noSSL),
]),
'update-vhost',
'validate-vhost',
$site->id
);
}

public function getVHost(Site $site): string
public function updateVHostTemplate(Site $site, string $template): void
{
return $this->service->server->ssh()->exec(
$this->getScript('nginx/get-vhost.sh', [
$this->service->server->ssh()->exec(
$this->getScript('nginx/update-vhost-template.sh', [
'template' => $template,
'domain' => $site->domain,
]),
'update-vhost-template',
$site->id
);
}

public function updateVHost(Site $site, ?string $vhost = null): void
{
$template = $vhost ?? $this->getScript('nginx/vhost.config.twig');
$data = $this->renderTemplate($template, $site);

$this->updateVHostTemplate($site, $template);
$this->validateVHost($site, $data);

$this->service->server->ssh()->exec(
$this->getScript('nginx/update-vhost.sh', [
'vhost' => $data,
'domain' => $site->domain,
]),
'update-vhost',
$site->id
);
}

public function getVHostTemplate(Site $site): string
{
try {
$template = $this->service->server->ssh()->exec(
$this->getScript('nginx/get-vhost-template.sh', [
'domain' => $site->domain,
]),
);
} catch (SSHError) {
$template = $this->getScript('nginx/vhost.config.twig');
}

return $template;
}

public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
Expand Down Expand Up @@ -160,49 +201,30 @@ public function removeSSL(Ssl $ssl): void
$ssl->site_id
);

$this->updateVHost($ssl->site, true);
$this->updateVHost($ssl->site);

$this->service->server->systemd()->restart('nginx');
}

protected function generateVhost(Site $site, bool $noSSL = false): string
public function renderTemplate(string $template, Site $site): string
{
$ssl = $site->activeSsl;
if ($noSSL) {
$ssl = null;
}
$vhost = $this->getScript('nginx/vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/vhost-ssl.conf');
}
if ($site->type()->language() === 'php') {
$vhost = $this->getScript('nginx/php-vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/php-vhost-ssl.conf');
}
}
if ($site->port) {
$vhost = $this->getScript('nginx/reverse-vhost.conf');
if ($ssl) {
$vhost = $this->getScript('nginx/reverse-vhost-ssl.conf');
}
$vhost = Str::replace('__port__', (string) $site->port, $vhost);
}
$templateName = 'vhost_template_'.md5($template);
$twig = new Environment(new ArrayLoader([$templateName => $template]));

$vhost = Str::replace('__domain__', $site->domain, $vhost);
$vhost = Str::replace('__aliases__', $site->getAliasesString(), $vhost);
$vhost = Str::replace('__path__', $site->path, $vhost);
$vhost = Str::replace('__web_directory__', $site->web_directory, $vhost);

if ($ssl) {
$vhost = Str::replace('__certificate__', $ssl->getCertificatePath(), $vhost);
$vhost = Str::replace('__private_key__', $ssl->getPkPath(), $vhost);
}

if ($site->php_version) {
$vhost = Str::replace('__php_version__', $site->php_version, $vhost);
}
$ssl = $site->activeSsl;
$configs = [
'http_mode' => $ssl == null ? 'http' : 'https',
'domain_name' => $site->domain,
'aliases' => $site->getAliasesString(),
'path' => $site->path,
'web_directory' => $site->web_directory,
'certificate' => $ssl?->getCertificatePath(),
'private_key' => $ssl?->getPkPath(),
'language' => $site->type()->language(),
'php_version' => $site->php_version,
'port' => $site->port ?? null,
];

return $vhost;
return trim($twig->render($templateName, $configs));
}
}
4 changes: 2 additions & 2 deletions app/SSH/Services/Webserver/Webserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interface Webserver
{
public function createVHost(Site $site): void;

public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void;
public function updateVHost(Site $site, ?string $vhost = null): void;

public function getVHost(Site $site): string;
public function getVHostTemplate(Site $site): string;

public function deleteSite(Site $site): void;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cat /etc/nginx/templates/__domain__
1 change: 0 additions & 1 deletion app/SSH/Services/Webserver/scripts/nginx/get-vhost.sh

This file was deleted.

38 changes: 0 additions & 38 deletions app/SSH/Services/Webserver/scripts/nginx/php-vhost-ssl.conf

This file was deleted.

34 changes: 0 additions & 34 deletions app/SSH/Services/Webserver/scripts/nginx/php-vhost.conf

This file was deleted.

35 changes: 0 additions & 35 deletions app/SSH/Services/Webserver/scripts/nginx/reverse-vhost-ssl.conf

This file was deleted.

31 changes: 0 additions & 31 deletions app/SSH/Services/Webserver/scripts/nginx/reverse-vhost.conf

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo '__template__' | sudo tee /etc/nginx/templates/__domain__
1 change: 0 additions & 1 deletion app/SSH/Services/Webserver/scripts/nginx/update-vhost.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
echo '__vhost__' | sudo tee /etc/nginx/sites-available/__domain__

sudo service nginx restart
10 changes: 10 additions & 0 deletions app/SSH/Services/Webserver/scripts/nginx/validate-vhost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sudo mkdir -p /etc/nginx/templates/output

echo 'events {} http { __vhost__ }' | sudo tee /etc/nginx/templates/output/__domain__.conf

if ! sudo nginx -t -c /etc/nginx/templates/output/__domain__.conf; then
echo "VITO_SSH_ERROR"
exit 1
fi

sudo rm -f /etc/nginx/templates/output/__domain__.conf
31 changes: 0 additions & 31 deletions app/SSH/Services/Webserver/scripts/nginx/vhost-ssl.conf

This file was deleted.

Loading