python - 在 Ubuntu 14.04 上使用 Apache 为我的 Django 2.0.1 服务器部署 SSL (droplet)

标签 python django ssl https mod-wsgi

我在本地玩过一点 Django (v2.0.1)。现在我正尝试在我的生产 Apache Web 服务器上实现一个测试用例。我正在运行 Ubuntu 14.04 DigitalOcean Droplet(将在今年晚些时候升级到 18.04)。

我让 Django 运行。

这是:http://www.angeles4four.info:8000/

在登录我的管理面板之前,我认为最好先设置 HTTPS。但是,当我使用 https://访问该 URL 时,Chrome 会抛出此消息:

This site can’t provide a secure connection www.angeles4four.info sent an invalid response. ERR_SSL_PROTOCOL_ERROR

我服务器上的 shell 显示了这条消息:

[20/Jan/2018 23:54:39] "GET / HTTP/1.1" 200 16559 [21/Jan/2018 00:01:23] code 400, message Bad request syntax ('\x16\x03\x01\x00Ì\x01\x00\x00È\x03\x03&6U\x10µ\x82\x97\x7f´8\x1e«\x0e¿ÿ§\x89æ\x82\r¢G§\x01ç°P%\x80)ÕÃ\x00\x00\x1c * À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x00') [21/Jan/2018 00:01:23] You're accessing the development server over HTTPS, but it only supports HTTP.

那是因为没有设置 SSL。我当前的 SSL 证书颁发机构是 Let's Encrypt。 SSL 对我的 public_html 内容运行正常,但对我最近部署的 Django 运行不正常。

我在 SO 的其他地方找到了一些用于使用 Django 设置 SSL 的资源。

在一篇标题为“在 Apache 上为 Django 应用程序 (mod_wsgi) 配置 SSL 证书”的 SO 帖子中,Alexey Kuleshevich 高度赞成的回答建议为 000-default.conf 使用模板default-ssl.conf 用于 Apache 虚拟主机。看这里: Configure SSL Certificate on Apache for Django Application (mod_wsgi)

我已尽力更改建议的值和条目,以便它们引用我的特定配置。这是我的这两个虚拟主机配置文件现在的样子。

/etc/apache2/sites-available/angeles4four.info-le-ssl.conf:

<IfModule mod_ssl.c> 
<VirtualHost *:443>
        #ServerName www.example.com
        ServerAdmin coffee.drinker.daniel@gmail.com
        ServerName angeles4four.info
        ServerAlias www.angeles4four.info
        DocumentRoot /var/www/html/angeles4four.info/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # Django Application
        Alias /static /var/www/html/angeles4four.info/public_html/Cel2FahConversion
        <Directory /var/www/html/angeles4four.info/public_html/Cel2FahConversion>
                Require all granted
        </Directory>
        <Directory /var/www/html/angeles4four.info/public_html/Cel2FahConversion>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
        WGIDaemonProcess cel python-path=/var/www/html/angeles4four.info/public_html/Cel2FahConversion/venv/bin/python3
        WSGIProcessGroup cel
        WSGIScriptAlias / /var/www/html/angeles4four.info/public_html/Cel2FahConversion/Cel2FahConversion/Cel2FahConversion/wsgi.py

        SSLCertificateFile /etc/letsencrypt/live/angeles4four.info/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/angeles4four.info/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateChainFile /etc/letsencrypt/live/angeles4four.info/chain.pem
</VirtualHost>
</IfModule>

angeles4four.info.conf:

<VirtualHost *:80>

        #ServerName www.example.com
        ServerAdmin coffee.drinker.daniel@gmail.com
        ServerName angeles4four.info
        ServerAlias www.angeles4four.info
        DocumentRoot /var/www/html/angeles4four.info/public_html
        <Directory "/var/www/html/angeles4four.info/public_html">
                Options Indexes FollowSymlinks
                AllowOverride All
                Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =angeles4four.info [OR]
RewriteCond %{SERVER_NAME} =www.angeles4four.info
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

没有骰子。我仍然得到与我最初分享的相同的回溯。

我遇到的下一篇 SO 帖子建议修改 settings.py。这是:Error "You're accessing the development server over HTTPS, but it only supports HTTP"

YoYo 在此赞成的建议是修改 session cookie 和安全 SSL 重定向。 YoYo 还建议管理基本的、本地的、生产设置,这对我来说并不适用。所以我尝试将这三行添加到我的 settings.py 中:

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True

