Add Caddy Server Support Alongside Nginx (#600)

* added enum

* add config for caddy

* add svg icon

* add caddy service class

* wip

* install caddy

* create base Caddyfile with common snippets

* Create a systemd service to run Caddy in the background.

* create uninstall file

* wip

* create path

* create vhost

* get vhost

* delete site

* add php version change file

* add custom ssl

* create redirect file

* add vhost for caddy site & load balancer

* update svg

* fix caddy icon

* fix style

* add systemctl reload method

* Reload systemd after modifying the Caddy service file.

* add caddy

* added tests

* format with pint

* prevent multiple web server installations

* added error log & access log

(cherry picked from commit 2318e1b1df)
This commit is contained in:
Rasel Islam Rafi
2025-05-29 15:25:36 +06:00
committed by Saeed Vaziry
parent 61506ff70e
commit 984c1f3a8e
24 changed files with 590 additions and 18 deletions

View File

@ -0,0 +1,195 @@
<?php
namespace App\SSH\Services\Webserver;
use App\Exceptions\SSHError;
use App\Exceptions\SSLCreationException;
use App\Models\Site;
use App\Models\Ssl;
use Throwable;
class Caddy extends AbstractWebserver
{
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.install-caddy'),
'install-caddy'
);
$this->service->server->ssh()->write(
'/etc/caddy/Caddyfile',
view('ssh.services.webserver.caddy.caddy'),
'root'
);
$this->service->server->ssh()->write(
'/etc/systemd/system/caddy.service',
view('ssh.services.webserver.caddy.caddy-systemd'),
'root'
);
$this->service->server->systemd()->reload();
$this->service->server->systemd()->restart('caddy');
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.uninstall-caddy'),
'uninstall-caddy'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function createVHost(Site $site): void
{
// We need to get the isolated user first, if the site is isolated
// otherwise, use the default ssh user
$ssh = $this->service->server->ssh($site->user);
$ssh->exec(
view('ssh.services.webserver.caddy.create-path', [
'path' => $site->path,
]),
'create-path',
$site->id
);
$this->service->server->ssh()->write(
'/etc/caddy/sites-available/'.$site->domain,
$this->generateVhost($site),
'root'
);
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.create-vhost', [
'domain' => $site->domain,
]),
'create-vhost',
$site->id
);
}
/**
* @throws SSHError
*/
public function updateVHost(Site $site, ?string $vhost = null): void
{
$this->service->server->ssh()->write(
'/etc/caddy/sites-available/'.$site->domain,
$vhost ?? $this->generateVhost($site),
'root'
);
$this->service->server->systemd()->restart('caddy');
}
/**
* @throws SSHError
*/
public function getVHost(Site $site): string
{
return $this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.get-vhost', [
'domain' => $site->domain,
]),
);
}
/**
* @throws SSHError
*/
public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.delete-site', [
'domain' => $site->domain,
'path' => $site->path,
]),
'delete-vhost',
$site->id
);
$this->service->restart();
}
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, string $version): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.change-php-version', [
'domain' => $site->domain,
'oldVersion' => $site->php_version,
'newVersion' => $version,
]),
'change-php-version',
$site->id
);
}
/**
* @throws SSHError
*/
public function setupSSL(Ssl $ssl): void
{
if ($ssl->type == 'custom') {
$ssl->certificate_path = '/etc/ssl/'.$ssl->id.'/cert.pem';
$ssl->pk_path = '/etc/ssl/'.$ssl->id.'/privkey.pem';
$ssl->save();
$command = view('ssh.services.webserver.caddy.create-custom-ssl', [
'path' => dirname($ssl->certificate_path),
'certificate' => $ssl->certificate,
'pk' => $ssl->pk,
'certificatePath' => $ssl->certificate_path,
'pkPath' => $ssl->pk_path,
]);
$result = $this->service->server->ssh()->setLog($ssl->log)->exec(
$command,
'create-ssl',
$ssl->site_id
);
if (! $ssl->validateSetup($result)) {
throw new SSLCreationException;
}
}
}
/**
* @throws Throwable
*/
public function removeSSL(Ssl $ssl): void
{
if ($ssl->certificate_path) {
$this->service->server->ssh()->exec(
'sudo rm -rf '.dirname($ssl->certificate_path),
'remove-ssl',
$ssl->site_id
);
}
$this->updateVHost($ssl->site);
}
private function generateVhost(Site $site): string
{
$vhost = view('ssh.services.webserver.caddy.vhost', [
'site' => $site,
]);
return format_nginx_config($vhost);
}
}