SSL / TLS加密会为您的用户带来更高的搜索排名和更好的安全性。
Let’s Encrypt 是一个认证机构(CA)。它可以提供免费证书,并且已经被大多数浏览器所信任。另外,通过工具 Certbot 可以让我们完全自动化证书的安装和更新。
安装证书的前提条件:

安装服务器(这里用 NGINX)。
注册域名。
创建一个DNS记录,将域名和服务器的 IP 地址相关联。
记得安装完成后,防火墙需要打开 443 端口,否则无法访问!!!

1. 安装 Let’s Encrypt 客户端
所有的证书相关的操作,都可以通过 Certbot 软件实现。
注意:HTTPS 作为重要的基础服务,一旦出问题就需要立刻把软件更新到官网提供的最新版本,所以不推荐用各个 Linux 发行版的默认仓库进行安装,而是通过官方工具 certbot-auto 安装。

wget https://dl.eff.org/certbot-auto # 下载到本地
chmod a+x ./certbot-auto # 添加可执行权限
./certbot-auto --help all # 查看帮助

2. 验证域名所有权
配置 Nginx,使要获取证书的域名对应的 80 端口可以正常访问。在 /etc/nginx/conf.d/ 目录下为域名创建新文件 www.example.com.conf,并添加相关配置信息:

server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;

location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

重启 Nginx

systemctl restart nginx

3. 生成证书
certbot-auto --nginx -d kikakika.com -d www.kikakika.com

出现错误:

Error while running nginx -c /etc/nginx/nginx.conf -t.

nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

解决方法:

/certbot-auto --nginx --nginx-server-root=/usr/local/nginx/conf

正常情况下,进入交互式界面,提示你输入邮箱(在证书失效前收到通知邮件),并同意官方协议。

[root@VM_120_242_centos tmp]# ./certbot-auto --nginx -d scall2.szhuizhong.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for scall2.szhuizhong.cn
Waiting for verification...
Cleaning up challenges
Deployed Certificate to VirtualHost /etc/nginx/nginx.conf for scall2.szhuizhong.cn

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
No redirect - Make no further changes to the webserver configuration.
Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.

证书生成成功后,会让你选择是否将所有的 HTTP 请求重定向到 HTTPS(输入 1 或者 2)。如果选 1,则通过 HTTP 和 HTTPS 都可以访问。如果选 2,则所有通过 HTTP 来的请求,都会被 301 重定向到 HTTPS。参考下面的 5. 配置 Nginx。
输完 1 或者 2 回车后,会有成功提示,并说明证书放在 /etc/letsencrypt/live/证书的域名 这个位置:

4. 开启 443 端口
开启443端口:firewall-cmd --permanent --add-port=443/tcp
查看是否成功:firewall-cmd --permanent --query-port=443/tcp
端口添加成功后,需要reload重新载入:firewall-cmd --reload

5. 配置 Nginx
certbot-auto 会自动调用 Nginx 的插件,改写配置文件。可以通过参数 certonly 关闭自动改写配置文件的功能,只获取证书。
./certbot-auto certonly -d kikakika.com

下面是 certbot-auto 自动改写过的配置文件,所有改写过的行都在后面加了注释:# managed by Certbot。

