Last Updated: February 25, 2016
·
2.204K
· penpen

Multi site system on single django project with uwsgi

Multi site system on single django project with uwsgi
Yesterday I’ve set up an interesting uwsgi installation, which can serve multiple sites from single django project. Here is uWSGI Upstart script for ubuntu server:

description "uWSGI starter"

start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]

respawn

exec /usr/local/bin/uwsgi --uid penpen \
-s 127.0.0.1:9010 -M -p 2 --reload-os-env \
--logto /home/penpen/logs/biribiri/uwsgi_log \
-H /home/penpen/biribiri/virtualenv \
--pythonpath /home/penpen --vhost --vhost-host
</code></pre>

This script uses uWSGI virtual hosting mode, which allows multiple wsgi apps in single interpreter (unlike emperor mode, which uses interpreter per app (or site)).
It is necessary to use “—reload-os-env” option, which passes environment variable through UWSGISETENV parameter. In this installation nginx passes current site name in uwsgi, which loads settings from module named as biribiri.hosts.SITENAME. 
For production use, it would be a nice idea to check if file exists and use default settings for non-existant sites. 
I use DJANGOHOST instead of DJANGOSETTINGS_MODULE because in current installation it overrides somewhere. 

Nginx config:

server {
    listen 80 default;
    server_name biribiri;
    root /home/penpen/biribiri;

location / {
    uwsgi_pass 127.0.0.1:9010;
    uwsgi_param UWSGI_MODULE biribiri.production.wsgi;
    uwsgi_param UWSGI_SETENV DJANGO_HOST=biribiri.hosts.$http_host;
    include uwsgi_params;   
}


}
</code>
</pre>

wsgi.py connector for django (in biribiri.production.wsgi):

import os
import sys
path = '/home/penpen/biribiri'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGOSETTINGSMODULE'] = os.environ['DJANGO_HOST'].replace('.ru', '')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
</code></pre>

In the above code snippet we are going to remove TLD from site name, because python modules don’t allow dot in their names.

1 Response
Add your response

how to deploy 2 site like www.web1.com www.web2.com on the one IP server

over 1 year ago ·