Django服务器安装ssl证书,Django网站(Apache部署)安装Let's Encrypt免费SSL证书

为什么要HTTPS

2018年2月8日,谷歌浏览器发布官方博客称,2018年7月发布的新版谷歌浏览器Chrome将把所有的HTTP网站标记为不安全。这项举措是为了促进网络安全,倡导更多网站使用HTTPS加密协议。

HTTPS,常称为HTTP over TLS,HTTP over SSL或HTTP Secure,也就是HTTP的安全升级版。HTTPS相较于HTTP,其实就是在HTTP跟TCP中间加多了一层加密层TLS/SSL。

在TSL/SSL证书机制启动的时候,当客户发送任意信息到服务器,浏览器首先根据公钥信息将内容加密后提供给服务器,然后服务器使用私钥解密数据,达到双方安全通信的目的。这样当攻击者即使篡改数据,如果没有私钥,根本无法解密和理解传输的内容。

Let’s Encrpyt是什么

Let’s Encrypt是一套新型证书管理器(简称CA),能够轻松帮助用户获取并安装免费TLS/SSL证书,并借此在Web服务器上实现HTTPS加密。官方网址:https://letsencrypt.org/

Django网站安装Let’s Encrypt SSL证书

我的网站www.zyypost.com是基于Django编写,部署在Apache和mod_wsgi下,经过一段时间的观察和了解,决定为网站安装Let’s Encrypt免费SSL证书。

先说一下环境

Ubuntu 16.04 LTS

Django 1.10.6

Apache 2.4.6

Python 3.5.1

1. 安装git,如果已经安装则忽略

$ sudo apt-get install git

2. clone letsencrpyt

$ cd /usr/local

$ sudo git clone https://github.com/letsencrypt/letsencrypt

3. 生成cert

$ cd /usr/local/letsencrypt

$ sudo ./letsencrypt-auto --apache -d example.com -d www.example.com

生成证书会有两个选项,第一个填A,第二个填N

-------------------------------------------------------------------------------

Please read the Terms of Service at

https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must

agree in order to register with the ACME server at

https://acme-v01.api.letsencrypt.org/directory

-------------------------------------------------------------------------------

(A)gree/(C)ancel: A

-------------------------------------------------------------------------------

Would you be willing to share your email address with the Electronic Frontier

Foundation, a founding partner of the Let's Encrypt project and the non-profit

organization that develops Certbot? We'd like to send you email about EFF and

our work to encrypt the web, protect its users and defend digital rights.

-------------------------------------------------------------------------------

(Y)es/(N)o: N

证书生成后存在live文件夹中,一下四个.pem存在,说明创建成功。

/etc/letsencrypt/live/zyypost.com# ls

cert.pem  chain.pem  fullchain.pem  privkey.pem  README

4. 配置apache启动SLL

Ubuntu下启用SSL模块非常简单

$ sudo a2enmod ssl

$ sudo a2ensite default-ssl.conf

$ sudo service apache2 restart

启动成功后,在:/etc/apache2/mods-enabled#中可以查看到ssl.conf配置文件,同时在/etc/apache2/ports.conf中配置监听443端口。

5. Apache SSL配置文件default-ssl.conf

ServerName www.zyypost.com

SSLEngine On

SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem

SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem

SSLCertificateChainFile /etc/letsencrypt/live/hoge.com/chain.pem

SSLOptions +StdEnvVars

SSLOptions +StdEnvVars

#####其余内容略,和普通的DjangoApache配置一样

6. http重定向配置文件

ServerName www.zyypost.com

RewriteEngine on

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://www.zyypost.com%{REQUEST_URI} [R=301,L]

7. apache打开rewrite

使上述的重定向生效,需要启动apache的rewrite模块。

$ sudo a2enmod rewrite

8. 打开443端口

这里使用的UFW防火墙,所以需要打开443端口

ufw allow 443

重启Aapche服务器

$ sudo service apache2 restart

如果您按照以上步骤耐心的配置到这里,打开您的网站试试,应该可以看到https前面的小锁标志。说明配置成功了!

9. 配置Let’s Encrypt自动更新

Let’s Encrypt免费证书有效期只有三个月,需要到期前手动更新,当然不用这么麻烦。这里写了一个脚本certbot-auto-renew.sh,脚本每天执行一次自动更新,如果证书更新成功后重启apache服务器。每执行完脚本,将日志写入renew.log

cd /usr/local/letsencrypt

sudo ./certbot-auto renew --renew-hook 'service apache2 restart' >> /var/log/letsencrypt/renew.log

exit 0

每天5:18分更新,避开10,30分整数时间以免服务器拥堵。

vi /etc/crontab

# m h dom mon dow user  command

18 5    * * *   root    /bin/certbot-auto-renew.sh

后记

第2步、第3步安装是根据网上教程摸索而来,在安装完成后,又看到一种不用安装git生成cert的方案,感觉更简单直接,也摘录到一起供大家参考吧。

安装

sudo apt-get install python-letsencrypt-apache

生成证书

sudo letsencrypt --apache certonly -d example.com -d www.example.com

参考

How to secure your Apache using Certbot SSL:https://hostpresto.com/community/tutorials/how-to-secure-your-apache-using-certbot-ssl/

Djangoで開発したWebアプリケーションをLet’s EncryptでSSL対応する(Apache編):https://gyobot.com/blog/detail/7/

Setting up SSL on a Django App with Let’s Encrypt - Ubuntu, Apache, and mod_wsgi:https://www.subevolvere.com/blog/items/4/setting-ssl-django-app-lets-encrypt-ubuntu-apache-and-mod_wsgi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值