Let's Encrypt是由互联网安全研究组(ISRG)开发的免费开放认证机构。 Let's Encrypt颁发的证书现在几乎所有浏览器都信任。
创新互联公司专注于企业营销型网站、网站重做改版、威宁网站定制设计、自适应品牌网站建设、H5页面制作、商城网站制作、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为威宁等各大城市提供网站开发制作服务。
先决条件
安装Certbot
更新软件包列表并安装certbot软件包:
sudo apt update
sudo apt install certbot
生成强大的Dh(Diffie-Hellman)组
Diffie-Hellman密钥交换(DH)是通过不安全的通信信道安全地交换加密密钥的方法。 我们将生成一组新的2048位DH参数以加强安全性:
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
如果你喜欢,你可以改变大小到4096位,但在这种情况下,生成可能需要超过30分钟,这取决于系统熵。
获取SSL证书
要获取我们域的SSL证书,我们将使用Webroot插件,该插件通过在${webroot-path}/.well-known/acme-challenge目录中创建所请求域的临时文件以及Let's Encrypt 验证服务器会使HTTP请求验证所请求域的DNS是否解析到运行certbot的服务器。
为了使它更简单,我们将把.well-known/acme-challenge的所有HTTP请求映射到单个目录/var/lib/letsencrypt。 以下命令将创建该目录并将其设置为可写入Nginx服务器。
mkdir -p /var/lib/letsencrypt/.well-known
chgrp www-data /var/lib/letsencrypt
chmod g+s /var/lib/letsencrypt
为避免重复代码,请创建以下两个片段,我们将在所有Nginx服务器块文件中使用这两个片段:
/etc/nginx/snippets/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/lib/letsencrypt/;
default_type "text/plain";
try_files $uri =404;
}
/etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
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;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;
add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
上面的代码片段使用了Mozilla的chippers recomendend,支持OCSP Stapling,HTTP严格传输安全性(HSTS),并强制实施少量以安全为重点的HTTP标头。 一旦创建了片段,打开域服务器块并包含letsencrypt.confsnippet,如下所示:
/etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
include snippets/letsencrypt.conf;
}
重新加载Nginx配置以使更改生效:
sudo systemctl reload nginx
您现在可以运行带有webroot插件的Certbot,并通过发出以下命令获取SSL证书文件:
sudo certbot certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com
如果SSL证书成功获取,certbot将打印以下消息:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2018-07-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
现在您已拥有证书文件,您可以按如下方式编辑域服务器块:
/etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com example.com;
include snippets/letsencrypt.conf;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
# . . . other code
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
# . . . other code
}
通过上面的配置,我们强制HTTPS并从www重定向到非www版本。
最后,重新加载Nginx服务以使更改生效:
sudo systemctl reload nginx
SSL证书自动续订
让我们加密的证书有效期为90天。 要在证书过期之前自动续订证书,certbot软件包将创建一个每天运行两次的cronjob,并且将在证书到期前30天自动续订任何证书。
由于我们在证书更新后使用certbot webroot插件,我们还必须重新加载nginx服务。 将--renew-hook "systemctl reload reload"添加到/etc/cron.d/certbot文件中,使其看起来像这样:
/etc/cron.d/certbot
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"
要测试续订过程,您可以使用certbot --dry-run开关:
sudo certbot renew --dry-run
如果没有错误,则意味着更新过程成功。
就这样! 如果您遇到任何问题,请随时发表评论。
分享名称:在Ubuntu 18.04上使用Let’s Encrypt保护Nginx
当前网址:http://www.shufengxianlan.com/qtweb/news42/168192.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联