Skip to content
theiviaxx edited this page Sep 11, 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.

Required

  • Apache
  • nginx
  • Python 2.6+
  • MySQL
  • PIL

##Django Download it here, then:

tar xzvf Django-1.4.tar.gz
cd Django-1.4
sudo python setup.py install

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

cd /var/www
python django-admin.py startproject dev

This will add a dev dir and populate it with some boilerplate files. Next we'll add a static dir for handling images, javascript, and css files:

mkdir /var/www/static

##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/'

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

##PIL We need Python Imaging Library to handle image conversions so we'll use Pillow as its just an easier install that yeilds the same lib: sudo pip uninstall PIL sudo pip install pillow

##Apache & 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 traffix to apache when it's a django request and just server the static files when not. I believe this is a typical setup. So Apache will listen on port :8080 and Nginx on port :80.

Ubuntu comes packaged with apache 2 already so nothing more is required to be installed. Though we do need to configure it. So we'll need to grab nginx:

sudo aptitude install nginx

###Configure First install mod_wsgi For apache we just need to set up a virtual host on port :8080 so edit the apache.conf file:

<VirtualHost *:8080>
    DocumentRoot /var/www/dev
    WSGIScriptAlias / /var/www/dev/dev/wsgi.py
    WSGIPythonPath /var/www/dev
    
    <Directory /var/www/dev>
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>
</VirtualHost>

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      http://127.0.0.1:8080;
    }
}
...

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