Do you have several Django sites that run from a common codebase and want to run them from a single uWSGI instance (similar to virtual hosting in Apache)? This tutorial borrows heavily from Multiple Sites with Routing in uWSGI to show how to modify a standard Opalstack Django (or bare uWSGI) application to support multiple sites by using uWSGI's emperor mode and internal routing features.
To begin, create a shell user and a Django or uWSGI application via the control panel and add it to a site using the domains that you want to serve.
The rest of this tutorial uses the following values:
- Shell user name:
myuser
- Application name:
myapp
- Application port:
55555
- Domain #1:
domain1.com
- Domain #2:
domain2.com
- Project directory #1:
project1
- Project directory #2:
project2
Be sure to replace those values with your own shell user name, app name, etc when following these steps.
- Log in to a SSH session as your shell user.
- Edit
/home/myuser/apps/myapp/uwsgi.ini
to look like this:
[uwsgi]
master = True
http = 127.0.0.1:55555
daemonize = /home/myuser/logs/myapp/uwsgi.log
pidfile = /home/myuser/apps/myapp/tmp/uwsgi.pid
emperor = %dvassals.d/*.ini
route-host = ^domain1\.com$ uwsgi:/home/myuser/apps/myapp/tmp/domain1.sock,0,0
route-host = ^domain2\.com$ uwsgi:/home/myuser/apps/myapp/tmp/domain2.sock,0,0
- Create a directory named
vassals.d
to hold the uWSGI configuration files for each site:
mkdir /home/myuser/apps/myapp/vassals.d
- Create a new file
/home/myuser/apps/myapp/vassals.d/domain1.ini
with the following contents:
[uwsgi]
pidfile = /home/myuser/apps/myapp/tmp/domain1.pid
uwsgi-socket = /home/myuser/apps/myapp/tmp/domain1.sock
virtualenv = /home/myuser/apps/myapp/env/
workers = 2
threads = 2
# adjust the following to point to your project
python-path = /home/myuser/apps/myapp/domain1
wsgi-file = /home/myuser/apps/myapp/domain1/domain1/wsgi.py
touch-reload = /home/myuser/apps/myapp/domain1/domain1/wsgi.py
- Create a new file
/home/myuser/apps/myapp/vassals.d/domain2.ini
with the following contents:
[uwsgi]
pidfile = /home/myuser/apps/myapp/tmp/domain2.pid
uwsgi-socket = /home/myuser/apps/myapp/tmp/domain2.sock
virtualenv = /home/myuser/apps/myapp/env/
workers = 2
threads = 2
# adjust the following to point to your project
python-path = /home/myuser/apps/myapp/domain2
wsgi-file = /home/myuser/apps/myapp/domain2/domain2/wsgi.py
touch-reload = /home/myuser/apps/myapp/domain2/domain2/wsgi.py
- Restart the application:
/home/myuser/apps/myapp/stop
/home/myuser/apps/myapp/start
That's it - you're done! Requests for http://domain1.com
and http://domain2.com
will be routed to their respective projects, both running as workers under a single uWSGI instance.