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


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

一般来说,在手动执行此操作时,我首先执行以下操作:-

sudo apt-get install certbot python-certbot-nginx

通过执行上述操作,我将被要求继续还是不继续?[Y/n]。我将输入"Y"。然后我将执行以下命令

sudo certbot --nginx

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

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

完成所有这些步骤后,将启用https。现在我需要用shell文件编程完成这些事情。我已经想好使用这个安装certbot和python certbot nginx

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

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

附言:当我自己执行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

相关内容

最新更新