Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Last active June 27, 2017 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save simkimsia/9ba75e8d69162a19c9d9 to your computer and use it in GitHub Desktop.
Save simkimsia/9ba75e8d69162a19c9d9 to your computer and use it in GitHub Desktop.
How to setup Ubuntu 14.04 for Python Django

How to setup Ubuntu 14.04 server for Python Django Web App

Assuming you are in root

Make sure the locale is set to UTF-8

locale-gen UTF-8

Create a .bash_profile in the ~/ with the following entries

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Update your packages repository apt-get update --force-yes -y && apt-get upgrade --force-yes -y

We will be using uwsgi, nginx, virtualenv.

What's nginx? It is a web server software like apache. What's uwsgi? It is a swiss army knife for all kinds of network applications What's virtualenv? It allows virtual environment for what you need to run your applications.

Why nginx and uwsgi? Because nginx is faster than apache web server. Uwsgi is needed for nginx to work with python files.

Why virtualenv? Because we may need to have different versions of Python to run different apps.

You need to realize that

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

is how things work.

Install all python 2.7 required libraries to have virtualenv

apt-get install python-virtualenv --force-yes -y ## for virtualenv
apt-get install python-virtualenv --force-yes -y ## for virtualenv
apt-get install python-setuptools --force-yes -y ## for python2.7 or above
apt-get install python-dev --force-yes -y ## because ubuntu 14.04 does not have dev version of python 2
apt-get install build-essential --force-yes -y ##

I assume nginx is installed and www-data is the user for nginx

Create a folder to hold all the virtualenvs

Using www-data as user, mkdir virtualenvs under ~/

Now we create a single python 2.7 environment

Inside virtualenvs, mkdir py2.7

Inside virtualenvs, virtualenv -p /usr/bin/python2 py2.7 In English, use this folder ~/virtualenvs/py2.7 as the virtual env holder for python2 libraries. The python2 libraries are in /usr/bin/python2.

To turn on the virtual env for python2, go inside ~/virtualenvs/py2.7 and run source/bin activate.

You will notice a (py2.7) in front of your command line.

Something like this

(py2.7)www-data@server:

To turn off, simply run deactivate anywhere in the file system.

Configure uwsgi.ini for django apps

  • Where to put the uwsgi.ini?

Typically you need to place uwsgi.ini inside your django project folder. In other words, it should be in the same folder as the manage.py

Let me show you a sample django app project folder setup.

weasyprint_site
    |---------db.sqlite3
    |---------manage.py
    |---------test.py
    |---------uwsgi.ini
    |---------weasyprint_site
                    |---------settings.py
                    |---------urls.py
                    |---------wsgi.py
  • How to configure uwsgi.ini?

This is the sample uwsgi.ini for the above folder structure.

[uwsgi]

socket=/tmp/uwsgi.sock
chmod-socket=666
uid = www-data
gid = www-data

chdir=/var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasyprint_site
virtualenv=/var/virtual/WebApps/virtualenvs/WeasyPrintProject
module=weasyprint_site.wsgi:application
master=true
pidfile=/var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasy_print.pid
vacuum=true

Let me run through each line in the uwsgi.ini

socket this should be the socket meant for the uwsgi. For ubuntu 14.04, it should be /tmp/uwsgi.sock

chmod-socket permissions on the socket. Typically use 666

uid user id of the operating system user account that will execute the socket. Because we are using nginx, it should be www-data

gid group id of the operating system user account that will execute the socket. Because we are using nginx, it should be www-data

chdir which django app to chdir into. in this case, the path is to the folder where the manage.py is at

virtualenv the folder where the virtualenvs is

module the Django app module

master no idea what this is. Just set as true.

pidfile no idea what this is. Just set as to the django app pid file

vacuum no idea what this is. just set as true.

  • how to configure nginx?

Typically nginx after installed into ubuntu, will have two folders.

/etc/nginx/sites-available and /etc/nginx/sites-enabled

Typically, you use root user to create a .conf file inside sites-available

Then as root, you create a symbolic link inside sites-enabled

and then you restart the nginx service. In Ubuntu this is usually

service nginx restart

In this case, I created a django.conf inside sites-available and the corresponding symbolic link inside sites-enabled.

# nginx.conf
# inside the config file, the # sign is for comments

# this is uwsgi unique
upstream django {
    server unix:///tmp/uwsgi.sock; # this is pointing to the uwsgi socket we saw earlier.
}

# this is more nginx related
# you need to type something like this even if you don't use uwsgi
server {
    listen              80;
    server_name         weasyprint.django.dev; # this refers to the domain url you expect.
    charset             utf-8;
    error_log           /var/log/nginx/django-weasyprint.log;

    # this is more uwsgi related
    # not too sure what they mean, but it works.
    location / {
        uwsgi_pass      django;
        include         /etc/nginx/uwsgi_params;
    }
}
  • How to run uwsgi and the django app

Go to the folder holding the uwsgi.ini

uwsgi --ini uwsgi.ini

When you run this you will notice that the uwsgi is started and you cannot do anything else unless you kill it.

I suggest screen inside Ubuntu 14.04

sudo apt-get update
sudo apt-get install screen

type screen so you can create a new terminal window.

Now go to the start the virtualenv you wanted.

source bin/activate

at the desired virtualenv.

Followed by

uwsgi --ini uwsgi.ini

at the right folder.

To go back to the previous window, you type

Ctrl+a followed by c

Ctrl+a followed by n to go back to window where you were running the uwsgi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment