用Let‘s Encrypt给网站添加一个免费的SSL证书

概要

最近在折腾自己的网站时,每次访问浏览器,左上角都有一个“不安全”提示,看起来十分不爽。于是就查了一下有没有比较好的解决办法,发现有一款很好用的开源、免费的SSL认证的工具Let's Encrypt,可以提供免费的SSL证书,配置完发现确实比较好用(实际上,如果网站未安装SSL证书,从某些安全性要求比较高的网络访问时,可能都打不开),就记录一下,给有需要的好兄弟参考借鉴!

下面的实操要求有服务器、域名,如果是境内网站,还需要ICP备案等,这些全部都OK了,可以接着往下看。

配置流程

前置检查

本文实操环境是:CentOS 7.9 + Nginx,使用的是腾讯云提供的云服务器。

如果已经安装过nginx,请通过执行命令 nginx -V,检查是否有 configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module输出,如果没有,后面配置nginx时会有问题,需要重新安装。这里先把nginx服务停掉:

# 检查nginx服务是否还在运行
ps -ef | grep nginx

# 如果还在后台运行,停掉nginx服务
service nginx stop

如果还没有安装nginx或者,上述检查没有开启ssl module编译选项,需要重新安装一下nginx:

# 1. cd到nginx-1.18.0源码 (文末提供下载方式)
# [root@localhost-centos nginx-1.18.0]# ls
# auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

安装完毕后,执行命令检查

[root@localhost-centos nginx-1.18.0]# /usr/local/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

如果有上述输出,说明nginx安装OK。service nginx start启动nginx服务。

安装证书

以root用户或前置加sudo,执行以下命令:

# 安装Let's Encrypt
yum install certbot certbot-utils

# 手动安装证书,注意域名替换成自己真实的域名
certbot certonly --manual --preferred-challenges dns -d your.domain.url.com

如果输出以下信息:

Please deploy a DNS TXT record under the name
_acme-challenge.your.domain.url with the following value:
wqHCDexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxNm4
Before continuing, verify the record is deployed.
Press Enter to Continue

需要添加一条域名解析,如果你也是云服务器,一般登录到云服务器(如果你也是腾讯云,可以直访问腾讯云添加云解析教程)后台,有一个云解析 DNS的功能,点进去,添加一条txt解析即可,如下图:
image

添加完成后,可通过dig -t txt _acme-challenge.your.domain.url检查txt是否配置生效。生效后,按照提示回车,即可生成ssl证书。并打印出证书路径:

 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/your.domain.url/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/your.domain.url/privkey.pem
   Your certificate will expire on 2025-04-12. 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

配置nginx

worker_processes  2;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    include blockip.conf;
    include /home/MyCodeBase/html/www_navigator/nginx_conf/blacklist.conf;

    # 限流配置: 漏桶算法,64000个容量,每秒最多响应100个请求
    limit_req_zone $binary_remote_addr zone=limit_freq:4m rate=100r/s;
    limit_req zone=limit_freq burst=200;

    # 防止DDos攻击,限制代理资源
    proxy_buffer_size 4k;
    proxy_connect_timeout 900s;
    server {
        listen       443 ssl;
        server_name  your.domain.url;

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

        ssl_certificate      /etc/letsencrypt/live/your.domain.url/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/your.domain.url/privkey.pem;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        
        location / {
            root   your_service_path;
            proxy_pass http://127.0.0.1:service_port;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

然后,通过service nginx reload或者service nginx restart刷新一下nginx服务,即可通过https访问你的网站啦!

定期更新

Let's Encrypt安装的SSL证书有3个月的有效期,接近过期时需要更新证书,可以通过配置定时任务的方式更新证书:

# 编辑定时任务
crontab -e
# 配置计划: 每月的11号凌晨3点执行一次更新任务
0 3 11 * certbot renew && service nginx reload >> /dev/null 2>&1

总结

本文主要介绍了安装SSL证书的过程,如果你需要下载nginx,可以访问:Nginx官方下载入口 。如果过程中遇到别的问题,欢迎关注微信公众号 24K纯学渣,也许就能找到你想要的答案呢。

posted @   24K纯学渣  阅读(56)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示