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

问题描述 投票:1回答:1

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

我让Django跑了。

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

在我登录管理面板之前,我认为首先设置HTTPS是最佳做法。但是当我使用https://访问该网址时,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)OA \ X00 \ X00 \ x1c *À+À/À,À0Ì©À×x16 \ 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应用程序配置SSL证书(mod_wsgi)”的SO帖子中,Alexey Kuleshevich提出了一个高度响应的答案,为Apache vhosts提供了000-default.confdefault-ssl.conf的模板。见这里:Configure SSL Certificate on Apache for Django Application (mod_wsgi)

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

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

<IfModule mod_ssl.c> 
<VirtualHost *:443>
        #ServerName www.example.com
        ServerAdmin [email protected]
        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 [email protected]
        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帖子,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我得到一些赞成票,我一定会回到这里编辑这篇文章,并用锚标记来修饰我的链接。

python django ssl https mod-wsgi
1个回答
2
投票

我的解决方案是在新域上重新启动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的道路上,我遇到了一个问题,即它超时并解决了这个错误信息:

内部服务器错误

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

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

这里的问题出在libapache2-mod-wsgi上。显然这个包是为Python2编写的,而我运行的Django 2.0是Python3。因此解决方案是在官方Ubuntu repos中安装'libapache2-mod-wsgi-py3'软件包(幸运的是它包含在14.04中)。

对于它的价值,这是我的工作配置文件。

daniel496.agency-LE-ssl.conf中:

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

        ServerAdmin [email protected]
        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 [email protected]
        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

© www.soinside.com 2019 - 2024. All rights reserved.