Configuring uwsgi, nginx for django project

- 2 mins

This post is about setting up Nginx and uwsgi for a django project.

Some conventions that I have sticked to while doing so are written down here. First of all I will be creating a user named ubuntu and a group named ubuntu. This ubuntu user will be responsible for running nginx as well as uwsgi. This is necessary so that the socket created between nginx and uwsgi does not get trapped in Permission denied error. Also, I am assuming that the server is a fresh install of Ubuntu 14.04. Let’s begin with the steps:

adduser ubuntu
gpasswd -a ubuntu sudo
sudo apt-get install python-dev
sudo pip install uwsgi
sudo apt-get -f install nginx-full
git clone https://github.com/staranjeet/explore-webserver.git
mkdir -p /etc/uwsgi/apps-available /etc/uwsgi/apps-enabled
[uwsgi]
project = explore-webserver
base = /home/ubuntu
uid = ubuntu
gid = ubuntu

chdir = %(base)/%(project)
home = %(base)/%(project)/pyenv
module = %(project).wsgi:application
env = DJANGO_SETTINGS_MODULE=settings.live

master = true
processes = 3
socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true

description "uWSGI application server in Emperor mode"

start on runlevel [2345]
stop on runlevel [!2345]

setuid ubuntu
setgid ubuntu

exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/apps-enabled --daemonize /var/log/uwsgi/app/explorewebserver.log
server{
        listen 80;
        server_name server_ip_address_or_domain_name;

        location / {
                include uwsgi_params;
                uwsgi_pass unix:/home/ubuntu/explore-webserver/explore-webserver.sock;
        }

        location /static {
                root /home/ubuntu/explore-webserver;
        }

        location /media {
                root /home/ubuntu/explore-webserver/explorewebserver;
        }
}
sudo nginx -t
sudo ln -s /etc/nginx/sites-available/explore-webserver /etc/nginx/sites-enabled/
user ubuntu;
sudo service uwsgi start

Note: Make sure that all directories(socket and logs) are owned by the same user(here ubuntu).

Taranjeet Singh

Taranjeet Singh

Engineer turned Product Manager

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora