Configuring uwsgi, nginx for django project
- 2 minsThis 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:
- Create a user named ubuntu.
adduser ubuntu
gpasswd -a ubuntu sudo
- Install the following packages
sudo apt-get install python-dev
sudo pip install uwsgi
sudo apt-get -f install nginx-full
- Clone the repository in the home directory of ubuntuuser. Assuming that the name of the project is explore-webserver.
git clone https://github.com/staranjeet/explore-webserver.git
- Configure uwsgi now. Create two folders
mkdir -p /etc/uwsgi/apps-available /etc/uwsgi/apps-enabled
- Create a config inifile for your project insites-availablefolder. Idea is to create to config files inavailablefolder and then create symlink for these inenabledfolder. The fileexplore-webserver.inishould look something like this
[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
- Create a upstart script for uwsgi in /etc/initfolder. This script is created to automatically start the service at boot plus we can manually start/stop or restart/reload the service. Let’s name this script asuwsgi.conf.
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
- Configure nginx now. Nginx also follows the sites-availableandsites-enabledconvention. In/etc/nginx/sites-availablefolder, create a file namedexplore-webserver.
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;
        }
}
- Check whether this config is correct or not by
sudo nginx -t
- Create a soft link to this file in sites-enableddirectory by
sudo ln -s /etc/nginx/sites-available/explore-webserver /etc/nginx/sites-enabled/
- Now change the user in /etc/nginx/nginx.conffromwww-dataubuntu
user ubuntu;
- Now start uwsgi by
sudo service uwsgi start
- Restart nginx by sudo service nginx restart
Note: Make sure that all directories(socket and logs) are owned by the same user(here ubuntu).
