【问题标题】:Deploying SSL for my Django 2.0.1 server with Apache on Ubuntu 14.04 (droplet)在 Ubuntu 14.04 (droplet) 上使用 Apache 为我的 Django 2.0.1 服务器部署 SSL
【发布时间】:2023-04-05 20:39:01
【问题描述】:

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

我让 Django 运行。

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

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

此网站无法提供安全连接 www.angeles4four.info 发送了无效响应。 ERR_SSL_PROTOCOL_ERROR

我服务器上的 shell 显示此消息:

[20/Jan/2018 23:54:39] "GET / HTTP/1.1" 200 16559 [21/Jan/2018 00:01:23] 代码 400,消息错误请求语法 ('\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] 您正在通过 HTTPS 访问开发服务器,但它仅支持 HTTP

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

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

在标题为“在 Apache 上为 Django 应用程序 (mod_wsgi) 配置 SSL 证书”的 SO 帖子中,Alexey Kuleshevich 高度赞成的回答建议为 Apache 虚拟主机提供 000-default.confdefault-ssl.conf 的模板。看这里: 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 推荐的建议是修改会话 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 帖子,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我得到一些支持,我一定会回到这里编辑这篇文章,并用锚标记润色我的链接。

【问题讨论】:

  • 在使用 mod_wsgi 托管 Django 时,您甚至不应该运行 python3 manage.py runserver。你为什么还在运行它?当您不运行它并实际访问 Apache 的 host:port 时会发生什么?
  • 我之所以使用 'python3 manage.py runserver' 而不是 wsgi 来运行 Django,是因为我是部署 Django 的新手。使用 mod_wsgi 关键字,我找到了有用的 DigitalOcean 指南和官方 Django 文档。我会和他们一起玩,很快就会回到这里报告我的解决方案。

标签: python django ssl https mod-wsgi


【解决方案1】:

我的解决方案是在新域上重新启动 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 提供服务的过程中,我遇到了超时问题并解决了以下错误消息:

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。请通过 coffee.drinker.daniel@gmail.com 联系服务器管理员,告知他们此错误发生的时间,以及您在此错误之前执行的操作。服务器错误日志中可能提供有关此错误的更多信息。

Apache/2.4.7 (Ubuntu) Server at www.daniel496.agency 端口 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>

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

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2015-02-08
    • 2015-08-31
    • 2014-12-02
    • 2014-10-12
    • 2017-09-03
    相关资源
    最近更新 更多