1. Home
  2. Linux
  3. Deploy Django Applications Using Nginx and uWSGI on Ubuntu 14.04
  1. Home
  2. Web Servers
  3. NGINX
  4. Deploy Django Applications Using Nginx and uWSGI on Ubuntu 14.04

Deploy Django Applications Using Nginx and uWSGI on Ubuntu 14.04

Django is a Python Web Platform of high standards that promotes fast development and clean, pragmatic design. A description on using uWSGI and nginx on Ubuntu 14.04 is given in this document.

Before You Begin

1. Get acquainted with the starting guide and finalize the steps for determining the hostname and timezone of your cloud server.

2. This guide uses an example of django. Complete securing your Server Guide sections to create a django account, improve access to SSH and disable unwanted network services. With your particular program, you need to build additional firewall rules.

3. update your system: upgrade your system

sudo apt-get update && sudo apt-get upgrade

Install nginx, Python Tools and uWSGI

1. Install the required framework packages for SQLite Python and Python Tools: SQLite Python bindings

sudo apt-get install build-essential nginx python-dev python-pip python-sqlite sqlite

2. Now Install virtualenv and virtualenvwrapper:

sudo pip install virtualenv virtualenvwrapper

Virtualenv and virtualenvwrapper are instruments for building Python environment in isolated environments. These help handle dependencies, versions and permissions easier. To function correctly with virtualenvwrapper, execute the following commands:

echo “export WORKON_HOME=~/Env” >> ~/.bashrc
echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc

3. Enable virtualenvwrapper in the current session:

source ~/.bashrc

4. Using pip to install uWSGI:

sudo pip install uwsgi

Set up a Sample Django Application

1. Make sure you are in the home directory of Django users and build the application’s virtual environment for the application :

cd /home/django && mkvirtualenv sample

Once this command has been executed, your prompt will turn to something like django@abc.com which means you are using the virtual setup sample. Type deactivate to exit the virtual environment.

2. Set up the framework for Django:

pip install Django

3. Build a new preview of the Django app on /home/django/sample:

django-admin.py startproject sample

4. Switch to the directory of the Django application and launch the database of SQLite:

cd ~/sample && ./manage.py migrate

5. It is essential to configure Django when running Django with Nginx to place all the static assets in the static folder of your application in settings.py , define your location:

echo ‘STATIC_ROOT = os.path.join(BASE_DIR, “static/”)’ >> sample/settings.py

6. To transfer all static assets in the above directory, run the following command:

./manage.py collectstatic

7. To check the sample code, open a development server:

./manage.py runserver 0.0.0.0:8080

Visit http:/abc.com:8080 on your computer browser to check the right setup and functionality of the sample program. See the check page for Django:

8. If you will get the message “It Worked,Congratulations on your first Django-powered page”

Then stop the development server with Ctrl-C.

Configure uWSGI

1. Create an uWSGI file settings directory:

sudo mkdir -p /etc/uwsgi/sites

2. Create the sample.ini configuration file with the contents below:

/etc/nginx/conf.d/abc.com.conf

[uwsgi]
project = sample
base = /home/django

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 2

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

3. Create an uWSGI upstart job:

/etc/init/uwsgi.conf

description “uWSGI”
start on runlevel [2345]
stop on runlevel [06]
respawn

env UWSGI=/usr/local/bin/uwsgi
env LOGTO=/var/log/uwsgi.log

exec $UWSGI –master –emperor /etc/uwsgi/sites –die-on-term –uid django –gid www-data –logto $LOGTO

In Emperor mode, this job will start uWSGI to track the directory of /etc/uwsgi/sites  and to create instances (vasals) for any configuration file it finds. The Emperor will immediately restart the vassals if a config file is updated.

4. Start the service uwsgi:

sudo service uwsgi start

Configure nginx

1. Delete the default nginx site configuration:

sudo rm /etc/nginx/sites-enabled/default

2. Creates your Django application’s nginx site configuration file:

/etc/nginx/sites-available/sample

server {
listen 80;
server_name abc.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/django/sample;
}

location / {
    include         uwsgi_params;
    uwsgi_pass      unix:/home/django/sample/sample.sock;
}

}

3. To allow your site configuration file, set up a symlink to the nginx sites-enabled directory:

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

4. Check and restart nginx settings:

sudo service nginx configtest
sudo service nginx restart

5. By now visiting a hostname or IP address on port 80 you should be able to access your Django program.

Was this article helpful?

Related Articles

footer shape