-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnginx_domain_ssl.sh
109 lines (88 loc) · 3.3 KB
/
nginx_domain_ssl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
###########################################
# Created by Dirty Harry for YiiMP use... #
###########################################
source /etc/functions.sh
source /etc/yiimpserver.conf
source $STORAGE_ROOT/yiimp/.yiimp.conf
set -eu -o pipefail
function print_error {
read line file <<<$(caller)
echo "An error occurred in line $line of file $file:" >&2
sed "${line}q;d" "$file" >&2
}
trap print_error ERR
if [[ ("$wireguard" == "true") ]]; then
source $STORAGE_ROOT/yiimp/.wireguard.conf
fi
echo -e " Generating Certbot Request for ${DomainName} ...$COL_RESET"
sudo mkdir -p /var/www/_letsencrypt
sudo chown www-data /var/www/_letsencrypt
hide_output sudo certbot certonly --webroot -d "${DomainName}" --register-unsafely-without-email -w /var/www/_letsencrypt -n --agree-tos --force-renewal
# Check to make sure certbot installed ok, if not keep the self generated ssl config.
if sudo [ -f /etc/letsencrypt/live/"$DomainName"/fullchain.pem ]; then
# Configure Certbot to reload NGINX after success renew:
sudo mkdir -p /etc/letsencrypt/renewal-hooks/post/
echo '#!/bin/bash\nnginx -t && systemctl reload nginx' | sudo -E tee /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh >/dev/null 2>&1
sudo chmod a+x /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
# Remove the '"${DomainName}"'.conf that had the self signed SSL and replace with the new file.
sudo rm /etc/nginx/sites-available/${DomainName}.conf
# I am SSL Man!
echo '###########################################
# Source Generated by nginxconfig.io
# Updated by Dirty Harry for YiiMP use... #
###########################################
# NGINX Simple DDoS Defense
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
limit_conn conn_limit_per_ip 80;
limit_req zone=req_limit_per_ip burst=80 nodelay;
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:40m rate=5r/s;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name '"${DomainName}"';
set $base "/var/www/'"${DomainName}"'/html";
root $base/web;
# SSL
ssl_certificate /etc/letsencrypt/live/'"${DomainName}"'/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/'"${DomainName}"'/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/'"${DomainName}"'/chain.pem;
# security
include yiimp.server/security.conf;
# logging
access_log '"${STORAGE_ROOT}"'/yiimp/site/log/'"${DomainName}"'.app.access.log;
error_log '"${STORAGE_ROOT}"'/yiimp/site/log/'"${DomainName}"'.app.error.log warn;
# index.php
index index.php;
# index.php fallback
location / {
try_files $uri $uri/ /index.php?$args;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?r=$1;
}
# handle .php
location ~ \.php$ {
include yiimp.server/php_fastcgi.conf;
}
# additional config
include yiimp.server/general.conf;
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name .'"${DomainName}"';
include yiimp.server/letsencrypt.conf;
location / {
return 301 https://'"${DomainName}"'$request_uri;
}
}
' | sudo -E tee /etc/nginx/sites-available/${DomainName}.conf >/dev/null 2>&1
restart_service nginx >/dev/null 2>&1
restart_service php7.3-fpm >/dev/null 2>&1
else
echo -e "Certbot generation failed, after the installer is finished check /var/log/letsencrypt (must be root to view) on why it failed."
fi
set +eu +o pipefail
cd $HOME/yiimpserver/yiimp_single