如何从单个Django代码库运行两个网站或一个站点的子域。
项目中的每个Django应用程序都可以在不同的域上为网站提供动力,但是所有的应用程序仍然可以共享单个pipe理界面的单个数据库。
我使用uWSGI-Nginx-Django进行部署
谢谢。
您必须在uwsgi上单独部署Django应用程序。 官方网站build议使用皇帝模式 。 首先,你必须像这样在/etc/init/uwsgi.conf写入Upstart脚本,
# 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应用程序的uwsgiconfiguration。 例如/etc/uwsgi/apps-enabled/app1.ini
这是我使用的示例configuration。
[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
/etc/nginx/sites-enabled/yourweb2.conf绑定到DjangoApp2
server_name app2.yourweb.com
有关使用nginx部署Django应用程序的更多信息