我的 python3 manage.py runserver shell tr​​aceback 仍然说:“您正在通过 HTTPS 访问开发服务器,但它只支持 HTTP。

有什么想法吗?我还能尝试什么?

感谢您的关注。

最后一点:这是第一篇 SO 帖子,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我获得一些赞成票,我一定会返回这里编辑这篇文章,并使用 anchor 标记完善我的链接。

最佳答案

我的解决方案是在新域上重新启动 Django 配置。

它正在运行:https://daniel496.agency/

我没有意识到 Django 应该使用 wsgi 运行。我只是愚蠢地用 $ python manage.py runserver 0.0.0.0:8000 运行服务器,就像我在编写应用程序时在本地进行测试一样。这里的关键字(由 Graham Dumpleton 提供,是 mod_wsgi)。所以我找到了一个指南,叫做“How To Serve Django Applications with Apache and mod_wsgi on Ubuntu 14.04 ”。

本指南也有很大帮助: Configure SSL Certificate on Apache for Django Application (mod_wsgi)

在我“正确”服务 Django 的道路上,我遇到了一个超时的问题,并解决了这个错误消息:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at coffee.drinker.daniel@gmail.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.

Apache/2.4.7 (Ubuntu) Serverat www.daniel496.agency Port 443

这里的问题是 libapache2-mod-wsgi。显然这个包是为 Python2 编写的,而我运行的 Django 2.0 是 Python3。所以解决方案是在官方 Ubuntu 存储库中安装“libapache2-mod-wsgi-py3”包(幸运的是它包含在 14.04 中)。

不管怎样,这里是我的工作配置文件。

daniel496.agency-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerAdmin coffee.drinker.daniel@gmail.com
        ServerName daniel496.agency
        ServerAlias www.daniel496.agency
        DocumentRoot /var/www/html/daniel496.agency/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # Django project
        Alias /static /home/<user>/cel2fah/static
        <Directory /home/<user>/cel2fah/static>
                Require all granted
        </Directory>

        <Directory /home/<user>/cel2fah/cel2fah>
                <Files wsgi.py>
                    Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess cel2fah2 python-path=/home/<user>/cel2fah python-home=/home/<user>/cel2fah/venv
        WSGIProcessGroup cel2fah2
        WSGIScriptAlias / /home/<user>/cel2fah/cel2fah/wsgi.py

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/daniel496.agency/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/daniel496.agency/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/daniel496.agency/chain.pem

</VirtualHost>
</IfModule>

还有 daniel496.agency.conf:

<VirtualHost *:80>
        ServerAdmin coffee.drinker.daniel@gmail.com
        ServerName daniel496.agency
        ServerAlias www.daniel496.agency
        DocumentRoot /var/www/html/daniel496.agency/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfDefine IgnoreBlockComment>
        Alias /static /home/<user>/cel2fah/static
        <Directory /home/<user>/cel2fah/static>
                Require all granted
        </Directory>

        <Directory /home/<user>/cel2fah/cel2fah>
                <Files wsgi.py>
                    Require all granted
                </Files>
        </Directory>

#       WSGIDaemonProcess cel2fah2 python-path=/home/<user>/cel2fah python-home=/home/<user>/cel2fah/venv
 #      WSGIProcessGroup cel2fah2
#       WSGIScriptAlias / /home/<user>/cel2fah/cel2fah/wsgi.py
</IfDefine>
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =daniel496.agency [OR]
        RewriteCond %{SERVER_NAME} =www.daniel496.agency
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我在阅读文档中找到了各种新的、详细的资源以供进一步阅读,以帮助使用更好的实践配置 Django,例如 putting my wsgi scripts inside /usr/local/www/wsgi-scripts/ instead of in my project folder in my home directory .

关于python - 在 Ubuntu 14.04 上使用 Apache 为我的 Django 2.0.1 服务器部署 SSL (droplet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48362856/

相关文章:

mysql - 通过 https 连接到 MySQL

python - 无法在 docker 内创建套接字

python - 如何在 autopy 或 pyautogui 中不释放按键?

python - kivy:单击 1 个按钮即可触发多个功能

django - 使用默认 LOGIN_URL 时,django 是否考虑 SCRIPT_NAME

python - django Rest框架如何验证对象列表?

ios - 卡在为 MoonAPNS 创建 p12 文件

python - 每个赛季篮球队/比赛/得分的数据库。表结构

python - 将外部 mysql 数据库连接到 django 应用程序并对其进行查询

ssl - 如何在谷歌云平台上安装 Godaddy SSL 证书