免费SSL-HTTS 申请与配置 NGINX配置

letsencrypt

Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期。适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示。Let's Encrypt已经发布了新的工具certbot,虽然是新的工具,但是生成证书的使用方法和参数是基本一致的,证书续期更简单了。但是目前看certbot在一些老版本的Linux发行版上的兼容性还是有问题的,特别是在CentOS 5上因为python版本过低是无法用的,CentOS 6上需要先安装epel才行,当然也有很多第三方的工具你也可以自己去尝试一下。

安装方法:

例:CentOS 6/7

yum install epel-release
cd /root/ wget https://dl.eff.org/certbot-auto --no-check-certificate
chmod +x ./certbot-auto
./certbot-auto -n
#只是用来安装依赖包的

域名生成证书:
./certbot-auto certonly --email XX@qq.com --agree-tos --webroot -w /home/wwwroot/ -d www.123.net

多域名单目录生成单证书:
./certbot-auto certonly --email XX@qq.com --agree-tos --webroot -w /home/wwwroot/ -d www.123.net -d bbs.123.net

多域名多目录生成多个证书
./certbot-auto certonly --email XX@qq.com --agree-tos --webroot -w /home/wwwroot/ -d www.123.net -d bbs.123.net -w /home/wwwroot/ -d www.123.org -d lnmp.org

生成证书成功!!!!!!

证书续期

cerrbot的续期比原来的更加简单,因为证书只有90天,所以建议使用crontab进行自动续期:
crontab 里加上如下规则:

0 */12 * * * /root/certbot-auto renew --renew-hook "/etc/init.d/nginx reload"
0 */12 * * * /root/certbot-auto renew --renew-hook "service nginx reload"

*官方建议一天两次

 

 Nginx 配置

#########HTTP and HTTPS###############################

server {
listen 8080;
listen 443 ssl;
server_name she.mymlsoft.com;
root /etc/nginx/html;

ssl_certificate /etc/letsencrypt/live/she.mymlsoft.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/she.mymlsoft.com/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

#########HTTP and HTTPS###############################

#########Only for HTTPS################################

server {
listen 80;
server_name my.domain.com;
root /usr/share/nginx/html;
location / {
return 301 https://$server_name$request_uri;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

#########Only for HTTPS################################

 

证书续期

cerrbot的续期比原来的更加简单,因为证书只有90天,所以建议使用crontab进行自动续期:

crontab 里加上如下规则:0 3 */5 * * /root/certbot-auto renew --renew-hook "/etc/init.d/nginx reload" 这样每5天就会执行一次所有域名的续期操作。当然时间也可以自行进行调整,建议别太频繁,因为他们都有请求次数的限制,如果需要强制更新可以在前面命令上加上 --force-renew 参数。

注意事项:

1、因为默认LNMP的虚拟主机里是禁止 . 开头的隐藏文件及目录的,所以访问http://abc.com/.well-known/acme-challenge/**** 这个链接的话返回403错误,所以必须要将对应虚拟主机配置文件里的
location ~ /\.
{
deny all;
}
这段配置删掉或注释掉或在这段配置前面加上
location ~ /.well-known {
allow all;
}

以上配置代码,然后重启nginx。

2、如果要启用http2的话,建议编辑lnmp.conf,将里面的Nginx_Modules_Options的单引号里加上 --with-openssl=/root/openssl-1.0.2h

并执行: cd /root && wget -c https://www.openssl.org/source/openssl-1.0.2h.tar.gz && tar zxf openssl-1.0.2h.tar.gz ,然后使用升级脚本 ./upgrade.sh nginx 升级nginx至1.9.5或更高版本。

3、国内有些用户反映会卡在Installing Python packages...这个地方不动,因为pip的默认源是国外的,国内可能会有点慢,可以执行下面命令来修改pip源为国内的:
mkdir ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.doubanio.com/simple/

[install]
trusted-host=pypi.doubanio.com
EOF

执行完,再重新运行certbot的命令应该正常安装python的包了。

转自:https://www.vpser.net/build/letsencrypt-certbot.html

posted on 2016-12-16 13:58  baxk2001  阅读(910)  评论(0编辑  收藏  举报

导航