ubuntu安装Let’s Encrypt证书实测
作者:matrix 被围观: 4,030 次 发布时间:2017-01-15 分类:兼容并蓄 零零星星 | 无评论 »
Let’s Encrypt证书出来已经有很长时间,之前用主机未到期,也就干瞪眼。
现在手上拿了一台有设备,其实算下来价格也都差不多,国外的速度是慢点,但是好处很多。
测试环境:
ubuntu 14 64bit
lnmp 1.3
获取证书
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly --standalone --email hhtjim@foxmail.com -d hhtjim.com -d www.hhtjim.com
# 若服务器已经占用80端口开启webserver则只需执行webroot:
# ./letsencrypt-auto certonly --webroot --email hhtjim@foxmail.com -d link.hhtjim.com
执行完上面三个命令之后会有图形界面出现
选择Agree之后出现这个也就完成了证书的获取
安装证书
修改域名对应下的nginx配置
进入/usr/local/nginx/conf/vhost目录找到和域名同名的conf文件
在server代码块中添加:
listen 443 ssl;
#listen [::]:80;
ssl on;
ssl_certificate /etc/letsencrypt/live/域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/域名/privkey.pem;
参考如下完整配置
server
{
listen 80;
listen 443 ssl;
#listen [::]:80;
ssl on;
ssl_certificate /etc/letsencrypt/live/hhtjim.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hhtjim.com/privkey.pem;
server_name hhtjim.com www.hhtjim.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/hhtjim.com;
include other.conf;
#error_page 404 /404.html;
include enable-php.conf;
include wordpress.conf;
if ($http_host !~ "^www.hhtjim.com$"){
rewrite ^(.*) https://www.hhtjim.com$1 permanent;
}
if ($scheme = "http"){
rewrite ^(.*)$ https://$host$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/hhtjim.com.log;
}
接着准备重启nginx:lnmp nginx restart
参考:loazuo
更新操作
按照网上大多执行的命令我这里就会出现交互界面,需要手动选择webroot目录
How would you like to authenticate with the ACME CA?
x x 1 Place files in webroot directory (webroot) x x
x x 2 Spin up a temporary webserver (standalone)
还好找到办法,只需要添加参数 --webroot -w 设置好存放web路径即可。最终参考如下
ubuntu下保存为sh文件。记得附加x执行权限。然后执行该sh文件即可自动更新证书.
#!/bin/sh
~/letsencrypt/letsencrypt-auto certonly --webroot -w /home/wwwroot/www.hhtjim.com/ --renew-by-default --email hhtjim@foxmail.com -d hhtjim.com -d www.hhtjim.com && lnmp nginx restart
若报错Failed authorization procedure...
则需要修改-w参数
为网站根目录
如:
-w /home/wwwroot/hhtjim.com/
且nginx配置中将.well-known加入白名单:
location ~ /.well-known {
allow all;
}
参考:
http://letsencrypt.readthedocs.io/en/latest/using.html
https://www.ubock.com/article/25
https://stackoverflow.com/questions/42269107/using-certbot-to-apply-lets-encrypt-certificate-failed-authorization-procedure
最后的定时执行
修改定时任务 crontab -e
0 0 1 * * /root/updateLetsEncrypt.sh
#每月1号执行sh脚本
重启定时服务 /etc/init.d/cron restart
peace