23

I'm trying do deploy a django project. I tried a lot of tutorials, but had no luck. I use a new clean Ubuntu 11.10. I've performed

apt-get install nginx
apt-get install uwsgi
service nginx start

I've created folder /deploy/project1 and put there manage.py and other files.

My current /deploy/project1/project1/wsgi.py contains:

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project1.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

So, could you tell me how to deploy my django app for domain.com properly?

I've also installed Django via pip and easy_install

What should I add in /etc/nginx/sites-enabled/default.

f1nn
  • 6,989
  • 24
  • 69
  • 92

2 Answers2

36

Assuming that you have installed all requirement and you are using the aptitude packages then you don't need the wsgi.py. All the configuration is in the uwsgi ini/xml/yaml file. (take the format that you prefer).

Here is a minimal example for example.com file for nginx(/etc/nginx/sites-available/examplecom for ubuntu 11.10)

server {
    listen      80;
    server_name example.com;
    access_log  /var/log/nginx/projectname.log;
    location /media {
        alias /vagrant/test/projectname/media/;
    }
    location /static {
        alias /vagrant/test/projectname/static/;
    }
    location / {
        uwsgi_pass unix:///run/uwsgi/projectname/socket;
        include uwsgi_params;
    }
}

Create a symbolic link to /etc/nginx/sites-enabled

sudo ln -s /etc/nginx/sites-available/examplecom /etc/nginx/sites-enabled/examplecom

or

sudo /usr/sbin/nxensite examplecom

You are done with NGINX.

Go to /etc/uwsgi/apps-available and create your ini file

sudo vim /etc/uwsgi/apps-available/projectname.ini

[uwsgi]
virtualenv=/home/vagrant/.virtualenvs/projectenv
thread=3
master=1
env = DJANGO_SETTINGS_MODULE=projectname.settings
module = django.core.handlers.wsgi:WSGIHandler()
chdir = /path/to/my/django/project
socket = /run/uwsgi/projectname/socket
logto = /var/log/uwsgi/projectname.log

Point your ini to /etc/uwsgi/apps-enabled/projectname.ini

sudo ln -s /etc/uwsgi/apps-available/projectname.ini /etc/uwsgi/apps-enabled/projectname.ini

For more information, see any of these files on your system:

/etc/uwsgi/apps-available/README
/etc/uwsgi/apps-enabled/README
/usr/share/doc/uwsgi/README.Debian.gz
/etc/default/uwsgi

You are done. You can now restart nginx & uwsgi

sudo service nginx restart
sudo service uwsgi restart

Cheers!

spazm
  • 4,399
  • 31
  • 30
ScotchAndSoda
  • 3,811
  • 4
  • 34
  • 38
  • Thanks for the great answer. I've added an edit with some ubuntu specific items (nxensite, README for /etc/uwsgi/apps-*). – spazm Oct 21 '12 at 11:10
  • Why do you use: uwsgi_pass unix:/// with 3 slashes? Is there a different using 3 or 1 leading slashes?? – cebor Dec 25 '12 at 20:00
  • There is no difference, 1 or 3 leading slashes stand for the full path to your unix socket. – ScotchAndSoda Dec 26 '12 at 17:55
9

Do not forget that Debian's, Ubuntu's and its derivates' uwsgi package does not require installation of its Python plugin — uwsgi-plugin-python, because uWSGI does not necessarily uses only Python (there are plugins for Lua, Erlang and other languages). However, Django requires Python plugin. Install it:

sudo apt install uwsgi-plugin-python

Unlike PIP's installation, you should explicitly mention uwsgi's plugin used in the app's config by adding a plugins = python line to it (if you use Python), like this:

[uwsgi]
plugins = python
uwsgi-socket = /var/sockets/django.sock
chmod-socket = 660
chdir = /home/v/django
module = project.wsgi

Unless you do this, there will be no Python-specific options available. And Debian's/Ubuntu's uWSGI will be just quiet about it!

Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
  • 2
    `plugin = python`, I think. You wouldn't *believe* how many places neglect to bring this up at all. I was migrating a python app from 1 server (which was working 100%) to another in hosted in a different location, and while I'd obviously done this ages, and ages ago, I'd completely forgotten about it. When looking for information about a uwsgi python app ini file, most places tell you to just remove the "plugin" line altogether, none mention to check to make sure that `uwsgi-plugin-python` is installed on the system. – seaders Jul 18 '16 at 11:49
  • From `man uwsgi`: `--plugins — load uWSGI plugins; --plugin — load uWSGI plugins`, so these two parameters are effectively synonyms. – Neurotransmitter Jul 18 '16 at 18:58