aspnetcore如何使用Let’s Encrypt支持https
这篇文章主要为大家展示了"aspnetcore如何使用Let's Encrypt支持https",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"aspnetcore如何使用Let's Encrypt支持https"这篇文章吧。
Let's Encrypt 是一个自动签发 https 证书的免费项目
Certbot 是 Let's Encrypt 官方推荐的证书生成客户端工具
基本操作
0 准备工作
将要签发证书的域名(如 you.domain.com) 解析到一台可在外网正常访问的服务器
1 登录服务器
my-pc$ ssh user@you.domain.com
2 安装 certbot
这里以 Ubuntu 16.04 为例
各个系统的安装方式不一样,可以在官网首页查阅
my-server$ cat /etc/issueUbuntu 16.04.2 LTS \n \lmy-server$ sudo apt-get updatemy-server$ sudo apt-get install software-properties-commonmy-server$ sudo add-apt-repository ppa:certbot/certbotmy-server$ sudo apt-get updatemy-server$ sudo apt-get install certbot my-server$ certbot --versioncertbot 0.19.0
3 检查端口占用
如果服务器上的 443 端口正在被占用,请先关闭对应的服务进程
否则可能个导致 certbot 运行出错
4 以命令交互方式开始制作证书
my-server$ certbot certonly --------> 开启命令Saving debug log to /var/log/letsencrypt/letsencrypt.logHow would you like to authenticate with the ACME CA?1: Spin up a temporary webserver (standalone)2: Place files in webroot directory (webroot)Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1 --------> 这里我们选择 1 standalone 模式Plugins selected: Authenticator standalone, Installer NoneEnter email address (used for urgent renewal and security notices) (Enter 'c' tocancel): you@gmail.com --------> 输入你的邮箱创建账号(只有第一次使用时会出现)Please read the Terms of Service athttps://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agreein order to register with the ACME server athttps://acme-v01.api.letsencrypt.org/directory(A)gree/(C)ancel: a --------> 选择 A 同意 (只有第一次使用时会出现)Would you be willing to share your email address with the Electronic FrontierFoundation, a founding partner of the Let's Encrypt project and the non-profitorganization that develops Certbot? We'd like to send you email about EFF andour work to encrypt the web, protect its users and defend digital rights.(Y)es/(N)o: y --------> 选择 Y 或 N (只有第一次使用时会出现)Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'to cancel): you.domain.com --------> 这里输入要制作证书的域名Obtaining a new certificatePerforming the following challenges:tls-sni-01 challenge for you.domain.comWaiting for verification...Cleaning up challengesIMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/you.domain.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/you.domain.com/privkey.pem Your cert will expire on 2018-01-26. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - 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-lemy-server$ ls /etc/letsencrypt/live/you.domain.com/ --------> 证书文件目录cert.pem chain.pem fullchain.pem privkey.pem README
看到 IMPORTANT NOTES
下方出现 Congratulations
就表明证书已经成功签发
可以进入 /etc/letsencrypt/live/you.domain.com/
目录查看相关生成的文件
进阶操作
使用 80 端口
默认 standalone 是使用 443 端口,也就是说要停止服务器现在占用 443 端口的进程
我们也可以将其改为使用 80 端口,同样道理,这时需要停止 80 端口的占用
my-server$ certbot certonly --standalone -n --agree-tos --email you@gmail.com --preferred-challenges http -d you.domain.com
-n
非交互式--email
指定账户--agree-tos
同意服务协议
使用 nginx 模式
安装 nginx 和插件
my-server$ apt-get install nginxmy-server$ apt-get install python-certbot-nginxmy-server$ certbot --nginx -d you.domain.com
使用 manual 模式
manual 模式其实和 webroot 类似,主要的区别就是 certbot 自动向 webroot 添加文件的过程,变为你自己手动完成
my-server$ certbot certonly --manual -d you.domain.comSaving debug log to /var/log/letsencrypt/letsencrypt.logObtaining a new certificatePerforming the following challenges:http-01 challenge for you.domain.comNOTE: The IP of this machine will be publicly logged as having requested thiscertificate. If you're running certbot in manual mode on a machine that is notyour server, please ensure you're okay with that.Are you OK with your IP being logged?(Y)es/(N)o: y --------> 选 YCreate a file containing just this data:_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BEAnd make it available on your web server at this URL:http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQPress Enter to Continue --------> 你需要保证 http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ 这个 url 返回的结果为 _Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BE (这个数据是随机的,每次都不一样) 当你做到后,按下回车键继续Waiting for verification...Cleaning up challengesIMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/you.domain.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/you.domain.com/privkey.pem Your cert will expire on 2018-01-27. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - 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
查看和管理当前服务器上的所有证书
my-server$ certbot certificates
使用 https 证书
以 nginx 为例
server { listen 80; listen 443 ssl; server_name you.domain.com; ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/you.domain.com/privkey.pem; # ...}
letsencrypt 有什么限制
标准限制
同一个顶级域名下的二级域名,一周做多申请 20 个
一个域名一周最多申请 5 次
1 小时最多允许失败 5 次
请求频率需要小于 20 次/s
一个 ip 3 小时内最多创建 10 个账户
一个账户最多同时存在 300 个 pending 的审核
测试模式限制
在 certbot 命令后加上 --staging
参数,限制更松一些,但不是正式证书
同一个顶级域名下的二级域名,一周做多申请 30000 个
一个域名一周最多申请 30000 次
1 小时最多允许失败 60 次
一个 ip 3小时内最多创建 50 个账户
转换为aspnetcore可用的证书
openssl pkcs12 -export -out cert.pfx -passout pass:你的密码 -inkey privkey.pem -in cert.pem -certfile fullchain.pem
在aspnetcore使用,修改 Program.cs 文件加载证书
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup() .UseKestrel(options => { options.AddServerHeader = false;#if !DEBUG options.ConfigureHttpsDefaults(co => { var cert = new X509Certificate2(File.ReadAllBytes(@"./cert.pfx"), "你的密码"); co.ServerCertificate = cert; });#endif }); })
以上是"aspnetcore如何使用Let's Encrypt支持https"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!