使用certbot在nginx搭建HTTPS

certbot

certbot官⽹
apache配置文档

安装证书自动工具 certbot

yum install -y epel-release
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhuiREGION-rhel-server-optional
sudo yum install certbot
# sudo certbot certonly

如果执行certbot报错

##  pkg_resources.DistributionNotFound: The 'urllib3<1.23,>=1.21.1' distribution was not found and is required by requests
rm /usr/lib/python2.7/site-packages/urllib3* -rf
python2.7 -m pip install urllib3
##  ImportError: 'pyOpenSSL' module missing required functionality.
Try upgrading to v0.14 or newer.     pip show pyOpenSSL yum remove certbot pyOpenSSL
pip uninstall pyOpenSSL
yum install -y python-devel
yum install -y openssl-devel
pip install certbot certbot certificates

配置Nginx

vi /usr/local/nginx/conf/nginx.conf
[In server{}]       location ^~/.well-known/acme-challenge/ {  default_type "text/plain";root /data/www;}
service nginx reload

申请证书

Web+FS服务器器 192.168.1.152

certbot certonly --webroot \-w /data/www \-d <域名> \-d <域名> \-d <域名> 
[root@izuf6b281zcjzt94z7ikdlz nginx]# certbot certonly --webroot -w /data/www -d <域名> -d <域名> -d <域名> -d <域名> -d <域名>
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You have an existing certificate that contains a portion of the domains you
requested (ref: /etc/letsencrypt/renewal/<域名>)It contains these names: <域名>,
<域名>, <域名>You requested these names for the new certificate: <域名>,
<域名>, <域名>,
<域名>, <域名>.Do you want to expand and replace this existing certificate with the new
certificate?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(E)xpand/(C)ancel: E
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for <域名>
http-01 challenge for <域名>
Using the webroot path /data/www for all unmatched domains.
Waiting for verification...
Cleaning up challengesIMPORTANT NOTES:- Congratulations! Your certificate and chain have been saved at:/etc/letsencrypt/live/<域名>/fullchain.pemYour key file has been saved at:/etc/letsencrypt/live/<域名>/privkey.pemYour cert will expire on 2019-12-27. To obtain a new or tweakedversion of this certificate in the future, simply run certbotagain. 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/donateDonating to EFF:                    https://eff.org/donate-le

证书自动更新

certbot renew

该命令尝试续订在30天内到期的所有先前获得的证书。除非您指定其他插件或选项,否则将使用与最初颁发证书时使用的插件和选项相同的插件和选项。不同于certonly,它renew作用于多个证书,并始终考虑每个证书是否即将到期。因此,renew适合(并设计为)自动使用,以允许系统在适当时自动续订每个证书。由于renew仅更新即将到期的证书,因此可以根据需要频繁运行它-因为通常不会采取任何措施。

该renew命令包括在证书更新之前或之后运行命令或脚本的挂钩。例如,如果您具有使用独立插件获得的单个证书,则可能需要在更新之前停止Web服务器,以便独立服务器可以绑定到必要的端口,然后在插件完成后重新启动它。例:

certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start"

如果钩子以非零退出代码退出,则错误将被打印到,stderr但无论如何都会尝试进行更新。失败的挂钩不会直接导致Certbot以非零的退出代码退出,但是由于续订失败时Certbot会以非零的退出代码退出,因此导致续约失败的失败的挂钩将间接导致退出代码为非零。挂钩仅在证书需要更新时才运行,因此您可以频繁运行上述命令而不必停止网络服务器。

当Certbot检测证书到期更换,–pre-hook 和–post-hook挂钩之前运行,每次尝试后续订。如果您希望挂钩仅在成功续订后才能运行,请–deploy-hook在这样的命令中使用 。

certbot renew --deploy-hook /path/to/deploy-hook-script

/path/to/deploy-hook-script

#!/bin/shset -efor domain in $RENEWED_DOMAINS; docase $domain inexample.com)daemon_cert_root=/etc/some-daemon/certs# Make sure the certificate and private key files are# never world readable, even just for an instant while# we're copying them into daemon_cert_root.umask 077cp "$RENEWED_LINEAGE/fullchain.pem" "$daemon_cert_root/$domain.cert"cp "$RENEWED_LINEAGE/privkey.pem" "$daemon_cert_root/$domain.key"# Apply the proper file ownership and permissions for# the daemon to read its certificate and key.chown some-daemon "$daemon_cert_root/$domain.cert" \"$daemon_cert_root/$domain.key"chmod 400 "$daemon_cert_root/$domain.cert" \"$daemon_cert_root/$domain.key"service some-daemon restart >/dev/null;;esac
done
certbot renew --dry-run # 添加linux定时器
vi /etc/crontab# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
32 3 * * * root /usr/bin/certbot renew 1>>/data/logs/crontab/certbot-auto.log 2>&1
35 3 * * 1 root /usr/sbin/service nginx reload 1>>/data/logs/crontab/nginx-reload.log 2>&1# 或者自己编写load Nginx脚本
# 35 3 * * 1 root /usr/local/nginx/loadNginx.sh 1>>/data/logs/crontab/nginx-reload.log 2>&1echo "exec ./sbin/nginx -s reload" > loadNginx.sh
chmod +x /usr/local/nginx/loadNginx.sh

配置nginx https

http{    ...   ssl_certificate     /etc/letsencrypt/live/<域名>/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/<域名>/privkey.pem;   ssl_trusted_certificate /etc/letsencrypt/live/<域名>/chain.pem;   ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;   ssl_ciphers         HIGH:!aNULL:!MD5;   ssl_session_cache   shared:SSL:10m;   ssl_session_timeout 10m;    server {         ...         listen       443 ssl;         ...    }
}

查看证书

certbot certificatesSaving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:Certificate Name: <域名>Domains: <域名> <域名> <域名> <域名> <域名>Expiry Date: 2019-12-27 12:29:17+00:00 (VALID: 89 days)Certificate Path: /etc/letsencrypt/live/<域名>/fullchain.pemPrivate Key Path: /etc/letsencrypt/live/<域名>/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

附录1:Nginx补安装https模块

cd /data/install-pkg/nginx-1.8.0 ./configure --user=www --prefix=/usr/local/nginx --withhttp_ssl_module --error-log-path=/data/logs/nginx/error.log --httplog-path=/data/logs/nginx/access.log
make
cd /usr/local/nginx/sbin
cp nginx nginx_bak
service nginx stop
ps -ef | grep nginx  (如果有,杀进程)
cp /data/install-pkg/nginx-1.8.0/objs/nginx ./
service nginx start

附录2:参考nginx.conf

