在 Ubuntu 14.04(droplet)上使用 Apache 为我的 Django 2.0.1 服务器部署 SSL
Deploying SSL for my Django 2.0.1 server with Apache on Ubuntu 14.04 (droplet)
我在本地玩过一点 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 应用程序配置 SSL 证书 (mod_wsgi)”的 SO post 中,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>
没有骰子。我仍然得到与我最初分享的相同的回溯。
我遇到的下一个SOpost建议修改settings.py。这是:
YoYo 在此赞成的建议是修改 session cookie 和安全 SSL 重定向。 YoYo 还建议管理基本的、本地的、生产设置,这对我来说并不适用。所以我尝试将这三行添加到我的 settings.py:
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
我的 python3 manage.py runserver shell traceback 仍然说:“你正在通过 HTTPS 访问开发服务器,但它只支持 HTTP。”
有什么想法吗?我还能尝试什么?
感谢您的关注。
最后一点:这是第一个 SO posts,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我得到一些赞成票,我一定会return在这里编辑这个post并用锚标记完善我的链接。
我的解决方案是在新域上重新启动 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 服务的路上 "properly" 我遇到了一个超时的问题并解决了这个错误消息:
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 编写的,而我 运行ning 的 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。
我在本地玩过一点 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 应用程序配置 SSL 证书 (mod_wsgi)”的 SO post 中,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>
没有骰子。我仍然得到与我最初分享的相同的回溯。
我遇到的下一个SOpost建议修改settings.py。这是:
YoYo 在此赞成的建议是修改 session cookie 和安全 SSL 重定向。 YoYo 还建议管理基本的、本地的、生产设置,这对我来说并不适用。所以我尝试将这三行添加到我的 settings.py:
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
我的 python3 manage.py runserver shell traceback 仍然说:“你正在通过 HTTPS 访问开发服务器,但它只支持 HTTP。”
有什么想法吗?我还能尝试什么?
感谢您的关注。
最后一点:这是第一个 SO posts,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我得到一些赞成票,我一定会return在这里编辑这个post并用锚标记完善我的链接。
我的解决方案是在新域上重新启动 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 服务的路上 "properly" 我遇到了一个超时的问题并解决了这个错误消息:
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 编写的,而我 运行ning 的 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。