The basics are almost identical with the uWSGI official document. Here I would like to note a few important points when setting up Nginx + uWSGI + Django on CentOS 7.
Concept
A web server faces the outside world. It can serve files (HTML, images, CSS, etc) directly from the file system. However, it can’t talk directly to Django applications; it needs something that will run the application, feed it requests from web clients (such as browsers) and return responses.
A Web Server Gateway Interface - WSGI - does this job. WSGI is a Python standard.
uWSGI is a WSGI implementation. In this tutorial we will set up uWSGI so that it creates a Unix socket, and serves responses to the web server via the WSGI protocol. At the end, our complete stack of components will look like this:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
Before you start setting up uWSGI
Create a user web
dedicated for running uwsgi and Django
As root
:
useradd web
virtualenv
As user web
, make sure you are in your home directory
cd ~
Make sure you are in a virtualenv for the software we need to install (we will describe how to install a system-wide uwsgi later):
virtualenv uwsgi-tutorial
cd uwsgi-tutorial
Django
As user web
, install Django into your virtualenv, create a new project, and cd
into the project:
pip install Django
django-admin.py startproject mysite
cd mysite
Basic uWSGI installation and configuration
Install uWSGI into your virtualenv
pip needs to compile some sources to install uwsgi, so you need development tools (gcc, make, …), libxml2 header and python headers.
As root
:
yum groupinstall "Development Tools"
yum install python2.7-devel libxml2-devel
As user web
, install uWSGI into your virtualenv
pip install uwsgi
Basic test
Create a file called test.py
:
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
# return [b"Hello World"] # python3
return ["Hello World"] # python2
Run uWSGI:
uwsgi --http :8000 --wsgi-file test.py
This should serve a ‘hello world’ message directly to the browser on port 8000. Visit:
http://127.0.0.1:8000
to check. If so, it means the following stack of components works:
the web client <-> uWSGI <-> Python
Test your Django project
Now we want uWSGI to do the same thing, but to run a Django si