使用 certbot 安装免费的安全证书

一 、安装certbot


乌班图系统 :
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx


========================================================================
如果是CentOS 6、7,先执行:yum install epel-release


cd /root/
wget https://dl.eff.org/certbot-auto --no-check-certificate
chmod a+x ./certbot-auto
./certbot-auto -n
./certbot-auto -n  是用来安装依赖包的,注意 :需要python2.7 以上


注意 :国内有些用户反映会卡在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






 二 、 获取证书
 
  2.1 、单域名 :
  
乌班图 : certbot certonly --webroot -w /var/www/ssl -d alory198.com -d www.alory19x.com --agree-tos --email [email protected]


centos :  /root/certbot-auto certonly --webroot -w /usr/local/nginx/web/ssl -d alory19x.com -d www.alory19x.com --agree-tos --email [email protected]


--------------参数解释 :
* certonly  只获取证书
* --webroot  以webroot插件去获得证书
* -w  需配合--webroot参数一起使用,用来指定网站根目录
* -d   指定域名
* --agree-tos  用意ACME用户协议(如果省略此项,则在命令执行过程中会询问是否同意)
* --email 指定邮箱用来接收一些通知(如果省略此项,则在命令执行过程中会要求填写)




2.2 、多域名:


如果有多个域名,则按照一个 -w  /var/www/example  后接一个 -d example.com  的形式继续输入。


 范例: certbot certonly --webroot -w /var/www/ssl -d www.alory19x.com -d alory19x.com  -w /var/www/ssly -d alory19y.com -d alory19y.com
 
 
2.3 、注意事项:


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


2.4、根目录不在本机的域名的获取方法


2.4.1 配置certbot 需要验证的目录到本机的 某个目录,
列如 配置 到/opt/ssl 目录


location ~ /.well-known {
root /opt/ssl;               
### 配置certbot 需要验证的目录到本机的/opt/ssl 目录
}
location ~ /.well-known {
allow all;
}


2.4.2 使用 --standalone
   但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone , 这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。


列如  :./certbot-auto certonly --standalone -dalory19x.com -d alory19x.com --agree-tos --email [email protected]



三、  配置nginx 


证书生成成功后,会有 Congratulations 的提示,并告诉我们证书放在 /etc/letsencrypt/live/ 这个目录下面对应的位置,可以在nginx里面配置


server
{
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/alory19x.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/alory19x.com/privkey.pem;
#这里 证书的位置会 有变化,要配置成自己的路径。

    root /home/guest/test;
    server_name alory19x.com;
    index index.html index.htm;
}
server
{
    listen 80;
    server_name alory19x.com;
    return      301 https://$host$request_uri;


}


===============================需注意:以上操作都是在一台服务器上进行,而不是执行命令是一台机,域名是另外一台机。=====================




四 、证书续期


乌班图 :certbot renew   证书只有90天有效期,使用此命令自动更新。
centos /root/certbot-auto renew   证书只有90天有效期,使用此命令自动更新。


这里当是反向代理的域名的时候会报错,因为我的域名生成证书的时候使用的是 --standalone 模式,验证域名的时候,需要启用443端口,这个错误的意思就是要启用的端口已经被占用了。这时候我必须把nginx先关掉,才可以成功。


证书是90天才过期,我们只需要在过期之前执行更新操作就可以了。 这件事情就可以直接交给定时任务来完成。linux 系统上有 cron 可以来搞定这件事情。
下面这段内容的意思就是 每隔 两个月的 凌晨 3:35 执行 更新操作。


35 3 * */2 * /root/certbot-auto renew --pre-hook "/usr/local/nginx/sbin/nginx -s stop" --post-hook "/usr/local/nginx/sbin/nginx"


35 3 * */2 * /root/certbot-auto renew --force-renew "/usr/local/nginx/sbin/nginx -s stop" --post-hook "/usr/local/nginx/sbin/nginx"   此命令为强制更新证书。

参数解释:
--pre-hook 这个参数表示执行更新操作之前要做的事情,因为我有 --standalone 模式的证书,所以需要 停止 nginx 服务,解除端口占用。
--post-hook 这个参数表示执行更新操作完成后要做的事情,这里就恢复 nginx 服务的启用

猜你喜欢

转载自blog.csdn.net/qq_35751770/article/details/78018613