Skip to content

Commit

Permalink
fix default and catch all hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth committed Mar 16, 2023
1 parent d2759d2 commit 38aab1e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 53 deletions.
12 changes: 7 additions & 5 deletions src/VirtualHostGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ private function buildHostList(\PDOStatement $statement, array &$virtualhosts, s
}
$virtualhosts[] = [
'domain' => $vhost,
'vhost' => $vhost,
'webroot' => $row['extra_webroot'] === '1' ? "/var/$vhost/public" : "/var/$vhost",
'root' => "/var/$vhost",
'admin' => $row['admin'],
Expand Down Expand Up @@ -100,7 +99,7 @@ private function buildDefaultHostList(\PDOStatement $statement, array &$virtualh
}
$virtualhosts[] = [
'domain' => $vhost,
'vhost' => '*.' . $row['domain'],
'aliases' => ['*.' . $row['domain']],
'webroot' => "/var/www/public",
'root' => "/var/www",
'admin' => $row['admin'],
Expand All @@ -118,7 +117,7 @@ private function buildServerHost(string $hostname, string $admin, array &$virtua
}
$virtualhosts[] = [
'domain' => $hostname,
'vhost' => $hostname,
'aliases' => ['*'],
'webroot' => "/var/www/public",
'root' => "/var/www",
'admin' => $admin,
Expand All @@ -141,13 +140,16 @@ public function create()
$stmt = $this->database->prepare("SELECT domain.domain, domain.admin FROM domain");
$stmt->execute();
$defaulthosts = [];
$this->buildDefaultHostList($stmt, $defaulthosts, $ip);
$stmt = $this->database->prepare('SELECT admin FROM server WHERE hostname=:hostname');
$stmt->execute([':hostname' => $hostname]);
$this->buildServerHost($hostname, $stmt->fetchColumn(), $virtualhosts, $ip);
$this->buildDefaultHostList($stmt, $defaulthosts, $ip);
file_put_contents(
'/etc/apache2/sites-enabled/all.conf',
$this->twig->render('config.twig', ['virtualhosts' => $virtualhosts, 'defaulthosts' => $defaulthosts])
$this->twig->render('config.twig', [
'virtualhosts' => $virtualhosts,
'defaulthosts' => $defaulthosts,
])
);
exec("service apache2 start");
}
Expand Down
3 changes: 1 addition & 2 deletions templates/config-based-virtual-host.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
ServerAdmin {{ host.admin }}
{% if host.vhost starts with '*' %}Servername {{ host.domain }}
ServerAlias {{ host.vhost }}{% else %}Servername {{ host.vhost }}{% endif %}
Servername {{ host.domain }}

{% for alias in host.aliases %}

Expand Down
46 changes: 0 additions & 46 deletions templates/config.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</VirtualHost>
{% endfor %}
{% for host in defaulthosts %}
{% set default_domain=host.domain %}
<VirtualHost *:80>
{% include 'config-based-virtual-host.twig' %}

Expand All @@ -36,48 +35,3 @@
SSLCertificateChainFile /etc/letsencrypt/live/{{ host.domain }}/chain.pem
</VirtualHost>
{% endfor %}
<VirtualHost *:80>
DocumentRoot /var/www/public
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory />
Options +FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /var/www>
Options +ExecCGI -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<Directory /usr/lib/cgi-bin>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>

SSLEngine Off
RewriteEngine On
RewriteRule (.*) https://{{ default_domain }}$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/public
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory />
Options +FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /var/www>
Options +ExecCGI -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<Directory /usr/lib/cgi-bin>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>

RewriteEngine On
RewriteRule (.*) https://{{ default.domain }}$1 [R,L]
</VirtualHost>

0 comments on commit 38aab1e

Please sign in to comment.