This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu, Nginx, Gunicorn and Postgres.
The version of Ubuntu I'm using for this guide is Ubuntu 11.10 64 bit Server. I've installed Ubuntu Server in a VirtualBox VM on my MacBook Pro which is currently running Mac OS X 10.7.2. During the installation of Ubuntu Server I answered the prompts with the following:
Language: English Install Menu: Install Ubuntu Server Select a language: English Select your location: United States Configure the Keyboard: No Configure the keyboard: English (US) Configure the keyboard: English (US) Hostname: ubuntu-vm Configure the clock: Yes Partition disks: Guided - use entire disk and set up LVM Partition disks: SCSI3 (0,0,0) (sda) - 21.5 GB ATA VBOX HARDDISK Partition disks: Yes Partition disks: Continue Partition disks: Yes Set up users and passwords: Brent O'Connor Set up users and passwords: oconnor Set up users and passwords: ******** Set up users and passwords: ******** Set up users and passwords: No Configure the package manager: <blank> Configure taskse1: No automatic updates Software selection: <Continue> Install the GRUB boot loader on a hard disk: Yes Installation complete: <Continue>
Since I like to connect to my servers using SSH the first thing I install is openssh-server:
$ sudo aptitude install openssh-server
Once that's installed if your using VirtualBox you can setup port forwarding so you can connect to your server from your local Terminal. Under settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus and change the port for host to 2222 and the port for guest to 22. You should now be able to open up your Terminal and connect to your Ubuntu Server using the following...
$ ssh localhost -p 2222
This is needed in order to install python postgres binding libraries like psycopg2.
$ sudo aptitude install python2.7-dev
$ sudo aptitude install postgresql postgresql-server-dev-9.1
Configure postgres so Django can connect without a password. Edit the file /etc/postgresql/9.1/main/pg_hba.conf so the following lines in the file are identical to the following. You should be able to do this by changing all the uncommented rows in the fith column to "trust".
local all postgres trust local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust
Make your Ubuntu user a PostgreSQL superuser:
$ sudo su - postgres $ createuser --superuser oconnor $ exit
Restart PostgreSQL:
$ sudo /etc/init.d/postgresql restart
$ sudo aptitude install nginx
$ sudo aptitude install git
Setup a virtualenv:
$ cd /usr/local/ $ sudo mkdir virtualenvs $ sudo chown oconnor virtualenvs $ cd virtualenvs $ sudo apt-get install python-setuptools $ sudo easy_install pip $ sudo pip install virtualenv $ virtualenv --no-site-packages example-site $ source example-site/bin/activate
Make a location for the example site:
$ cd /srv/ $ sudo mkdir sites $ sudo chown oconnor sites $ cd sites $ git clone git://github.com/epicserve/django-base-site.git example-site $ cd example-site/ $ mkdir -p static/cache $ sudo chown www-data:www-data static/cache $ echo `pwd` > /usr/local/virtualenvs/example-site/lib/python2.7/site-packages/django_project_root.pth
Create the file /srv/sites/example-site/config/settings/local.py and add the following and save:
from base import *
LOCAL_SETTINGS_LOADED = True
DEBUG = True
INTERNAL_IPS = ('127.0.0.1', )
ADMINS = (
('Your Name', '[email protected]'),
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'example_site', # Or path to database file if using sqlite3.
'USER': 'example_site', # Not used with sqlite3.
'HOST': 'localhost',
}
}
Install the sites required python packages:
$ pip install -r config/requirements.txt
Install Gunicorn:
$ pip install gunicorn
Install psycopg2:
$ pip install psycopg2
Create a PostgreSQL user and database for your example-site:
$ createuser example_site $ Shall the new role be a superuser? (y/n) n $ Shall the new role be allowed to create databases? (y/n) y $ Shall the new role be allowed to create more new roles? (y/n) n $ createdb example_site -O example_site
Create your Upstart configuration file:
$ sudo vi /etc/init/gunicorn_example-site.conf
Add the following and save the file:
description "upstart configuration for gunicorn example-site" start on net-device-up stop on shutdown respawn exec /usr/local/virtualenvs/example-site/bin/gunicorn_django -u www-data -c /srv/sites/example-site/config/gunicorn/example-site.py /srv/sites/example-site/config/settings/__init__.py
Start the gunicorn site:
$ sudo start gunicorn_example-site
Create a new file /etc/nginx/sites-available/example-site.conf and add the following to the contents of the file:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/example-site.access.log;
error_log /var/log/nginx/example-site.error.log;
location = /biconcave {
return 404;
}
location /static/ {
root /srv/sites/example-site/;
}
location /media/ {
root /srv/sites/example-site/;
}
location / {
proxy_pass http://127.0.0.1:8001/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
}
}
Enable the new site:
$ cd /etc/nginx/sites-enabled $ sudo rm default $ sudo ln -s ../sites-available/example-site.conf
Start nginx:
$ sudo /etc/init.d/nginx start
While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site:
wget -qO- 127.0.0.1:80