Setup Django behind uWSGI and NGINX on CentOS 7


Setting up a web server for Django could be challenging and headache. Let’s try to make it simple: Django behind uWSGI and NGINX on CentOS 7 from scratch. At the end, our complete stack of components will look like this:

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

1. Install Dependencies

1.1. NGINX

yum install epel-release -y
yum install nginx -y

1.2. Python 3 & PIP

yum install python34-devel gcc -y
curl -O

1.3. Create VirtualEnv with Python3

pip install virtualenv
mkdir -p /var/www && cd /var/www
virtualenv -p python3 p3venv

1.4. Install uWSGI & Django

# Activate virtual environment
source p3venv/bin/activate
pip install uwsgi
pip install django

2. Configurations

2.1. Basic NGINX config

For simplest & testing purposes, let’s create NGINX server block by issuing “vi /etc/nginx/conf.d/cainterest.conf”. Any *.conf file inside this folder will be loaded as per instructed by main & default NGINX configuration (/etc/nginx/nginx.conf).

Save NGINX config and start NGINX service: systemctl start nginx

As of now, we have NGINX serves static files and by pass others to Django Server which will be configured shortly. It means you will get 502 bad gateway when accessing but this is totally fine.

2.2. Create Django project

# Make sure we are in right place
cd /var/www startproject cainterest
# Also allow domain ( or IP in Django settings (/var/www/cainterest/cainterest/

Test if they look good by starting Django Development and uWSGI server. You will get “It worked! Congratulations on your first Django-powered page.”

  • python runserver (“ctrl + c” to terminate)
  • uwsgi –http :8000 –module cainterest.wsgi (“ctrl + c” to terminate)

Alright, let’s configure uWSGI as service so we don’t have to keep terminal open.

2.3. Configure uWSGI as service

Save cainterest_uwsgi.ini file and create symlink from the default config directory to your config file

ln -s /var/www/cainterest/cainterest/cainterest_uwsgi.ini /etc/uwsgi/vassals/

Quick test if the configuration is good by start uWSGI server and navigate to You should get “It worked! Congratulations on your first Django-powered page.”

/var/www/p3venv/bin/uwsgi –emperor /etc/uwsgi/vassals

Ctrl + C to terminate uWSGI server and let’s make it runs as a service

Start uWSGI and NGINX services and you should be able to access to your Django app without having to hold terminal open.

systemctl stop nginx
systemctl start uwsgi
systemctl start nginx

Final thought

Congratulations. You’ve completed setting up NGINX, uWSGI to serve Django application. I know this is not so easy, especially when you are new to Django and uWSGI like me. It took me almost 2 weeks to search and try things out before writing this article.

I am still stuck at cainterest_uwsgi.ini with chmod-socket = 666. Whenever I change it to chmod-socket = 664, I get 502 bad gateway. If someone knows the cause and how to fix it, please let me know.

Any input or comment are more than welcomed and appreciated. So why not leave a comment now, huh?

Image credit:



About Author

A QA guy who is trying to learn some dev's skills as well as English by blogging his development notes in English. Hey, please let me know if you see any spelling mistake or I should write things in another better way.