Shell 使用 certbot 和 nginx 制作站点 https 的脚本

Shell script to make site https using certbot and nginx

我知道如何使用命令行安装 ssl 证书。但是这次我需要一个 shell 脚本来完成这个任务。

通常在手动执行此操作时,我首先执行 fowllowing:-

sudo apt-get install certbot python-certbot-nginx

如果执行上面的命令,我会被询问是否继续?[Y/n]。我将输入'Y'。然后我将执行以下命令

sudo certbot --nginx

执行上面的命令会要求我按顺序回答以下步骤:

  1. 输入电子邮件地址(用于紧急续订和安全通知)(输入 'c' 取消)。
  2. 同意条款和服务[(A)gree/(C)ancel: A]
  3. 同意将开发新闻和活动分享到我的电子邮件地址。 [(Y)es/(N)o: N]
  4. 我想为哪些名字激活 HTTPS? [Select适当的数字用逗号隔开and/or空格,或者留下输入 空白 select 显示所有选项(输入 'c' 取消):空白]
  5. 从 http 重定向到 https [Select 适当的数字 [1-2] 然后 [enter](按 'c' 取消):2 ]

完成所有这些步骤后,https 将被启用。现在我需要通过 shell file.I 以编程方式完成这些事情已经想出安装 certbot 和 python-certbot-nginx 使用这个

sudo apt-get install certbot python-certbot-nginx -y

但我不知道如何继续,因为我从未使用过 shell 脚本

PS: 我在执行sudo certbot --nginx 并自己输入详细信息时提供示例结果。

sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): mygmailid@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: example.com
2: www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/demowebsite.conf
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/demowebsite.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/demowebsite.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/demowebsite.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://example.com and
https://www.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2020-01-28. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

虽然我来的很晚但是我终于做到了 正如@JohnHanley 在评论中所建议的那样。我浏览了文档,这是我通过命令行执行问题中上述步骤所需要的。

run            Obtain & install a certificate in your current webserver

-n             Run without ever asking for user input. This may
               require additional command line flags; the client will
               try to explain which ones are required if it finds one
               missing (default: False)

--nginx        Obtain and install certificates using Nginx (default:False)


-d             Domain names to apply. For multiple domains you can
               use multiple -d flags or enter a comma separated list
               of domains as a parameter. The first domain provided
               will be the subject CN of the certificate, and all
               domains will be Subject Alternative Names on the
               certificate. The first domain will also be used in
               some software user interfaces and as the file paths
               for the certificate and related material unless
               otherwise specified or you already have a certificate
               with the same name. In the case of a name collision it
               will append a number like 0001 to the file path name.


-m             Email used for registration and recovery contact. Use
               comma to register multiple emails, ex:
               u1@example.com,u2@example.com. (default: Ask).

--redirect     Automatically redirect all HTTP traffic to HTTPS for
               the newly authenticated vhost. (default: Ask)

--agree-tos    Agree to the ACME server's Subscriber Agreement

所以我执行的最终命令如下

certbot run -n --nginx --agree-tos -d example.com,www.example.com -m mygmailid@gmail.com --redirect