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

Ama*_*ney 4 shell lets-encrypt certbot

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

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

sudo apt-get install certbot python-certbot-nginx
Run Code Online (Sandbox Code Playgroud)

通过执行上述我将被要求继续或不?[是/否]。我将输入'Y'。然后我将执行以下命令

sudo certbot --nginx
Run Code Online (Sandbox Code Playgroud)

执行上述将要求我按顺序回答以下步骤:

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

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

sudo apt-get install certbot python-certbot-nginx -y
Run Code Online (Sandbox Code Playgroud)

但我不知道如何进一步进行,因为我从未使用过 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
Run Code Online (Sandbox Code Playgroud)

Ama*_*ney 8

虽然我来晚了,但最后我还是按照@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
Run Code Online (Sandbox Code Playgroud)

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

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