use blade as conmmands template (#444)

* use blade as conmmands template

* fix lint

* fix ssl
This commit is contained in:
Saeed Vaziry
2025-01-27 21:27:58 +01:00
committed by GitHub
parent a73476c1dd
commit cdbde063f0
208 changed files with 1080 additions and 1012 deletions

View File

@ -6,25 +6,31 @@
use App\Exceptions\SSLCreationException;
use App\Models\Site;
use App\Models\Ssl;
use App\SSH\HasScripts;
use Closure;
use Illuminate\Support\Str;
use Throwable;
class Nginx extends AbstractWebserver
{
use HasScripts;
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/install-nginx.sh', [
'config' => $this->getScript('nginx/nginx.conf', [
'user' => $this->service->server->getSshUser(),
]),
]),
view('ssh.services.webserver.nginx.install-nginx'),
'install-nginx'
);
$this->service->server->ssh()->write(
'/etc/nginx/nginx.conf',
view('ssh.services.webserver.nginx.nginx', [
'user' => $this->service->server->getSshUser(),
]),
true
);
$this->service->server->systemd()->restart('nginx');
$this->service->server->os()->cleanup();
}
@ -43,10 +49,13 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/uninstall-nginx.sh'),
view('ssh.services.webserver.nginx.uninstall-nginx'),
'uninstall-nginx'
);
$this->service->server->os()->cleanup();
@ -62,50 +71,68 @@ public function createVHost(Site $site): void
$ssh = $this->service->server->ssh($site->user);
$ssh->exec(
$this->getScript('nginx/create-path.sh', [
view('ssh.services.webserver.nginx.create-path', [
'path' => $site->path,
]),
'create-path',
$site->id
);
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
true
);
$this->service->server->ssh()->exec(
$this->getScript('nginx/create-vhost.sh', [
view('ssh.services.webserver.nginx.create-vhost', [
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $this->generateVhost($site),
'vhost' => view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
]),
'create-vhost',
$site->id
);
}
public function updateVHost(Site $site, bool $noSSL = false, ?string $vhost = null): void
/**
* @throws SSHError
*/
public function updateVHost(Site $site, ?string $vhost = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/update-vhost.sh', [
'domain' => $site->domain,
'path' => $site->path,
'vhost' => $vhost ?? $this->generateVhost($site, $noSSL),
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
$vhost ?? view('ssh.services.webserver.nginx.vhost', [
'site' => $site,
]),
'update-vhost',
$site->id
true
);
$this->service->server->systemd()->restart('nginx');
}
/**
* @throws SSHError
*/
public function getVHost(Site $site): string
{
return $this->service->server->ssh()->exec(
$this->getScript('nginx/get-vhost.sh', [
view('ssh.services.webserver.nginx.get-vhost', [
'domain' => $site->domain,
]),
);
}
/**
* @throws SSHError
*/
public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/delete-site.sh', [
view('ssh.services.webserver.nginx.delete-site', [
'domain' => $site->domain,
'path' => $site->path,
]),
@ -115,13 +142,16 @@ public function deleteSite(Site $site): void
$this->service->restart();
}
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, $version): void
{
$this->service->server->ssh()->exec(
$this->getScript('nginx/change-php-version.sh', [
view('ssh.services.webserver.nginx.change-php-version', [
'domain' => $site->domain,
'old_version' => $site->php_version,
'new_version' => $version,
'oldVersion' => $site->php_version,
'newVersion' => $version,
]),
'change-php-version',
$site->id
@ -137,19 +167,18 @@ public function setupSSL(Ssl $ssl): void
foreach ($ssl->getDomains() as $domain) {
$domains .= ' -d '.$domain;
}
$command = $this->getScript('nginx/create-letsencrypt-ssl.sh', [
$command = view('ssh.services.webserver.nginx.create-letsencrypt-ssl', [
'email' => $ssl->site->server->creator->email,
'domain' => $ssl->site->domain,
'domains' => $domains,
'web_directory' => $ssl->site->getWebDirectoryPath(),
]);
if ($ssl->type == 'custom') {
$command = $this->getScript('nginx/create-custom-ssl.sh', [
$command = view('ssh.services.webserver.nginx.create-custom-ssl', [
'path' => $ssl->getCertsDirectoryPath(),
'certificate' => $ssl->certificate,
'pk' => $ssl->pk,
'certificate_path' => $ssl->getCertificatePath(),
'pk_path' => $ssl->getPkPath(),
'certificatePath' => $ssl->getCertificatePath(),
'pkPath' => $ssl->getPkPath(),
]);
}
$result = $this->service->server->ssh()->setLog($ssl->log)->exec(
@ -160,8 +189,6 @@ public function setupSSL(Ssl $ssl): void
if (! $ssl->validateSetup($result)) {
throw new SSLCreationException;
}
$this->updateVHost($ssl->site);
}
/**
@ -175,55 +202,6 @@ public function removeSSL(Ssl $ssl): void
$ssl->site_id
);
$this->updateVHost($ssl->site, true);
$this->service->server->systemd()->restart('nginx');
}
protected function generateVhost(Site $site, bool $noSSL = false): 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);
}
$php_socket = 'unix:/var/run/php/php-fpm.sock';
if ($site->isIsolated()) {
$php_socket = "unix:/run/php/php{$site->php_version}-fpm-{$site->user}.sock";
}
$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);
$vhost = Str::replace('__php_socket__', $php_socket, $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);
}
return $vhost;
$this->updateVHost($ssl->site);
}
}

View File

@ -9,7 +9,7 @@ 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;

View File

@ -1,9 +0,0 @@
if ! sudo sed -i 's/php__old_version__/php__new_version__/g' /etc/nginx/sites-available/__domain__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "PHP Version Changed to __new_version__"

View File

@ -1,13 +0,0 @@
if ! sudo mkdir -p __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo "__certificate__" | sudo tee __certificate_path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo "__pk__" | sudo tee __pk_path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
echo "Successfully received certificate."

View File

@ -1,3 +0,0 @@
if ! sudo certbot certonly --force-renewal --nginx --noninteractive --agree-tos --cert-name __domain__ -m __email__ __domains__ --verbose; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,16 +0,0 @@
export DEBIAN_FRONTEND=noninteractive
if ! rm -rf __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi
if ! mkdir __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi
if ! chmod -R 755 __path__; then
echo 'VITO_SSH_ERROR'
exit 1
fi

View File

@ -1,15 +0,0 @@
if ! echo '' | sudo tee /etc/nginx/conf.d/__domain___redirects; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! echo '__vhost__' | sudo tee /etc/nginx/sites-available/__domain__; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo ln -s /etc/nginx/sites-available/__domain__ /etc/nginx/sites-enabled/; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,7 +0,0 @@
rm -rf __path__
sudo rm /etc/nginx/sites-available/__domain__
sudo rm /etc/nginx/sites-enabled/__domain__
echo "Site deleted"

View File

@ -1 +0,0 @@
cat /etc/nginx/sites-available/__domain__

View File

@ -1,8 +0,0 @@
sudo DEBIAN_FRONTEND=noninteractive apt-get install nginx -y
if ! echo '__config__' | sudo tee /etc/nginx/nginx.conf; then
echo 'VITO_SSH_ERROR' && exit 1
fi
sudo service nginx start
# install certbot
sudo DEBIAN_FRONTEND=noninteractive apt-get install certbot python3-certbot-nginx -y

View File

@ -1,85 +0,0 @@
user __user__;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

View File

@ -1,38 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass __php_socket__;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,34 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass __php_socket__;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,3 +0,0 @@
location __from__ {
return __mode__ __to__;
}

View File

@ -1,35 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:__port__/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,31 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:__port__/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,12 +0,0 @@
sudo service nginx stop
sudo DEBIAN_FRONTEND=noninteractive apt-get purge nginx nginx-common nginx-full -y
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/lib/nginx
sudo rm -rf /var/cache/nginx
sudo rm -rf /usr/share/nginx
sudo rm -rf /etc/systemd/system/nginx.service
sudo systemctl daemon-reload

View File

@ -1,7 +0,0 @@
if ! echo '__redirects__' | sudo tee /etc/nginx/conf.d/__domain___redirects; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo service nginx restart; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -1,3 +0,0 @@
echo '__vhost__' | sudo tee /etc/nginx/sites-available/__domain__
sudo service nginx restart

View File

@ -1,31 +0,0 @@
server {
listen 80;
listen 443 ssl;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
ssl_certificate __certificate__;
ssl_certificate_key __private_key__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.html;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.html;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}

View File

@ -1,27 +0,0 @@
server {
listen 80;
server_name __domain__ __aliases__;
root __path__/__web_directory__;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.html;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.html;
location ~ /\.(?!well-known).* {
deny all;
}
include conf.d/__domain___redirects;
}