Skip to content
theiviaxx edited this page Sep 15, 2012 · 24 revisions

This section is intended for those who have a brand new server or one without any django or python web server services

The following instructions are for Ubuntu 11, but the steps should be translatable to your OS of choice. It also describes a possible web server setup and not the only way to do this as your needs may differ.

I just yanked the steps from http://senko.net/en/django-nginx-gunicorn/ as he has already done a ncie job explaining the setup.

Required

  • nginx
  • MySQL
  • Python 2.6+
  • pip
  • virtualenv
  • gunicorn
  • PIL

Next we'll get virtualenv going. This simply stores everything python related in one nice little package so we don't mess up anything system wide. May seem like overkill but it does make deployment and management a lot easier.

PIP

curl http://python-distribute.org/distribute_setup.py | python
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

virtualenv

pip install virtualenv

Next, cd into wherever you want to store you web stuff, i.e. /var

virtualenv www
cd www
mkdir /var/www/static
source bin/activate
pip install django
pip install django-frog
pip install pillow
pip install south

##Project Dir First make a project dir using django, I'll name it dev:

python django-admin.py startproject dev

##MySQL I'm using MySQL, but you could use postgres if that's what you are already using. Ubuntu comes with MySQL 5, so no need to install anything further. We just need the python bindings: apt-get install python-mysqldb

Next we just need to create a database and add a user. For this tutorial, I'll use 'dev' as the database and 'django' as the user:

> mysql -u root -p
mysql> create database dev;
mysql> create user 'django'@'localhost' identified by 'django';
mysql> grant all on dev.* to 'django'@'localhost' identified by 'django';

##Django Settings Edit /var/www/dev/dev/settings.py and set the following values:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dev',
        'USER': 'django',
        'PASSWORD': 'django',
        'HOST': '',
        'PORT': '',
    }
}
MEDIA_ROOT = '/var/www/static/'
MEDIA_URL = 'http://127.0.0.1/static/'
("django.contrib.auth.context_processors.auth",
 "django.core.context_processors.debug",
 "django.core.context_processors.i18n",
 "django.core.context_processors.media",
 "django.core.context_processors.static",
 "django.core.context_processors.tz",
 "django.contrib.messages.context_processors.messages",
 "frog.context_processors.media",
)

In the INSTALLED_APPS section, add 'frog' and 'south' to the list.

You'll also need to edit the dev/urls.py file to add the frog urls:

...
url(r'^frog/', include('frog.urls')),
...

##Nginx The idea here is that nginx will sit in front of everything. Nginx is a lightweight and fast webserver, though for this tutorial, we'll just be using it to serve static files like images and javascript. So Nginx will redirect traffic to gunicorn when it's a django request and just server the static files when not. I believe this is a typical setup. So gunicorn will listen on port :8000 and Nginx on port :80.

Grab nginx:

sudo apt-get install nginx

Nginx will have a more fleshed out conf file. So edit /etc/nginx/nginx.conf. You'll only need to edit the server section of the default conf:

...
server {
    listen       80;
    server_name  dev;
    access_log   logs/www.dev.log  main;

    # serve django admin files
    # This location may differ based on your setup
    location /media  {
      alias /usr/lib/python2.7/site-packages/django/contrib/admin;
      expires 30d;
    }

    # serve static files
    location /static  {
      alias /var/www/static;
      expires 30d;
    }

    # pass requests for dynamic content
    location / {
      proxy_pass_header Server;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Scheme $scheme;
      proxy_connect_timeout 10;
      proxy_read_timeout 10;
      proxy_pass http://127.0.0.1:8000/;
    }
}
...

You should be all set, just restart the web servers and follow the install instructions for frog.

Restart apache and nginx: sudo /etc/init.d/apache2 restart sudo /etc/init.d/nginx start

Clone this wiki locally