HTTPS+Let' s Encrypt+Nginx(格

2017-04-13  本文已影响326人  风清水遥

注意:本文主要是为了让服务器支持https协议。因为服务器默认支持http://www.xxx.com:80,本文主要介绍申请免费证书。

1.下面按照步骤来:全命令行模式

2.申请证书

3.使用官方git开源库自动创建的方法

//如果没有安装git  :
sudo apt-get update     //更新apt-get命令到最新
sudo apt-get install git    //自动安装git
1. 下载git仓库到本地

git clone https://github.com/certbot/certbot

详细介绍步骤在:https://certbot.eff.org/docs/contributing.html

2.切换到项目根目录下

cd certbot

3.启动自动系统更新依赖

./letsencrypt-auto-source/letsencrypt-auto —os-packages-only

4.执行脚本工具

./tools/venv.sh

5.添加执行动作快捷命令

source ./venv/bin/activate

6.启动自动申请流程

certbot

7.启动后,按照提示来操作
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: www.baidu.com
2: www.baidu1.com
3: www.baidu2.com
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input

blank to select all options shown (Enter 'c' to cancel):

坑:这里会自动查询默认(Apache)服务器配置的域名,比如上面百度的三个域名,由于我的服务器是nginx,所以会把Apache以前配置的旧域名搜搜过来,解决办法就是,改掉apachectl 配置的域名到正确,搜索出来就对了,还有通过https://github.com/certbot/certbot的介绍方式
./certbot-auto certonly --standalone --email admin@example.com -d example.com -d www.example.com -d other.example.net
当然还有其他方法,还有一个坑就是,必须先关闭服务器,因为,这个命令会占用校验80,443端口

8.选中你要申请的域名,就会等待生成,中间会问你一下,还有结果,我一次粘贴
Please choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://www.baidu.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.baidu.com
-------------------------------------------------------------------------------
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at/etc/letsencrypt/live/www.baidu.com/fullchain.pem. 
Your cert  will expire on 2017-02-08. To obtain a new or tweaked version of  this certificate in the future, 
simply run certbot again with the  "certonly" option. 
To non-interactively renew *all* of your  certificates, run "certbot renew"

- 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

(venv)root@localhost:~/certbot#
9.如果上部出错,网络连接的话,看看用v-p-n,或者fan-qiang软件,应该是不需要的。如果连接域名问题,看看你的域名带不带www. 这个很关键,如果没有不带www的域名,申请时用的证书配置的域名与你网址域名不符合会失败的。
10.成功后的结果是,可以上证书评级 这个网站,输入你的域名,查看你申请域名的评级,一般为A以上,信任就很好了。如果评级过低,可以重新申请。
11.成功后会告诉你证书保存在/etc/letsencrypt/live/www.baidu.com下,下面配置进服务器配置文件就行。
12.这里只介绍nginx配置,apchectl自己搜索
server {

   ……………….

   listen              443 ssl;

   server_name         www.baidu.com;

   ssl_certificate     /etc/letsencrypt/live/www.baidu.com/cert.pem;

   ssl_certificate_key /etc/letsencrypt/live/www.baidu.com/privkey.pem;

   ……………………..

}

这里只写了要配置的证书cert.pem和privkey.pem,这个Server里面只有这些是不够的,其他省略的可以把配置文件上面http的配置粘贴过来。完成后保存,重启服务,OK!

13.打开网页惊喜出现,其他特殊的配置,重定向https,和强制https,可以自行搜索,网上很多
14.证书更新(默认申请的是3个月)

在cerbot 文件夹下执行
./letsencrypt-auto renew
如果报错,可以先直接执行第3步骤,类似于重新申请一样
./letsencrypt-auto-source/letsencrypt-auto

15.另外补充
//Ubuntu 启动nginx  ,下面不用多解释了吧
/etc/init.d/nginx start
nginx -s  reload
service nginx start
start  restar  stop
//服务器远程登录
ssh root@111.137.92.67

Like(喜欢)

上一篇 下一篇

猜你喜欢

热点阅读