# 生成证书时选 1: No redirect,不把 HTTP 请求重定向到 HTTPS,一个 server 同时监听 80 和 443 端口
server {
listen 80;
server_name scall2.szhuizhong.cn;
root /home/szhuizhong/trunk;

location / {
if (-f $request_filename) {
expires max;
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
index index.html index.htm index.php l.php;
autoindex off;
}

error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param CI_ENV 'testing';
include fastcgi_params;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/scall2.szhuizhong.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/scall2.szhuizhong.cn/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

# 生成证书时选 2: redirect,HTTP 请求重定向到 HTTPS,两个 server 分别监听 80 和 443 端口
server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;

location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/kikakika.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/kikakika.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
server_name kikakika.com www.kikakika.com;
listen 80;
return 301 https://$host$request_uri; # managed by Certbot
}

6. 设置定时任务
出于安全策略, Let’s Encrypt 签发的证书有效期只有 90 天。通过 ./certbot-auto renew 命令可以续签。
- 编辑 crontab 文件:

$ crontab -e

添加 certbot 命令:
在每天凌晨3点运行。该命令将检查服务器上的证书是否将在未来30天内过期,如果是,则进行更新。--quiet 指令告诉 certbot 不要生成输出。
0 3 * * * /root/letsencrypt/certbot-auto renew --quiet

写入日志

0 0 1 * * /home/soft/ssl/letsencrypt/certbot-auto renew>>/home/soft/ssl/letsencrypt/log.txt

保存并关闭文件。所有安装的证书将自动更新并重新加载。

Nginx 实现 HTTPS(基于 Let's Encrypt 的免费证书)的更多相关文章

  1. 基于Let's Encrypt生成免费证书-支持多域名泛域名证书

    目录 客户端 certbot acme.sh 安装acme.sh 1. 自动安装 2. 手动安装 3. 测试收否安装成功 使用acme.sh生成证书 1. HTTP 方式 2. DNS 方式 1. 生 ...

  2. nginx配置https及Android客户端访问自签名证书

    前一篇随笔通过keytool生成keystore并为tomcat配置https,这篇随笔记录如何给nginx配置https.如果nginx已配置https,则tomcat就不需要再配置https了.通 ...

  3. Ubuntu Linux服务器搭建SSL/TLS(https)(在StartSSL可以得到免费证书)

    目录 1 生成公钥和私钥对 2 公钥提交到CA机构签发一个crt证书 3 配置证书链 4 在Apache里开启SSL支持并配置crt证书和私钥 5 配置HSTS (可选) 6 总结 首先SSL/TLS ...

  4. Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)

    单域名证书的生成可以 参考这里. acme.sh 项目中文文档 Let's Encrypt 在 18 年 1 月份推出了 ACME v2,支持通配符域名证书,对小网站.个人站长的友好度进一步增加. 常 ...

  5. nginx安装Lets Encrypt SSL免费HTTPS加密证书

    Linux Nginx网站:Certbot安装配置Lets Encrypt SSL免费HTTPS加密证书 原文地址:https://renwole.com/archives/157 实验环境:Cent ...

  6. nginx+tomcat https实践

    1. 安装ssl'证书 使用Let's Encrypt 的免费证书: 下载源代码: git clone https://github.com/letsencrypt/letsencrypt 我时阿里云 ...

  7. 免费证书https://lamp.sh/ssl.html

    https(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 http 通道,简单讲是 http 的安全版.即 ht ...

  8. 基于 Nginx 的 HTTPS 性能优化

    前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...

  9. 基于 Nginx 的 HTTPS 性能优化实践

    前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...

随机推荐

  1. mac OS 安装 nvm

    nvm官网 https://github.com/creationix/nvm nvm,node,npm之间的区别 nvm:nodejs 版本管理工具 一个 nvm 可以管理很多 node 版本和 n ...

  2. 解决layui动态追加的点击事件不起作用问题

    2019独角兽企业重金招聘Python工程师标准>>> //不起作用 $('#demo').on('click', function() { layer.msg('响应点击事件'); ...

  3. 开发常见错误解决(6)WSE3.0未处理的WebException,未处理的Web异常,基础连接以及关闭...

    开发常见错误解决(6)WSE3.0未处理的WebException,未处理的Web异常,基础连接以及关闭. 我们在调试WSE服务端服务的时候会抛出,未处理的Web异常,基础连接以及关闭的异常信息.如图 ...

  4. 信息竞赛进阶指南--区间最值问题的ST算法

    void ST_prework() { for (int i = 1; i <= n; i++) f[i][0] = a[i]; int t = log(n) / log(2) + 1; for ...

  5. shell之路 shell核心语法【第一篇】shell初识

    shell简介 1.Shell是Unix的脚本语言,是与 Unix/Linux 交互的工具,shell使用的熟练程度反映了用户对Unix/Linux使用的熟练程度 2.Shell是系统命令+程序逻辑的 ...

  6. 低价购买(LIS方案统计)

    题意:https://www.luogu.com.cn/problem/P1108 如果两个数列组成的数字完全相同,那我们说这两个数列相同. 求出最长下降子序列的方案数. 题解来自 wjyyy大神. ...

  7. 一些常见的pro文件配置

    UI_DIR = ./ui #ui文件目录 TARGET = Test #最终生成目标名 DESTDIR = $$PWD/../test #目标生成目录,$$PWD表示当前目录下 DLLDESTDIR ...

  8. Leetcode_236. 二叉树的最近公共祖先

    求二叉树的LCA code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le ...

  9. dumpsys-package

    dumpsys-package ams和pms是android系统最重要的系统服务,本文解析dumpsys package命令,看哪些PMS相关的系统信息,数据结构是运行时可以查看的. 命令提示 co ...

  10. qt绘制甘特图

    重写paintEvent事件,代码如下 void xx::paintEvent(QPaintEvent *event){ QPainter painter(this); //绘制x,y轴,_maxWi ...