在 Debian 9 中使用 SSL/TLS 安装 Lets Encrypt and Secure Nginx在 Debian 9 中使用 SSL/TLS 安装 Lets Encrypt and Secure Nginx在 Debian 9 中使用 SSL/TLS 安装 Lets Encrypt and Secure Nginx在 Debian 9 中使用 SSL/TLS 安装 Lets Encrypt and Secure Nginx
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

在 Debian 9 中使用 SSL/TLS 安装 Lets Encrypt and Secure Nginx

在此页

  1. 安装 Certbot 客户端实用程序
  2. 获取 Let's Encrypt 证书
  3. 获取 Lets encrypt SSL 证书的替代方法
  4. 为 TLS (SSL) 配置 Nginx
  5. 在 Nginx 中安装 Let's Encrypt Certificates
  6. 强制使用 HTTPS 传输网络流量
  7. 最终检查
  8. 自动更新 Let's Encrypt 证书

本教程将向您展示如何使用 Let's Encrypt 证书颁发机构免费颁发的 TLS 证书在 Debian 9 上安装和保护 Nginx 网络服务器。此外,我们将在证书过期之前使用 cron 作业配置 Lets’ Encrypt TLS 证书的自动更新。

TLS,也称为传输层安全性,是一种网络协议,它使用 SSL 证书来加密在服务器和客户端之间流动的网络流量,或者在 Web 服务器(例如 Nginx 服务器)和浏览器之间流动的网络流量。这两个实体之间交换的所有数据都是安全的,即使使用中间人攻击或数据包嗅探等技术拦截连接也无法解密。 certbot 软件包软件是 Let's Encrypt CA 提供的官方客户端实用程序,可用于在 Debian 中生成和下载免费的 Let's Encrypt 证书的过程。

要求

  • 在 Debian 9 中安装 LEMP Stack。
  • 具有适当 DNS 记录(子域的 A 记录或 CNAME 记录)的公共注册域名。
  • 直接访问服务器的控制台或远程 SSH 访问服务器。
  • 具有根权限或直接访问根帐户的用户帐户。

安装 Certbot 客户端实用程序

为了从 Debian 官方存储库在 Debian 9 中安装 Let's Encrypt cerbot 客户端实用程序,打开终端并以 root 权限发出以下命令,如以下屏幕截图所示。

apt-get install certbot

Debian apt 实用程序是 Debian 主包管理器的命令行界面,在进行一些初步检查后,会询问您是否同意继续安装 certbot 包及其所有必需的依赖项。要接受安装软件的过程,请在服务器的控制台提示中用是 (y) 回答。

获取 Let's Encrypt 证书

要为域生成和下载 Let's Encrypt 证书,首先确保 Nginx Web 服务器已启动并正在运行,并且至少可以从 Internet 访问端口 80。要验证 Nginx 是否正在运行,请以 root 权限执行以下命令。

sudo systemctl status nginx

如果服务器尚未运行,请使用以下命令启动 Nginx 守护进程。

sudo systemctl start nginx

如果您在 Debian 中安装了通用防火墙,例如 UFW 防火墙,它会阻止端口 80 和 443 上的所有传入连接,请发出以下命令在系统中打开 HTTP 和 HTTPS 端口。

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

要为您的域获取证书,请在控制台中使用以下参数和标志执行 cerbot 命令,如下所述。使用 root 权限运行该命令,并提供您的域名和您想要通过使用 -d 标志获取证书的所有其他子域。此外,提供 --standalone 选项以使 certbot 不干扰 Nginx 配置文件。使用此选项颁发证书时必须停止 Nginx 服务器。

cerbot 命令语法:

sudo certbot certonly --standalone –d yourdomain.com –d www.yourdomain.com

当您第一次运行 certbot 命令并获取证书时,系统会提示您添加您的电子邮件地址并同意 Let's Encrypt 服务条款。写“a”表示同意服务条款“不”表示不与 Let's Encrypt 合作伙伴共享您的电子邮件地址。最后,在您获得域的证书后,请阅读页脚注释以找到证书系统路径和它们的过期日期。

获取 Lets encrypt SSL 证书的替代方法

要通过 \webroot\ 插件获取 Let's Encrypt 证书,请使用以下语法。添加 Nginx web 根目录,默认情况下位于 /var/www/html/ 系统路径,同时使用 --webroot 和 –w 标志发出 certbot 命令。另外,确保 Nginx 对 web 根目录具有完全写入权限,以便创建 /.well-known 目录。

cerbot 的 --webrot 选项还会要求您添加用于证书续订和安全通知的电子邮件地址。 Certbot 客户端具有可以检测虚假电子邮件地址的内置代码。您必须提供可公开访问的电子邮件地址才能继续获取证书。

certbot certonly --webroot –w /var/www/html/ -d yourdomain.com –d www.yourdomain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):  #A fake email address will be detected
There seem to be problems with that address. Enter email address (used for
urgent renewal and security notices)  If you really want to skip this, you can
run the client with --register-unsafely-without-email but make sure you then
backup your account key from /etc/letsencrypt/accounts   (Enter 'c' to cancel):
 
-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a
 
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: n
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.domain.com
Using the webroot path /var/www/html/  for all unmatched domains.
Waiting for verification...
Cleaning up challenges
 
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
    /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem. Your
   cert will expire on 2017-12-28. 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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should
   make a secure backup of this folder now. This configuration
   directory will also contain certificates and private keys obtained
   by Certbot so making regular backups of this folder is ideal.
 - 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

为 TLS (SSL) 配置 Nginx

域的 Nginx 完整默认 TLS 配置文件应类似于以下文件摘录。

