This article describes one of the ways to deploy Django-projects, in conjunction Nginx + uWSGI on Unix-like operating systems. Testing, as well as further operational maintenance was performed on FreeBSD, but on Linux the process will be similar, except for a few minor points.
Introduction
One day, on the job site, it became necessary to launch Web applications written in Django (Python). During the search, a large number of similar articles were found describing the launch of Django projects using Nginx + uWSGI. The authors considered the launch of one particular Django project, which had its own xml configuration file for uWSGI worker (s), as well as a separate socket / port for interacting with Nginx. As a result, when adding a new Django project, it will be necessary to reconfigure the uWSGI worker (s) already for this application (even if using copy-paste, but still). Unfortunately, this method is not universal, and with this configuration, there is no single, familiar
FreeBSD to me, means of controlling a running daemon, a single management point. As a result, after searching in the Internet and studying the official wiki of the uWSGI project, the following launch method was developed (fundamental differences):
- Uniform, generalized configuration xml-file for uWSGI-worker (s);
- The specific parameters described for each Django project in the xml configuration file for the uWSGI worker (s) are transferred to the Nginx locations and are transferred to the NWex UWSGI workers.
Application Directory
It is assumed to use a single directory in which a conglomerate of Django projects will be deployed, in my case this is / usr / local / www. For example, the contents of the directory will be something like:
# ls -la /usr/local/www/
...
drwxr-xr-x ... app1
drwxr-xr-x ... app2
drwxr-xr-x ... appN
-rw-r--r-- ... webapp.xml
Nginx setup
Setting up locations is done on the basis of the principle that one Django project is one location.
nginx.conf:
location / {
uwsgi_pass unix: ///tmp/uwsgi.sock;
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT webapp;
uwsgi_param UWSGI_CHDIR / usr / local / www / app1;
}
location / app2 {
uwsgi_pass unix: ///tmp/uwsgi.sock;
include uwsgi_params;
uwsgi_param SCRIPT_NAME / app2;
uwsgi_param UWSGI_SCRIPT webapp;
uwsgi_param UWSGI_CHDIR / usr / local / www / app2;
}
location / appN {
uwsgi_pass unix: ///tmp/uwsgi.sock;
include uwsgi_params;
uwsgi_param SCRIPT_NAME / appN;
uwsgi_param UWSGI_SCRIPT webapp;
uwsgi_param UWSGI_CHDIR / usr / local / www / appN;
}
In this configuration location:
Webapp.xml configuration file
The configuration xml file for uWSGI worker (s) webapp.xml is located in the / usr / local / www directory.
webapp.xml:
<uwsgi>
<socket> /tmp/uwsgi.sock </ socket>
<process> 1 </ process>
<master />
<enable-threads />
<uid> 80 </ uid>
<gid> 80 </ gid>
<pidfile> /var/run/uwsgi.pid </ pidfile>
</ uwsgi>
Django Project Entry Point
It is important that in each app1, app2, appN directory there is a webapp file, which is the entry point to the Django project. The uWSGI-worker runs the webapp, and the webapp in turn starts the WSGI handler in the Django project.
')
This is what the webapp for app1 looks like:
# - * - coding: iso-8859-1 - * -
import sys, os
import django.core.handlers.wsgi
sys.path.insert (0, '/ usr / local / www / app1')
os.environ ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = django.core.handlers.wsgi.WSGIHandler ()
Instead of conclusion
At this setting ends. Now, to run uWSGI-worker (s) serving all Django-projects, just run the following command:
# uwsgi -s /tmp/uwsgi.sock -x /usr/local/www/webapp.xml
On FreeBSD, the launch can be organized more beautifully than on Linux:
/etc/rc.conf:
uwsgi_enable = "YES"
uwsgi_flags = "- x /usr/local/www/webapp.xml"
# / usr / local / etc / uwsgi start
With this configuration, just unpack the Django project into the working directory, add the template location to Nginx and restart the Nginx and uWSGI daemons.
Used literature blogs
projects.unbit.it/uwsgi/wiki/RunOnNginxprojects.unbit.it/uwsgi/wiki/Exampleprojects.unbit.it/uwsgi/wiki/Emperorhttp://wiki.diphost.ru/Category:WSGIblog.zacharyvoase.com/2010/03/05/django-uwsgi-nginxwww.westphahl.net/blog/2010/4/8/running-django-nginx-and-uwsgiposterous.adambard.com/start-to-finish-serving-mysql-backed-django-wbrandonkonkle.com/blog/2010/sep/14/django-uwsgi-and-nginxwww.jeremybowers.com/blog/post/5/django-nginx-and-uwsgi-production-serving-millions-page-viewswww.cherokee-project.com/doc/cookbook_uwsgi.html