django 无法通过 HTTPS 使用 apache 和 mod_wsgi 运行

发布于 2025-01-17 16:32:10 字数 1324 浏览 3 评论 0原文

我的要求是通过 HTTPS 获取我的 django 应用程序。

我在命令的帮助下生成了 ssl 证书:

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout ssl_cert.key -out ssl_cert.crt

安装了 mod_wsgi 和 mod_ssl 并将它们配置到 httpd.conf 中:

SSLCertificateChainFile "location/to/certs/ssl_cert.cert"
WSGIScriptAlias / /location/to/django/app/wsgi.py
WSGIPythonHome /root/python3.9
#WSGIPythonPath "/root/python3.9/lib;/root/python3.9/lib/python3.9/site-packages"
WSGIPythonPath /location/to/django/app

<Directory /location/to/django/app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

并重新启动了 apache 服务。

我在 settings.py 中进行了更改:

SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

现在使用命令启动 Django 应用程序:

HTTPS=on python3 manage.py runserver 8000

它说:

Django version 2.2, using settings 'djangoApp.settings'
Starting development server at http://localhost:8000/

并且它仍然只能通过 http 访问,如果尝试通过 HTTPS 访问,它会给出错误:

This site can’t provide a secure connection

来自 debug.log:

You're accessing the development server over HTTPS, but it only supports HTTP.

有人可以帮忙吗?我缺少配置以及如何解决这个问题?

My requirement is to take my django app over HTTPS.

I generated ssl certs with the help of command:

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout ssl_cert.key -out ssl_cert.crt

Installed mod_wsgi and mod_ssl and configured them into httpd.conf as:

SSLCertificateChainFile "location/to/certs/ssl_cert.cert"
WSGIScriptAlias / /location/to/django/app/wsgi.py
WSGIPythonHome /root/python3.9
#WSGIPythonPath "/root/python3.9/lib;/root/python3.9/lib/python3.9/site-packages"
WSGIPythonPath /location/to/django/app

<Directory /location/to/django/app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

And restarted apache service.

I made changes in settings.py as:

SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

Now on starting Django App with command:

HTTPS=on python3 manage.py runserver 8000

It says:

Django version 2.2, using settings 'djangoApp.settings'
Starting development server at http://localhost:8000/

And its still accessible over http only and if trying to access over HTTPS, it gives error as:

This site can’t provide a secure connection

from debug.log:

You're accessing the development server over HTTPS, but it only supports HTTP.

Can somebody please help what I'm missing to configure and how to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文