Browse Source

update functions autocertbot nginx 30.06.22:03.01

root 2 years ago
parent
commit
466dd52147
1 changed files with 105 additions and 5 deletions
  1. 105 5
      certbot4nginx/auto4certbot.sh

+ 105 - 5
certbot4nginx/auto4certbot.sh

@@ -1,20 +1,38 @@
1 1
 #!/bin/bash
2 2
 #
3
+# author: Koshuba V.O.
4
+# license: GPL 2.0
5
+# create 2022
6
+#
7
+version="0.2.3";
8
+sname="autocertbot";
3 9
 # необходимы для работы: nginx,certbot
4 10
 # create new cert
5 11
 path_ssl="/etc/ssl";
6 12
 path_cert="/etc/letsencrypt/live";
7 13
 source "/etc/scripts/certbot4nginx/auto4certbot.conf";
14
+## - nginx
15
+nginx_enable="/etc/nginx/sites-enabled";
16
+nginx_available="/etc/nginx/sites-available";
17
+##
18
+www_root="/tmp/letsencrypt";
19
+##
20
+path_tmp="/tmp/certbot";
21
+##
8 22
 log="/var/log/syslog";
9 23
 #
10 24
 cmd=$1;
25
+#-list enable sites
26
+scan_list=();
11 27
 #
12 28
 
13 29
 function createCert() {
14 30
 for ((dmn=0; dmn != ${#domains[@]}; dmn++))
15 31
     do
16 32
 eval local dreg="(" $(echo -e ${domains[$dmn]}) ")";
17
-certbot certonly --webroot -w $webcrt -d ${dreg[0]}
33
+certbot --update-registration -m "${dreg[1]}";
34
+## example manual: certbot certonly --webroot --webroot-path /tmp/letsencrypt -d mydomen.ru
35
+certbot certonly --webroot --webroot-path $www_root -d ${dreg[0]}
18 36
 done
19 37
 }
20 38
 
@@ -74,14 +92,89 @@ for ((dmn=0; dmn != ${#domains[@]}; dmn++))
74 92
 done
75 93
 if [ $valtrue != 0 ];
76 94
    then
77
-     :>/etc/ssl/crt-list.txt
95
+     echo >/etc/ssl/crt-list.txt
78 96
         for ((icrt=0; icrt != ${#domains[@]}; icrt++))
79 97
          do
80
-          echo "$path_ssl/${domains[$icrt]}.pem">>/etc/ssl/crt-list.txt
98
+           eval local dcrt="(" $(echo -e ${domains[$icrt]}) ")";
99
+          echo "$path_ssl/private/${dcrt[0]}.pem">>/etc/ssl/crt-list.txt
81 100
         done
82 101
 fi
83 102
 }
84 103
 
104
+function downSite(){
105
+sudo systemctl stop nginx.service;
106
+
107
+eval list_www="(" $(find $nginx_enable/* -maxdepth 0 -type l -printf '%f\n') ")";
108
+for ((dwx=0; dwx != ${#list_www[@]}; dwx++))
109
+    do
110
+      rm $nginx_enable/${list_www[dwx]};
111
+done
112
+
113
+for ((dnm=0; dnm != ${#domains[@]}; dnm++))
114
+    do
115
+eval local dcert="(" $(echo -e ${domains[$dnm]}) ")";
116
+    sitename="${dcert[0]}";
117
+    siteport="${dcert[2]}";
118
+    createConf;
119
+done
120
+sudo systemctl start nginx.service;
121
+}
122
+
123
+function upSite(){
124
+sudo systemctl stop nginx.service;
125
+eval cert_bot="(" $(find $nginx_enable/* -maxdepth 0 -type l -printf '%f\n') ")";
126
+for ((cr=0; cr != ${#cert_bot[@]}; cr++))
127
+    do
128
+      rm $nginx_enable/${cert_bot[cr]};
129
+done
130
+
131
+for ((dwx=0; dwx != ${#list_www[@]}; dwx++))
132
+    do
133
+     ln -s $nginx_available/${list_www[dwx]} $nginx_enable/${list_www[dwx]};
134
+done
135
+sudo systemctl start nginx.service;
136
+}
137
+
138
+
139
+function createConf(){
140
+if [ ! -d $path_tmp ];
141
+  then
142
+    mkdir -p $path_tmp;
143
+fi
144
+
145
+if [ ! -d $www_root ];
146
+  then
147
+    mkdir -p $www_root/.well-known/acme-challenge;
148
+chown -R www-data:www-data $www_root;
149
+fi
150
+    echo >$path_tmp/$sitename.conf;
151
+    echo -e 'server { listen      0.0.0.0:'"$siteport"';' >>$path_tmp/$sitename.conf;
152
+    echo -e '\n' >>$path_tmp/$sitename.conf;
153
+    echo -e 'server_name '"$sitename"';' >>$path_tmp/$sitename.conf;
154
+    echo -e '\n' >>$path_tmp/$sitename.conf;
155
+    #echo -e 'if ($http_host != $server_name){' >> $path_tmp/$sitename.conf;
156
+    #echo -e '    rewrite ^(.*)$ http://$server_name$1 redirect;'>>$path_tmp/$sitename.conf;
157
+    #echo -e '}' >>$path_tmp/$sitename.conf;
158
+    #echo -e '\n' >>$path_tmp/$sitename.conf;
159
+    echo -e 'location /.well-known/acme-challenge {' >>$path_tmp/$sitename.conf;
160
+    echo -e '    allow all;' >>$path_tmp/$sitename.conf;
161
+    echo -e '    autoindex off;' >>$path_tmp/$sitename.conf;
162
+    echo -e '    default_type "text/plain";' >>$path_tmp/$sitename.conf;
163
+    echo -e '    root '"$www_root"';' >>$path_tmp/$sitename.conf;
164
+    echo -e '}' >>$path_tmp/$sitename.conf;
165
+    echo -e '\n' >>$path_tmp/$sitename.conf;
166
+    echo -e 'location = /.well-known {' >>$path_tmp/$sitename.conf;
167
+    echo -e '    return 404;' >>$path_tmp/$sitename.conf;
168
+    echo -e '}' >>$path_tmp/$sitename.conf;
169
+    echo -e '\n' >>$path_tmp/$sitename.conf;
170
+    echo -e 'error_page 404 /404.html;' >>$path_tmp/$sitename.conf;
171
+    echo -e 'error_page 500 502 503 504 /50x.html;' >>$path_tmp/$sitename.conf;
172
+    echo -e '\n' >>$path_tmp/$sitename.conf;
173
+    echo -e 'error_log /var/log/nginx/err-certbot.log;' >>$path_tmp/$sitename.conf;
174
+    echo -e 'access_log /var/log/nginx/access-certbot.log;' >>$path_tmp/$sitename.conf;
175
+    echo -e '}' >>$path_tmp/$sitename.conf;
176
+ln -s $path_tmp/$sitename.conf $nginx_enable/$sitename.conf
177
+}
85 178
 
86 179
 
87 180
 
@@ -89,12 +182,18 @@ case "$cmd" in
89 182
 
90 183
 ## create cert
91 184
 "--create" | "--create" )
185
+downSite;
92 186
 createCert;
187
+upSite;
188
+toSSL;
93 189
 ;;
94 190
 
95 191
 ## update cert
96 192
 "--update" | "--update" )
193
+downSite;
97 194
 renew;
195
+upSite;
196
+toSSL;
98 197
 ;;
99 198
 
100 199
 ## update cert force
@@ -109,6 +208,7 @@ echo "please input pameters: auto4certbot.sh --create | --update | --flist";
109 208
 echo "auto4certbot.sh --create; create new certificate"
110 209
 echo "auto4certbot.sh --update; update certificates;"
111 210
 echo "auto4certbot.sh --flist; update certificates from ssl;"
112
-
113 211
 ;;
114
-esac
212
+esac
213
+
214
+exit