如何从单个 Django 代码库运行两个网站或一个网站的子域。
项目中的每个 Django 应用程序都可以为不同域上的网站提供支持,但所有应用程序仍然可以使用单个管理界面共享单个数据库。
我正在使用 uWSGI-Nginx-Django 进行部署
谢谢。
答案1
您必须在 uwsgi 上单独部署 Django 应用。官方网站建议使用皇帝模式.首先你必须写暴发户像这样的脚本/etc/init/uwsgi.conf
,
# Emperor uWSGI script
description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]
# uwsgi location
#env UWSGI=/usr/bin/uwsgi
env UWSGI=/usr/local/bin/uwsgi
env LOGTO=<your log folder>
exec $UWSGI --master --die-on-term --emperor /etc/uwsgi/apps-enabled/ --pythonpath /usr/local/lib/python2.7/dist-packages --uid www-data --gid www-data --logto $LOGTO --enable-threads
在 /etc/uwsgi/apps-enabled/ 中,它将包含每个 Django 应用程序的 uwsgi 配置。例如 /etc/uwsgi/apps-enabled/app1.ini
这是我使用的示例配置。
[uwsgi]
; define variables to use in this script
; process name for easy identification in top
project = <project name>
base_dir = /<your base directory>/
chdir = %(base_dir)
pythonpath = /usr/local/lib/python2.7/dist-packages
http = 0.0.0.0:8000
uid = www-data
gid = www-data
procname = %(project)
; Enable master mode
; uWSGI’s built-in prefork+threading multi-worker management mode, activated by flicking the master switch on. For ; all practical serving deployments it’s not really a good idea not to use master mode.
master = true
master = 1
; run master process as root
master-as-root = true
; This value needs to be tuned
workers = 4
; Create pid file for easier process management
pidfile=/run/uwsgi/%(project).pid
# Specify your Django app here
module = mysite.wsgi:application
#or
#wsgi-file = %(base_dir)/<your wsgi file>.py
log-reopen = true
logto = /<your log directory>
chmod-socket = 666
vacuum = True
enable-threads = True
# Enable stats. View using `uwsgitop localhost:4000`
stats = :4000
; unix socket (referenced in nginx configuration)
socket = /run/uwsgi/%(project).sock
要从单个 Django 代码库运行两个网站或一个网站的子域,您需要为每个 Django 应用程序在 server_name 指令中设置您的域,例如绑定到 DjangoApp1 的 /etc/nginx/sites-enabled/yourweb1.conf
server_name app1.yourweb.com
绑定到 DjangoApp2 的 /etc/nginx/sites-enabled/yourweb2.conf
server_name app2.yourweb.com