如何使用acme.sh和Cloudflare DNS颁发Lets Encrypt通配符证书
如何使用acme.sh设置并使用Cloudflare DNS。
如何通过Cloudflare DNS的Lets Encrypt TLS/SSL证书问题保护Nginx Web服务器的安全?
对于通配符TLS/SSL证书,"让加密"接受的唯一挑战方法是DNS挑战,以验证域所有权。
因此,我们需要Cloudflare DNS API为我们的域添加/修改DNS。
本教程说明了如何使用在Linux或类Unix系统上运行的Lets Encrypt客户端acme.sh生成通配符TLS/SSL证书。
获取Lets Encrypt通配符证书的配置要求
您需要安装并运行Nginx服务器。
例如:
$ sudo apt install nginx $ sudo yum install nginx
步骤1获取Cloudflare API密钥
转到Cloudflare控制面板并获取API密钥:单击"创建令牌">"编辑区域DNS">"使用模板":确保将DNS"权限"设置为"编辑"并将区域包括到您的DNS域中名称,例如theitroad.local:最后,单击"继续摘要"以查看"编辑区域DNS API令牌摘要",如下所示:最后,复制屏幕上显示的令牌以访问Cloudflare API。
不要与任何人共享此令牌。
保持安全和秘密。
步骤2安装acme.sh客户端
获取Cloudflare DNS API密钥后,现在设置acme.sh客户端。
因此,使用git命令克隆acme.sh存储库:
$ cd /tmp/ $ git clone https://github.com/Neilpang/acme.sh.git
安装客户端,但首先使用su命令/sudo命令以root用户身份登录:
$ sudo -i # touch /root/.bashrc # cd /tmp/acme.sh/ # acme.sh --install --accountemail your-email-id@domain-here
步骤3颁发Lets Encrypt通配符证书
到目前为止,我们已经设置了Nginx,获得了Cloudflare DNS API密钥,现在是时候使用acme.sh来获得theitroad.local域的通配符证书了。
首先使用export命令设置CF_Token,如下所示:
# Export single variable for the CloudFlare DNS challenge to work # # export CF_Token="Your_Cloudflare_DNS_API_Key_Goes_here"
无需定义shell变量CF_Account_ID和CF_Zone_ID,因为这些变量将由acme.sh自动提取。
然后请求证书。
确保使用域名替换theitroad.local:
# acme.sh --issue --dns dns_cf --ocsp-must-staple --keylength 4096 -d theitroad.local -d *.theitroad.local
如果您更喜欢椭圆曲线加密(ECC/ECDSA)而不是RSA,请尝试:
尽管可以将Nginx配置为使用RSA和ECDSA证书,但是我将在这里使用RSA,因为我的LB仅支持RSA。
但是您也可以提供双证书配置,默认情况下会提供RSA证书,并向那些表示支持的客户端提供(小得多的)ECDSA证书。
# acme.sh --issue --dns dns_cf --ocsp-must-staple --keylength ec-384 -d theitroad.local -d *.theitroad.local
您的Cloudflare DNS API密钥位于/root/.acme.sh/account.conf文件中,我们可以使用cat命令或grep命令查看它:
# cat /root/.acme.sh/account.conf # grep '_CF_' /root/.acme.sh/account.conf
步骤4配置Nginx Web服务器
确保使用openssl命令按如下所示创建Diffie-Hellman密钥交换文件:
# mkdir -pv /etc/nginx/letsencrypt/theitroad.local/ # cd /etc/nginx/letsencrypt/theitroad.local/ # openssl dhparam -out /etc/nginx/ssl/letsencrypt/theitroad.local/dhparams.pem -dsaparam 4096
然后编辑您的Nginx配置文件:
# vi /etc/nginx/nginx.conf
编辑/更新如下:
# Port 80 config server { listen 80 default_server; # IPv4 listen [::]:80 default_server; # IPv6 server_name www.theitroad.local; access_log off; error_log off; root /var/www/html; return 301 https://$host$request_uri; } # Port 443 config server { listen 443 ssl http2; # IPv4 listen [::]:443 ssl http2; # HTTP/2 TLS IPv6 server_name www.theitroad.local; # domain name # Set document root location / { root /var/www/html; index index.html; } # Set access and error log for this vhos access_log /var/log/nginx/theitroad.local_access.log; error_log /var/log/nginx/theitroad.local_error.log; # TLS/SSL CONFIG ssl_certificate /etc/nginx/ssl/theitroad.local/theitroad.local.fullchain.cer; ssl_certificate_key /etc/nginx/ssl/theitroad.local/theitroad.local.key; # ECC/ECDSA certificates (dual config) #ssl_certificate /etc/nginx/ssl/theitroad.local/theitroad.local.fullchain.cer.ecc; #ssl_certificate_key /etc/nginx/ssl/theitroad.local/theitroad.local.key.ecc; ssl_dhparam /etc/nginx/ssl/theitroad.local/dhparams.pem; # A little bit of optimization ssl_session_timeout 1d; ssl_session_cache shared:theitroadSSL:10m; # TLS version 1.2 and 1.3 only ssl_session_tickets off; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; # HSTS (ngx_http_headers_module is required) # ************************************************************************* # WARNING - Wrong headers can create problems. Read docs otherwise # all 3rd party scripts/ads won't load and in some case # browser won't work. Read docs @ https://developer.mozilla.org # ************************************************************************* add_header Strict-Transport-Security "max-age=63072000" always; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Xss-Protection "1; mode=block" always; add_header Referrer-Policy strict-origin-when-cross-origin always; add_header Feature-policy "accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'" always; # *************************************************************************************************** # WARNING: The HTTP Content-Security-Policy response header allows sysadmin/developers # to control resources the user agent is allowed to load for a given page. # Wrong config can create problems for third party scripts/ad networks. Hence read the following url: # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy # **************************************************************************************************** add_header content-security-policy "default-src https://www.theitroad.local:443" always; # OCSP stapling # Verify chain of trust of OCSP response using Root CA and Intermediate certs ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /etc/nginx/ssl/theitroad.local/theitroad.local.fullchain.cer; # Replace with the IP address of your resolver resolver 1.1.1.1; }
在vim中保存并关闭文件。
步骤5安装Lets Encrypt通配符证书
安装您的证书(根据您的Linux/Unix发行版替换" systemctl reload nginx"命令):
# DOMAIN="theitroad.local" # CONFIG_ROOT="/etc/nginx/ssl/${DOMAIN}" # acme.sh -d "$DOMAIN" \ --install-cert \ --reloadcmd "systemctl reload nginx" \ --fullchain-file "${CONFIG_ROOT}/$DOMAIN.fullchain.cer" \ --key-file "${CONFIG_ROOT}/$DOMAIN.key" \ --cert-file "${CONFIG_ROOT}/$DOMAIN.cer"
如果您也需要安装ECC/ECDSA(同样,请根据您的Linux/Unix发行版替换" systemctl reload nginx"命令):
# acme.sh -d "$DOMAIN" \ --ecc \ --install-cert \ --reloadcmd "systemctl reload nginx" \ --fullchain-file "${CONFIG_ROOT}/$DOMAIN.fullchain.cer.ecc" \ --key-file "${CONFIG_ROOT}/$DOMAIN.key.ecc" \ --cert-file "${CONFIG_ROOT}/$DOMAIN.cer.ecc"
第6步测试
如果尚未打开Nginx服务器tcp端口443,请确保已打开。
例如,这是我们如何在Ubuntu或Debian Linux上打开它的方法:
$ sudo ufw allow https comment 'Open all to access Nginx port 443'
打开浏览器并输入网址:
https://www.theitroad.local/
当然,我们可以访问SSL实验室来测试TLS/SSL配置页面。
另一个选择是运行testssl.sh命令,如下所示:
$ testssl.sh --fast --parallel https://www.theitroad.local/