/etc/nginx/sites-enabled/default-ssl 文件示例:

    server {
                    listen 443 ssl default_server;
                    listen [::]:443 ssl default_server;
      
                server_name  www.yourdomain.com yourdomain.com;
                #server_name _;
                               
                root /var/www/html;
                               
                access_log /var/log/nginx/access.log;
                error_log /var/log/nginx/error.log;
              
               #SSL Certificates
                ssl_certificate "/etc/letsencrypt/live/www.yourdomain.com/cert.pem";
                ssl_certificate_key "/etc/letsencrypt/live/www. yourdomain.com/privkey.pem";
                ssl_dhparam /etc/nginx/dhparam.pem;
               
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
 
                ssl_session_cache shared:SSL:1m;
               ssl_session_timeout 10m;
                ssl_ciphers HIGH:!aNULL:!MD5;
                ssl_prefer_server_ciphers  on;
               
                add_header Strict-Transport-Security "max-age=31536000;
    #includeSubDomains" always;
 
                location / {
        index index.php index.html index.htm;
                    try_files $uri $uri/ /index.php?$args $uri/ =404;
                }
 
               
                set $cache_uri $request_uri;
               
                location ~ /.well-known {
                allow all;
                                }
              
                #
                #             # With php-fpm (or other unix sockets):
                                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                #             # With php-cgi (or other tcp sockets):
                #             fastcgi_pass 127.0.0.1:9000;
                }

               # deny access to .htaccess files, if Apache's document root
                # concurs with nginx's one
                #
                #location ~ /\.ht {
                #             deny all;
                #}
               
    }

通过 FastCGI Process Manager 处理 PHP 脚本的代码行也可以在此摘录中找到,并由以下行表示。

location ~ \.php$ {
                                include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                }

在 Nginx 中安装 Let's Encrypt 证书

Let's Encrypt 证书和密钥存储在 Debian 中的 /etc/letsencrypt/live/www.yourdomain.com/ 目录中。针对上述目录的 ls 命令将显示所有证书组件,例如链文件、全链文件、私钥和证书文件。

ls /etc/letsencrypt/live/www.yourdomain.com/

要在 Nginx Web 服务器 TLS 配置文件中安装 Let's Encrypt 证书,请打开 Nginx default-ssl 文件并更新以下行以反映您域的 Let's Encrypt 证书文件路径,如以下摘录所示。

nano /etc/nginx/sites-enabled/default-ssl

更改以下行,如下所示:

                ssl_certificate "/etc/letsencrypt/live/www.yourdomain.com/cert.pem ";
                ssl_certificate_key "/etc/letsencrypt/live/www.yourdomain.com /privkey.pem";

此外,如果 Nginx SSL 配置中存在 ssl_dhparam 语句,则必须通过发出以下命令生成一个新的 2048 位 Diffie–Hellman 密钥。 Diffie–Hellman 关键参数生成需要一段时间,具体取决于您的系统随机性或熵。

openssl dhparam –out /etc/nginx/dhparam.pem 2048

最后,在通过重新启动 Nginx 守护进程以反映更改来激活 Nginx TLS 配置之前,首先检查 Nginx 配置是否存在潜在的语法错误。之后,如果 Nginx 配置文件测试成功,则通过发出以下命令重新启动 Nginx 守护进程以加载新配置以及 Let's Encrypt 证书。

nginx -t
service nginx restart

为了验证 nginx 服务是否在端口 443 上拥有一个处于侦听状态的打开的套接字,请执行 netstat 命令,如下面的摘录所示。

netstat –tulpn | grep -e 443 -e LISTEN

您还应该打开浏览器并通过 HTTPS 协议导航到您的域名。如果 Let's Encrypt 证书在 Nginx 中成功应用,SSL 握手应该可以顺利进行,不会抛出任何错误。

强制网络流量到 HTTPS

要强制您的域访问者仅通过 HTTPS 协议浏览您的网站,请打开启用 Nginx 站点的默认配置文件并添加以下行,这将强制所有命中端口 80 的请求使用 301 状态代码(永久移动)重定向到端口443.

nano /etc/nginx/sites-enabled/default

重定向语句应该类似于下面的摘录。

server_name www.yourdomain.com yourdomain.com;
return         301 https://$server_name$request_uri;

随后,通过发出以下命令重新启动 Nginx 守护进程以应用更改。

service nginx restart

最终检查

要进一步调查 Let's Encrypt CA 生成的证书,您可以使用现代网络浏览器,例如 Chrome。从 Chrome 浏览器访问您的域,然后按 F12 功能键打开开发人员工具。导航到安全选项卡并点击查看证书按钮以打开证书,如以下屏幕截图所示。

事实证明,调查 SSL 证书的另一个有用实用程序是 openssl 命令行实用程序。为了显示 Let's Encrypt CA 证书的额外信息,请在 Linux 控制台中执行以下命令。

openssl s_client –connect www.yourdomain.com:443

自动更新 Let's Encrypt 证书

为了在到期日期之前自动续订 Let's Encrypt CA 颁发的证书,请通过发出以下命令安排一个 crontab 作业在每天凌晨 2:00 运行一次。执行的 cron 作业的输出将被定向到一个日志文件,存储在 /var/log/letsencrypt.log

crontab –e

更新证书的 Cron 作业。

0 2 * * * certbot renew >> /var/log/letsencrypt.log

就这样!有关 Let's Encrypt 证书和实用程序的其他更高级配置,请访问以下网址的官方文档 https://certbot.eff.org/docs/。

©2015-2025 艾丽卡 support@alaica.com