ubuntu 生成https证书 for let's encrypt

Posted 知识源于分享

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu 生成https证书 for let's encrypt相关的知识,希望对你有一定的参考价值。

1、安装certbot(用于自动续约证书)

# 添加安装源
sudo add-apt-repository ppa:certbot/certbot
# 更新apt安装源
sudo apt-get update
# 安装
sudo apt-get install python-certbot-apache

 

2、安装letsencrypt并生成证书

# 安装 letsencrypt
sudo apt-get install letsencrypt
# 生成证书
# letsencrypt certonly --agree-tos --email [email protected]163.com --webroot -w /var/html/www -d www.gabin.top -d cab.gabin.top
letsencrypt certonly --agree-tos --email [email protected]163.com -d www.gabin.top

 

3、检查/etc/letsencrypt/live/www.gabin.top

目录,已经生成证书文件

  • cert.pem: 你不用关心 (这个实际上是服务器证书文件)
  • chain.pem: 你不用关心 (这个实际上是… 自己看文档吧, 我没读懂. 貌似是个递归查找用的链式证书)
  • fullchain.pem: cert.pem + chain.pem 的合体. 需要配置到 nginx 配置文件中的 ssl_certificate . 
  • privkey.pem: 私钥. 需要配置到 nginx 配置文件中的 ssl_certificate_key .

4、配置nginx服务器

1)修改默认服务配置

server {
    listen 80;
    # listen [::]:80;
    server_name www.gabin.top; 
  # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. 
  return 301 https://$host$request_uri; 
}

 

2)增加一个ssl的网站配置 

vim /etc/nginx/sites-available/default-ssl

如下内容:

server {
    listen 443 ssl http2;
    # listen [::]:443 ssl http2;
    server_name www.gabin.top;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate /etc/letsencrypt/live/www.gabin.top/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.gabin.top/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    # ssl_dhparam /etc/ssl/certs/dhparam.pem;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS;
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

    ## verify chain of trust of OCSP response using Root CA and Intermediate certs
    # ssl_trusted_certificate /etc/letsencrypt/live/www.gabin.top/root_ca_cert_plus_intermediates;

    # resolver 8.8.8.8 8.8.4.4 valid=300s;
    # resolver_timeout 5s;
}

3)建立软连接并重启nginx

# 建立软链接配置网站
sudo ln -s/etc/nginx/sites-available/default-ssl /etc/nginx/sites-sites-enabled
# 重启nginx服务器
sudo service nginx restart

 

5、定时续约证书

# 增加定时器任务
crontab -e

编辑增加任务

# 每天夜里凌晨 2 点续签:
* 2 * * * letsencrypt renew

# 重启 nginx 以使证书生效
* 3 * * * nginx -s reload

 

以上是关于ubuntu 生成https证书 for let's encrypt的主要内容,如果未能解决你的问题,请参考以下文章

在Ubuntu使用 Let's Encrypt 证书部署 HTTPS的方法

Windows Ubuntu Bash申请免费通配符证书(Let's Encrypt)并绑定IIS

生成 Let's Encrypt 免费https证书

生成线上用https证书,支持通配符和多域名,初学Let’s Encrypt用于IIS,纯本地手动

centos+nginx申请Let's Encrypt 通配符HTTPS证书

使用 Docker 生成 Let’s Encrypt 证书