网站搜索

如何在 FreeBSD 中使用 SSL 和 Let’s Encrypt 保护 Nginx


在本指南中,我们将讨论如何使用 Let's Encrypt 证书提供的 TLS/SSL 证书来保护 FreeBSD 中的 Nginx Web 服务器权威。我们还将向您展示如何在到期日期之前自动续订 Lets’ Encrypt 证书。

TLS传输层安全的缩写,是一种在HTTP协议下运行的协议,使用证书和密钥来封装数据包和加密服务器和客户端之间交换的数据,或者在本例中是 Nginx Web 服务器和客户端浏览器之间交换的数据,以保护连接安全,以便可能拦截流量的第三方无法解密传输。

另请阅读:在 FreeBSD 上安装 Let’s Encrypt for Apache

通过安装 certboot 客户端实用程序(这是官方使用的 Let's Encrypt 客户端),可以大大简化在 FreeBSD 中获取免费 Let's Encrypt 证书的过程用于生成和下载证书。

要求

  1. 在 FreeBSD 中安装 FBEMP(Nginx、MariaDB 和 PHP)堆栈

第 1 步:配置 Nginx TLS/SSL

1. 默认情况下,FreeBSD 中未启用 TLS/SSL 服务器配置,因为 TLS 服务器阻止语句在 Nginx 默认配置文件中注释。

为了在Nginx中激活TLS服务器,打开nginx.conf配置文件,搜索定义开头的行SSL 服务器并更新整个块,如下例所示。

nano /usr/local/etc/nginx/nginx.conf

Nginx HTTPS 块摘录:

server {
       listen 443 ssl  default_server;
       server_name  www.yourdomain.com;
	
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	error_page   500 502 503 504  /50x.html;
        
	location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
	
	location / {
	    root   /usr/local/www/nginx;
       	    index  index.html index.htm;
	    try_files $uri $uri/ /index.php?$args;
				}

	ssl_certificate "/usr/local/etc/letsencrypt/live/www.yourdomain.com/cert.pem";
	ssl_certificate_key "/usr/local/etc/letsencrypt/live/www.yourdomain.com/privkey.pem";
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_dhparam /usr/local/etc/nginx/dhparam.pem;
	ssl_session_cache shared:SSL:1m;
	ssl_session_timeout 10m;
	ssl_ciphers HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;

	# Use gzip compression
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.0;

	# Set a variable to work around the lack of nested conditionals
	
	set $cache_uri $request_uri;
	
	location ~ /.well-known {
	allow all;
		}
    


        location ~ \.php$ {
        root	/usr/local/www/nginx;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $request_filename;    
		include        fastcgi_params;
        }	
    }

上面的块除了SSL块之外,还包含一些启用gzip压缩和FastCGI Process Manager的语句,用于将PHP代码传递给PHP-FPM< 网关,以便运行动态 Web 应用程序。

将上述代码添加到 Nginx 主配置文件后,在为您的域安装并获取 Let’s Encrypt 证书之前,请勿重新启动守护程序或应用设置。

步骤 2:在 FreeBSD 中安装 Certbot 客户端

2. 在 FreeBSD 中安装 Let's Encrypt certbot 客户端实用程序的过程包括下载 py-certbot 的源代码并在本地编译它,方法是发出以下命令。

cd /usr/ports/security/py-certbot
make install clean

3.与安装常规二进制包相比,编译py-certbot实用程序需要花费大量时间。这段时间需要下载一系列依赖在FreeBSD本地编译。

此外,屏幕上会出现一系列提示,要求您选择在编译时为每个依赖项使用哪些包。在第一个屏幕中,通过按[空格]键选择以下工具,用于编译python27依赖项,如下图所示。

  • IPV6
  • LIBFFI
  • 非线性LS
  • 皮玛洛克
  • 线程
  • UCS4 支持 Unicode

4. 接下来,为 gettext-tools 依赖项选择 DOCSTHREADS,然后按确定 > 继续,如下图所示。

5. 在下一个屏幕上,将 libffi-3.2.1TESTS 选项禁用,然后按 OK 移动更远。

6. 接下来,点击空格py27-enum34依赖项选择DOCS,这将安装此文档工具,然后按确定继续,如下面的屏幕截图所示。

7.最后,按[空格]键选择安装py27-openssl依赖项的示例示例,然后点击确定 完成py-certbot客户端的编译和安装过程。

8.编译和安装py-certbot实用程序完成后,运行以下命令以将工具升级到最新版本的软件包,如下所示下面的截图。

pkg install py27-certbot

9.为了避免在获取免费Let's Encrypt证书时可能出现的一些问题,最常见的错误是“pkg_resources.DistributionNotFound” ,确保您的系统中还存在以下两个依赖项:py27-saltpy27-acme

pkg install py27-salt
pkg install py27-acme

步骤 3:在 FreeBSD 上安装 Nginx 的 Let’s Encrypt 证书

10. 要为您的域获取 Let's Encrypt 独立证书,请运行以下命令,并通过暗示 -d 标志。

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

11.生成证书时,系统会要求您输入电子邮件地址并同意 Let’s Encrypt 服务条款。从键盘输入 a 以同意并继续,系统还会询问您是否愿意与 Let’s Encrypt 合作伙伴共享您的电子邮件地址。

如果您不想共享您的电子邮件地址,只需在提示中输入no单词,然后按[enter]键继续。成功获取您的域的证书后,您将收到一些重要的注释,其中将通知您证书在系统中的存储位置以及证书何时过期。

12. 如果您想使用“webroot”插件通过添加您域的 Nginx 服务器的 webroot 目录来获取 Let's Encrypt 证书,使用 --webroot-w 标志发出以下命令。默认情况下,如果您没有更改 Nginx webroot 路径,它应该位于 /usr/local/www/nginx/ 系统路径中。

certbot certonly --webroot -w /usr/local/www/nginx/ -d yourdomain.com -d www.yourdomain.com

与获取证书的 --strandalone 过程一样,--webroot 过程也会要求您提供用于证书续订和安全通知的电子邮件地址,请按 a 同意 Let's Encrypt 条款和条件,noyes 表示或不共享 Let's Encrypt 合作伙伴的电子邮件地址,如下例所示。

请注意,certbot 客户端可以检测到虚假电子邮件地址,并且在您提供真实电子邮件地址之前不会让您继续生成证书。

赛博特样本:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):[email   #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):[email 

-------------------------------------------------------------------------------
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 /usr/local/www/nginx/ for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /usr/local/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 /usr/local/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

步骤 4:更新 Nginx TLS 证书

13.在FreeBSD中获取的Let’s Encrypt证书和密钥的位置是/usr/local/etc/letsencrypt/live/www.yourdomain.com/系统路径。发出 ls 命令以显示 Let’s Encrypt 证书的组件:链文件、全链文件、私钥和证书文件,如以下示例所示。

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

14. 为了在 Nginx Web 服务器中为您的域安装 Let's Encrypt 证书,请打开 Nginx 主配置文件或 Nginx TLS 服务器的配置文件(如果它是单独的文件),然后修改以下行以反映 Let's Encrypt 颁发的证书的路径,如下所示。

nano /usr/local/etc/nginx/nginx.conf

将以下行更新为与本示例中类似:

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

15. 另外,如果 Nginx SSL 配置中存在 ssl_dhparam 行,您应该生成一个新的 2048 位使用以下命令的 Diffie–Hellman 键:

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

16.最后,为了激活 Nginx TLS 配置,首先检查 Nginx 全局配置是否存在可能的语法错误,然后通过发出以下命令重新启动 Nginx 服务以应用 SSL 配置。

nginx -t
service nginx restart

17.通过发出以下命令来确认Nginx守护进程是否绑定在443端口上,该命令可以列出系统中所有处于侦听状态的打开的网络套接字。

netstat -an -p tcp| grep LISTEN
sockstat -4 

18.您还可以通过打开浏览器并输入以下地址,通过HTTPS协议访问您的域名地址,以确认 Let’s Encrypt 证书是否按预期工作。由于您使用的是有效证书颁发机构生成的证书,因此浏览器中不应显示任何错误。

https://www.yourdomain.com

19. Openssl 实用程序还可以通过运行带有以下选项的命令来帮助您查找从 Let’s Encrypt CA 获取的证书的信息。

openssl s_client -connect www.yourdomain.com:443

如果您想强制 Nginx 将您的域在端口 80 上收到的所有 http 到 https 请求定向到 HTTPS,请打开 Nginx 配置文件,找到端口 80 的服务器指令并在 server_name 语句后添加以下行,如下例所示。

rewrite ^(.*) https://www.yourdomain.com$1 permanent;

20. 在 Let’s Encrypt 机构颁发的证书过期之前设置自动续订可以通过发出以下命令安排 cron 作业每天运行一次来完成。

crontab -e

更新证书的 Cron 任务。

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

就这样! Nginx 现在可以使用 Let’s Encrypt 免费证书向访问者提供安全的 Web 应用程序。


版权所有。 © Linux-Console.net • 2019-2024