This is a brief introduction of production environment configuration for Django, uWSGI and Nginx.
If you haven’t installed
virtualenv, do it first.
Install Django, uWSGI, Nginx
Create a virtual environment, and activate it
Use pip to install Django and uWSGI
Use your package manager to install Nginx, like
apt-get install nginx,
yum install nginx, or
Django and uWSGI
Since Django is a WSGI already, it’s easy to configure. What you need to do is creat a uwsgi.ini file. Here is an example.
1 2 3 4 5 6 7 8 9
socketis for communication with Nginx.
virtualenvis the path of your virtual environment.
chdiris you project folder. For this example, assume you have created a project called mysite in folder /home/leon (
django-admin startproject mysite).
moduleis the entrance to your application. If you use the current version of Django, the
django-admin startprojectcommand should have genereated a wsgi.py file for you with an
applicationvariable in it, so just substite
mysitewith your project name.
mastermeans this uwsgi worker is master.
workersis the number of uwsgi workers.
pidfileis the pid of the running uwsgi process. You can use it to stop or reload your uWSGI server.
uWSGI and Nginx
You may would like to use a port to connect uWSGI and Nginx, but in this example, let’s use a socket, as it’s very easy as well plus it don’t need to occupy a port.(Someone says it’s more efficient with socket than a port in some systems.)
For example we use
socket=/var/run/uwsgi.sock in the uwsgi.ini file, we need to configure nginx like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
user: it’s better to use the same user as the one for the uWSGI server.
usgi_pass: the sock file to communicate with uWSGI.
- don’t forget
Start uWSGI and Nginx
(The following commands are for Gentoo system.)
Configure Nginx to autostart with system.
To start uWSGI, first use this command to test if all these work:
If everything is OK, you need to find a daemon tool to monitor uWSGI servers and restart them if error happens, like