forked from rehmatworks/serverpilot-letsencrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sple.sh
178 lines (149 loc) · 5.56 KB
/
sple.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
#################################################
# #
# This script automates the installation #
# of Let's Encrypt SSL certificates on #
# your ServerPilot free plan #
# #
#################################################
theAction=$1
domainName=$2
appName=$3
spAppRoot="/srv/users/serverpilot/apps/$appName"
domainType=$4
spSSLDir="/etc/nginx-sp/vhosts.d/"
# Install Let's Encrypt libraries if not found
if ! hash letsencrypt 2>/dev/null; then
lecheck=$(eval "apt-cache show letsencrypt 2>&1")
if [[ "$lecheck" == *"No"* ]]
then
sudo wget --no-check-certificate https://dl.eff.org/certbot-auto &>/dev/null
sudo chmod a+x certbot-auto &>/dev/null
sudo mv certbot-auto /usr/local/bin/letsencrypt &>/dev/null
else
sudo apt-get install -y letsencrypt &>/dev/null
fi
fi
if [ -z "$theAction" ]
then
echo -e "\e[31mPlease specify the task. Should be either install or uninstall\e[39m"
exit
fi
if [ -z "$domainName" ]
then
echo -e "\e[31mPlease provide the domain name\e[39m"
exit
fi
if [ ! -d "$spAppRoot" ]
then
echo -e "\e[31mThe app name seems invalid as we didn't find its directory on your server\e[39m"
exit
fi
if [ -z "$appName" ]
then
echo -e "\e[31mPlease provide the app name\e[39m"
exit
fi
if [ "$theAction" == "uninstall" ]; then
sudo rm "$spSSLDir$appName-ssl.conf" &>/dev/null
sudo service nginx-sp reload
echo -e "\e[31mSSL has been removed. If you are seeing errors on your site, then please fix HTACCESS file and remove the rules that you added to force SSL\e[39m"
elif [ "$theAction" == "install" ]; then
if [ -z "$domainType" ]
then
echo -e "\e[31mPlease provide the type of the domain (either main or sub)\e[39m"
exit
fi
sudo service nginx-sp stop
echo -e "\e[32mChecks passed, press enter to continue\e[39m"
if [ "$domainType" == "main" ]; then
thecommand="letsencrypt certonly --register-unsafely-without-email --agree-tos -d $domainName -d www.$domainName"
elif [[ "$domainType" == "sub" ]]; then
thecommand="letsencrypt certonly --register-unsafely-without-email --agree-tos -d $domainName"
else
echo -e "\e[31mDomain type not provided. Should be either main or sub\e[39m"
exit
fi
output=$(eval $thecommand 2>&1) | xargs
if [[ "$output" == *"too many requests"* ]]; then
echo "Let's Encrypt SSL limit reached. Please wait for a few days before obtaining more SSLs for $domainName"
elif [[ "$output" == *"Congratulations"* ]]; then
if [ "$domainType" == "main" ]; then
sudo echo "server {
listen 443 ssl;
listen [::]:443 ssl;
server_name
$domainName
www.$domainName
;
ssl on;
ssl_certificate /etc/letsencrypt/live/$domainName/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$domainName/privkey.pem;
root $spAppRoot/public;
access_log /srv/users/serverpilot/log/$appName/dev_nginx.access.log main;
error_log /srv/users/serverpilot/log/$appName/dev_nginx.error.log;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-SSL on;
proxy_set_header X-Forwarded-Proto \$scheme;
include /etc/nginx-sp/vhosts.d/$appName.d/*.nonssl_conf;
include /etc/nginx-sp/vhosts.d/$appName.d/*.conf;
}" > "$spSSLDir$appName-ssl.conf"
elif [ "$domainType" == "sub" ]; then
sudo echo "server {
listen 443 ssl;
listen [::]:443 ssl;
server_name
$domainName
;
ssl on;
ssl_certificate /etc/letsencrypt/live/$domainName/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$domainName/privkey.pem;
root $spAppRoot/public;
access_log /srv/users/serverpilot/log/$appName/dev_nginx.access.log main;
error_log /srv/users/serverpilot/log/$appName/dev_nginx.error.log;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-SSL on;
proxy_set_header X-Forwarded-Proto \$scheme;
include /etc/nginx-sp/vhosts.d/$appName.d/*.nonssl_conf;
include /etc/nginx-sp/vhosts.d/$appName.d/*.conf;
}" > "$spSSLDir$appName-ssl.conf"
fi
echo -e "\e[32mSSL should have been installed for $domainName with auto-renewal (via cron)\e[39m"
elif [[ "$output" == *"Failed authorization procedure."* ]]; then
echo -e "\e[31m$domainName isn't being resolved to this server. Please check and update the DNS settings if necessary and try again when domain name points to this server\e[39m"
elif [[ ! $output ]]; then
# If no output, we will assume that a valid SSL already exists for this domain
# so we will just add the vhost
sudo echo "server {
listen 443 ssl;
listen [::]:443 ssl;
server_name
$domainName
www.$domainName
;
ssl on;
ssl_certificate /etc/letsencrypt/live/$domainName/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$domainName/privkey.pem;
root $spAppRoot/public;
access_log /srv/users/serverpilot/log/$appName/dev_nginx.access.log main;
error_log /srv/users/serverpilot/log/$appName/dev_nginx.error.log;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-SSL on;
proxy_set_header X-Forwarded-Proto \$scheme;
include /etc/nginx-sp/vhosts.d/$appName.d/*.nonssl_conf;
include /etc/nginx-sp/vhosts.d/$appName.d/*.conf;
}" > "$spSSLDir$appName-ssl.conf"
echo -e "\e[32mSSL should have been installed for $domainName with auto-renewal (via cron)\e[39m"
else
echo -e "\e[31mSomething unexpected occurred\e[39m"
fi
sudo service nginx-sp start && sudo service nginx-sp reload
else
echo -e "\e[31mTask cannot be identified. It should be either install or uninstall \e[39m"
fi