How to Deploy Python 3 Django WSGI Applications Using uWSGI Web Server with Nginx on CentOS 7

I use CentOS 7 as the server operating system, and use ssh to access the server by command ssh root@192.**.***.1

Preparing Droplet for Production
In this section, we are going to prepare our virtual server for production (i.e. for deploying our application).
We primarily need:

  • updating the default operating system
  • downloading and installing Python and common Python tools (i.e. pip, virtualenv)
  • creating a virtual environment to contain the application
Updating the default operating system
yum -y update
yum install -y epel-release
Install Python 3.3
In CentOS, There is no Python3 rpm package in EPEL, we need manually install Python 3.3 and we need some pakages before installing Python.
yum install -y openssl-devel sqlite-devel bzip2-devel
yum install -y zlib zlib-devel libjpeg-devel libpng12-devel
yum install git wget curl
tar -xvf Python-3.3.3.tar.xz && cd Python-3.3.3
make && make altinstall

Install Pip and Virtualenv
pip install virtualenv

Creating a self-contained Virtual (Python) Environment
now  create a virtual environment
virtualenv --python=python3.3 --no-site-packages env
cd env && source bin/activate

Downloading and Installing uWSGI and some packages
pip install uwsgi
pip install django
pip install pytz pillow

Download our project from bitbucket or github. use Git
git clone
cd neweco && python runserver ip_address:8000
visit: http://ip_address:8000 to check.
We can test it by creating a file called
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
uwsgi --http :8000 --wsgi-file

This should serve a ‘hello world’ message directly to the browser on port 8000. Visit: to check. If so, it means the following stack of components works:
the web client <-> uWSGI <-> Python

Now we want uWSGI to do the same thing, but to run a Django site instead of the module.
uwsgi --http :8000 --module mysite.wsgi

Point your browser at the server; if the site appears, it means uWSGI is able to serve your Django application from your virtualenv, and this stack operates correctly:
the web client <-> uWSGI <-> Django

Now normally we won’t have the browser speaking directly to uWSGI. That’s a job for the webserver, which will act as a go-between.
nginx installation and configuration
yum instlall -y  nginx
systemctl start nginx

And now check that the nginx is serving by visiting it in a web browser on port 80 - you should get a message from nginx: “Welcome to nginx!”. That means these components of the full stack are working together:
the web client <-> the web server  

Now create a file called neweco.conf in /etc/nginx/conf.d/, so nginx can see it .In my case put this in the file:

# the upstream component nginx needs to connect to
upstream django {
    #server unix:///home/neweco/env/neweco/neweco.sock; # for a file socket
    server; # for a web port socket (we'll use this first)
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name; # substitute your machine's IP address or FQDN
    charset     utf-8;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {        alias /home/neweco/env/neweco/media;  # your Django project's media files - amend as required
    location /static {        alias /home/neweco/env/neweco/static2; # your Django project's static files - amend as required
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;        include     /home/neweco/env/neweco/uwsgi_params; # the uwsgi_params file you installed

You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from
Copy it into your project directory. In a moment we will tell nginx to refer to it.

Just create this script and make it executable
mkdir /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/conf.d/neweco.conf /etc/nginx/sites-enabled/

Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit neweco/ adding:

STATIC_ROOT = os.path.join(BASE_DIR, "static2/")
and then run
python collectstatic
Make sure we need to comment the added line after collecting or the django project won't work.
Restart nginx to check that media files are being served correctly, add an image called media.png to the /path/to/your/project/project/media directory, then visit - if this works, you’ll know at least that nginx is serving files correctly.

Let’s get nginx to speak to the “hello world” application.
uwsgi --socket :8001 --wsgi-file
Visit: to check. And this is our stack:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
Now we can deploy the django app.

uwsgi --socket :8001 --module neweco.wsgi
Create a file called neweco_uwsgi_production.ini:
# neweco_uwsgi_production.ini file

# Django-related settings
# the base directory (full path)
chdir           = /home/neweco/env/neweco
# Django's wsgi file
module          = neweco.wsgi
# the virtualenv (full path)
home            = /home/neweco/env

# process-related settings
master          = true
pidfile         = /tmp/
# maximum number of worker processes
processes       = 5
# the socket
socket          = :8001
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum           = true
# background the process

daemonize        = /home/neweco/env/neweco/neweco.log

This .ini file also makes sure that the uwsgi can serve the app even outside virtualenv.Now run uswgi using this file:
uwsgi --ini neweco_uwsgi_production.ini

Once again, test that the Django site works as expected. And we can manage uwsgi process in a more gentle way:
uwsgi --stop /tmp/
uwsgi --reload /tmp/

check uWSGI and nginx documentation
now restart nginx server
systemctl restart nginx
visit: http://ip_address/ or to check.
django app is successfully deployed!

About Lasha Gogua

    Blogger Comment
    Facebook Comment


  1. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Installment loans
    Payday loans
    Title loans

  2. Great articles, first of all Thanks for writing such lovely Post! Earlier I thought that posts are the only most important thing on any blog. But here at Shoutmeloud I found how important other elements are for your blog.Keep update more posts..
    RFID Solutions
    Athletic Management Software
    CCTV Camera Dealers in Chennai
    CCTV Camera Installation in Chennai