使用 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 xxxxxx@163.com


centos :  /root/certbot-auto certonly --webroot -w /usr/local/nginx/web/ssl -d alory19x.com -d www.alory19x.com --agree-tos --email xxxxxx@163.com


--------------参数解释 :
* 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 xxxxxx@163.com



三、  配置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 服务的启用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Certbot 可以很容易地为 Django 应用程序申请 SSL 证书,以加强安全性。以下是一些步骤: 1. 安装 Certbot 在终端中运行以下命令来安装 Certbot: ``` sudo apt-get update sudo apt-get install certbot ``` 2. 获取 SSL 证书 在运行 Certbot 之前,需要确保您的域名已经指向您的服务器。运行以下命令获取 SSL 证书: ``` sudo certbot certonly --webroot --webroot-path /path/to/your/django/app/staticfiles -d yourdomain.com -d www.yourdomain.com ``` 请将“/path/to/your/django/app/staticfiles”替换为您 Django 应用程序的静态文件路径。此命令将为您的域名和 www 子域名获取 SSL 证书。 3. 配置 Django 在 Django 的 settings.py 文件中添加以下内容: ``` SECURE_SSL_REDIRECT = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True ``` 以上设置将确保 Django 应用程序只使用 HTTPS 协议,并且会将所有 HTTP 请求重定向到 HTTPS。此外,会将安全头设置为从代理服务器传递的 HTTPS 请求。 4. 配置 Web 服务器 在您的 Web 服务器配置文件中,将 HTTPS 设置为默认协议,并将 SSL 证书路径设置为 Certbot 生成的路径。 例如,在 Nginx 中,可以按如下方式配置: ``` server { listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; ... } ``` 以上设置将确保您的 Web 服务器接受 HTTPS 请求,并将 SSL 证书路径设置为 Certbot 生成的路径。 5. 重新启动 Web 服务器 最后,重新启动您的 Web 服务器以使更改生效。 现在,您的 Django 应用程序已经具有 SSL 证书,可以通过 HTTPS 访问了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值