user  www www;
worker_processes  2;
worker_cpu_affinity 1010 0101;
error_log  /data/logs/nginx_error.log crit;
pid        /var/run/nginx.pid;
events {   use epoll;   worker_connections  65535;
}http {   include       mime.types;   default_type  application/octet-stream;   charset  utf8;   #access_log  logs/access.log  main;   #定义访问⽇日志的写⼊入格式   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for $host $upstream_response_time $request_time';   access_log  /data/logs/nginx_access.log  access;   #设定请求缓冲server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 300m;sendfile on;tcp_nopush     on;keepalive_timeout 60;tcp_nodelay on;client_body_buffer_size 512k;####下⾯面这段要添加上去的proxy_connect_timeout 5;proxy_read_timeout    60;proxy_send_timeout    5;proxy_buffer_size     16k;proxy_buffers         4 1024k;proxy_busy_buffers_size 1024k;proxy_temp_file_write_size 1024k;server_tokens off;#对⽹网⻚页⽂文件、CSS、JS、XML等启动gzip压缩,减少数据传输量量,提⾼高访问速度gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types       text/plain application/x-javascript text/css application/xml;gzip_vary on;# HTTPS配置ssl_certificate /etc/letsencrypt/live/<域名>/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/<域名>/privkey.pem;ssl_trusted_certificate /etc/letsencrypt/live/<域名>/chain.pem;ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers         HIGH:!aNULL:!MD5;ssl_session_cache   shared:SSL:10m;ssl_session_timeout 10m;server {listen       80;listen       443 ssl;server_name  <域名>;index index.html index.htm index.jsp index.do default.do default.jsp;root /data/www/front;access_log  /data/logs/nginx_access.log  access;#所有jsp的⻚页⾯面均交由tomcat处理理location / {proxy_set_header Host "<域名>";proxy_set_header Real-Host $host;proxy_set_header X-Forward-For $remote_addr;proxy_pass http://10.144.82.104:8080;}location ^~ /res/ {rewrite ^(.*)\;jsessionid=(.*)$ $1 break;root /data/www/front/ROOT;}location ^~/.well-known/acme-challenge/ {default_type "text/plain";root /data/www;}location = /favicon.ico {root /data/www;}}server {listen       80;server_name  <域名>;index index.html index.htm index.jsp index.do default.do default.jsp;root /data/www/fs;if ( -d $request_filename ) {rewrite ^/(.*)([^/])$  http://$host/$1$2/ permanent;}location / {expires      30d;}location ~ .*\.(js|css)?$ {expires      1h;}}

遇到的问题

  1. 阿里云上配置的,需要防火墙开放443端口,或者安全组配置443端口开放
  2. 如果公网IP是阿里云上的负载均衡公网IP,则需要开放443端口监听(优化:监听端口80改为HTTP协议,监听转发到443端口)
  3. **配置HTTPS协议443端口监听 -> 选择“新建证书” -> “上传第三方证书” -> 将服务器的certbot公钥和秘钥上传 **

使用certbot在nginx搭建HTTPS 以及 阿里云负载均衡HTTPS搭建相关推荐

  1. 新功能:阿里云负载均衡SLB支持HTTPS虚拟主机功能(SNI)

    Greeting 大家好,很高兴告诉大家,阿里云负载均衡SLB支持HTTPS虚拟主机(单VIP多证书功能-SNI),目前已经在所有地域开放,欢迎大家使用.具体内容请大家观看视频. 温馨提醒:视频杀流量 ...

  2. 新功能:阿里云负载均衡SLB支持HTTP访问强制跳转HTTPS

    摘要: 很高兴的告诉大家,阿里云负载均衡SLB已经在澳大利亚(悉尼).日本(东京).阿联酋(迪拜).美国 (弗吉尼亚).美国(硅谷).马来西亚(吉隆坡).德国(法兰克福).新加坡.印度尼西亚(雅加达) ...

  3. 新功能:阿里云负载均衡SLB支持HTTP/HTTPS超时时间自定义功能

    2019独角兽企业重金招聘Python工程师标准>>> 摘要: 大家好,很高兴的告诉大家,阿里云负载均衡SLB已经在新加坡.澳大利亚(悉尼).马来西亚(吉隆坡).日本(东京).美国( ...

  4. 阿里云 负载均衡 HTTP转HTTPS

    一.相关文档 1.证书服务 2.简单路由-HTTP 协议变为 HTTPS 协议 二.阿里云操作界面 1.云盾证书服务管理控制台(查询CA证书服务) 2.负载均衡管理控制台 三.相关文档 1.Syman ...

  5. 阿里云负载均衡(SLB)简介

    阿里云负载均衡(SLB)简介 1.SLB的概念 SLB:是将访问流量根据转发策略分发到后端多台云服务器(ECS实例)的流量分发控制. 2.概述 1.负载均衡通过设置虚拟服务地址,将添加的同一地域的多台 ...

  6. 阿里云负载均衡 SLB CLB 虚拟服务器组配置

    虚拟服务器组优势 当您需要将不同的请求转发到不同的后端服务器上时,或需要通过域名和URL进行请求转发时,可以选择使用虚拟服务器组. 虚拟服务器组配置中需要注意的问题 如果一旦配置转发策略,访问非策略的 ...

  7. 【云周刊】第175期:终于来了!重磅发布:阿里云负载均衡SLB率先支持IPv6!

    本期头条 终于来了!重磅发布:阿里云负载均衡SLB率先支持IPv6! IPv6的设计初衷是用以解决IPv4地址枯竭问题,同时对IPv4进行大量改进,并最终取代IPv4.然而由于NAT等技术的广泛应用, ...

  8. 一篇文章读懂阿里云负载均衡性能保障型实例

    1. 什么是负载均衡性能保障型实例? 2. 性能保障型实例如何收费? 3. 性能保障型实例规格费的定价 4. 如何选择性能保障型实例? 5. 性能保障型实例的变配操作限制 6. 性能保障型实例何时收费 ...

  9. 新功能:阿里云负载均衡支持HTTP/2、WSS协议

    摘要: 很高兴的告诉大家,阿里云负载均衡在欧洲中部(法兰克福)与亚太东南3(吉隆坡)地域支持HTTP/2.WSS(Web Socket Secure)协议. 1.HTTP/2协议支持 什么是HTTP/ ...

最新文章

  1. SAP-注入“AI基因” 打造全球第一款“智能ERP
  2. IDC:全球大数据和业务分析收入预计到2019年突破1870亿美元
  3. 在.NET环境下将报表导出Excel和Word
  4. 十条不错的编程观点(转载)
  5. Leetcode 92 反转链表 II (每日一题 20210726)
  6. Java学习资料-SimpleFactory
  7. [MyBatisPlus]入门案例
  8. REVERSE-PRACTICE-BUUCTF-5
  9. Java环境创建_Java环境的搭建
  10. 关于SRAM,DRAM,SDRAM,以及NORFLASH,NANDFLASH
  11. 社交网络图形可视化工具Gephi使用教程
  12. 如何查看opencv版本
  13. 【Love2d从青铜到王者】第八篇:Love2d之多个文件和作用域范围
  14. unity动态加载FBX模型
  15. row_number() over (partition by....order by...)用法
  16. 获取城市a-z jso列表
  17. 软件公司 sun公司 Oracle公司
  18. 用于情绪测试的软件,第七章 情感心理自我测试心理测评软件
  19. 【理论】编译原理导论
  20. 百度竞价排名SEM介绍

热门文章

  1. 怎样清理苹果手机内存空间_你还不知道?苹果手机这样清理垃圾,轻松腾出10G内存!...
  2. 中文数字与阿拉伯数字转换(Python)
  3. 过冲(overshoot)、下冲(Undershoot)的量化标准与评估实例
  4. Java并发编程学习-日记1、常见的IO模型、NIO、OIO
  5. Nginx学习笔记(反向代理搭建集群)
  6. PS平面设计零基础如何学好PS平面设…
  7. 数据挖掘:概念与技术 第五章-数据立方体技术
  8. Android Studio汉化(中文支持)
  9. linux查看进程临时日志,Linux sed查看某时间段的系统日志
  10. AFEPack 使用 Tutorial(二):解带系数二维泊松方程