WSGIDaemonProcess example.com lang='en_US.UTF-8' locale='en_US.UTF-8'
参考 Unicode 参考指引的 文件 章节获取细节信息。
使用 mod_wsgi 后台模式
"Daemon mode" 是运行 mod_wsgi 的推荐模式(在非 Windows 平台上)。为了创建必要的后台进程组并在其中运行 Django 实例,你需要添加合适的 WSGIDaemonProcess 和 WSGIProcessGroup 指令。上述配置在你使用后台模式时需要点小修改,即你不能使用 WSGIPythonPath;作为替换,你要在 WSGIDaemonProcess 中添加 python-path 选项,例如:
WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com
WSGIProcessGroup example.com
如果你想在子目录中开放你的项目(本例中 https://example.com/mysite),你可在上述配置中添加 WSGIScriptAlias:
WSGIScriptAlias /mysite /path/to/mysite.com/mysite/wsgi.py process-group=example.com
参考官方 mod_wsgi 文档获取 配置后台模式的细节。
提供文件服务
Django 本身并不提供文件,它把这项工作留给你选择的任何一个网络服务器。
我们建议使用一个单独的 Web 服务器——即一个不同时运行 Django 的服务器——来提供媒体服务。这里有一些不错的选择。
Nginx
一个 Apache 的朴素版本
然而,若你别无选择,只能在与 Django 相同的 Apache VirtualHost 上提供媒体文件,你可以让 Apache 为一些 URL 提供静态媒体服务,其它的用 mod_wsgi 接口传递给 Django。
本例在站点根目录配置 Django,但以静态文件的形式提供 robots.txt, favicon.ico 以及 /static/ 和 /media/ 中的内容。其它所有 URL 以 mod_wsgi 提供服务:
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
服务后台文件
当 django.contrib.staticfiles 在 INSTALLED_APPS 中时,Django 开发服务器会自动提供管理应用程序(以及其他任何已安装的应用程序)的静态文件。然而,当你使用任何其他服务器安排时,情况就不是这样了。你要负责设置 Apache 或你所使用的任何网络服务器,以提供管理文件。
管理文件位于 Django 分发版的 (django/contrib/admin/static/admin) 目录中。
我们 强烈 建议使用 django.contrib.staticfiles 来处理管理文件(同时使用上一节所述的网络服务器;这意味着使用 collectstatic 管理命令来收集 :setup:`STATIC_ROOT` 中的静态文件,然后配置你的网络服务器在 :setup:`STATIC_URL` 中提供 :setup:`STATIC_ROOT` 服务),但这里有三种其他方法:
在文档根目录中创建一个指向后台静态文件的符号链接(Apache 配置中可能要添加 +FollowSymLinks)。
使用前文介绍的 Alias 指令,为合适的 URL (可能是 STATIC_URL + admin/) 取个别名,指向后台文件的实际位置。
直接将后台静态文件拷贝至 Apache 的文档根目录。
Apache 利用 Django 的用户数据库进行验证
Django 提供了一个处理器,允许 Apache 直接用 Django 的认证授权后端认证用户。参考文档 mod_wsgi 认证授权文档。
JetBrains
JetBrains delivers intelligent software solutions that make developers more productive by simplifying their challenging tasks, automating the routine, and helping them adopt the best development practices. PyCharm is the Python IDE for Professional Developers by JetBrains providing a complete set of tools for productive Python, Web and scientific development.
Kraken Tech
Kraken is the most-loved operating system for energy. Powered by our Utility-Grade AI™ and deep industry know-how, we help utilities transform their technology and operations so they can lead the energy transition. Delivering better outcomes from generation through distribution to supply, Kraken powers 70+ million accounts worldwide, and is on a mission to make a big, green dent in the universe.