zoukankan      html  css  js  c++  java
  • Let's Encrypt申请免费SSL证书

    1.https的作用
    安全,防止网站被劫持,数据被修改

    2.Let's Encrypt是什么
    Let's Encrypt是一个证书授权机构(CA),可以从Let's Encrypt获得网站域名的免费证书。

    3.Certbot是什么
    Certbot是Let's Encrypt官方推荐的获取证书的客户端,可以帮助我们获取免费的Let's Encrypt证书。

    4.获取免费证书
    1)安装Certbot客户端

    yum install certbot
    

    2)获取证书
    需要是绑定这台服务器的域名

    certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com
    

    这个命令会为example.com和www.example.com这两个域名生产一个证书。
    webroot:网站跟目录,命令执行后会在/var/www/example中创建.well-known文件夹,这个文件夹包含了一些验证文件,certbot会通过访问example.com/.well-known/acme-challenge来验证你的域名是否绑定的这个服务器。

    有些服务没有根目录如何处理呢?certbot提供了另一种模式--standalone,这种模式不需要指定根目录,它会自动启用服务器的443端口,来验证域名的归属。

    certbot certonly --standalone -d example.com -d www.example.com
    

    命令执行成功以后,可以在/etc/letsencrypt/live/目录下看到对应域名的文件夹

    5.Nginx配置启用HTTPS

    server {
            server_name example.com www.example.com;
            listen 443;
            ssl on;
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
            location / {
               proxy_pass http://127.0.0.1:3999;
               proxy_http_version 1.1;
               proxy_set_header X_FORWARDED_PROTO https;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header Host $host;
            }
        }
    

    listen 443:监听443端口,HTTPS占用443端口
    ssl on:启用SSL
    ssl_certificate:配置公钥证书路径
    ssl_certificate_key:配置私钥证书路径

    重启ngingx:nginx -s reload

    6.访问https网站
    在浏览器中输入https://example.com,可以看到网址前面带有“安全锁”标识

    7.自动更新SSL证书
    Let's Encrypt提供的证书只有90天的有效期,必须在证书到期之前重新获取这些证书。
    模拟证书renew

    certbot renew --dry-run
    

    关闭nginx,实际更新证书

    nginx -s stop
    certbot renew 
    

    也可以使用linux定时任务crontab来完成这个操作,新建一个定时任务文件certbot-auto-renew-cron,内容如下。

    15 2 * */2 * certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start"
    

    crontab启动定时任务

    crontab certbot-auto-renew-cron
    nginx -c /etc/nginx/nginx.conf
    

    每隔两个月的2:15执行更新操作。
    --pre-hook:执行命令前的操作,如果使用--standalone模式,会启用443端口,因为nginx已经占用了443端口,所以要先关闭
    --post-hook:执行命令后的操作,启动nginx

  • 相关阅读:
    【Leetcode】【Easy】Remove Duplicates from Sorted List
    【Leetcode】【Easy】Pascal's Triangle II
    【Leetcode】【Easy】Pascal's Triangle
    【Leetcode】【Easy】Binary Tree Level Order Traversal II
    【Leetcode】【Easy】Binary Tree Level Order Traversal
    【Leetcode】【Easy】Maximum Depth of Binary Tree
    【Leetcode】【Easy】Minimum Depth of Binary Tree
    【Leetcode】【Easy】Balanced Binary Tree
    【Leetcode】【Easy】Symmetric Tree
    如何使用Action.Invoke()触发一个Storyboard
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/8037770.html
Copyright © 2011-2022 走看看