Apache and PostgreSQL (postgis) for Django deploy in Ubuntu Server 12.04 (precise)

$ uname -srvm

Linux 3.8.0-35-generic #50~precise1-Ubuntu SMP Wed Dec 4 17:25:51 UTC 2013 x86_64

$ sudo apt-get update
$ sudo tasksel

Install lamp server with tasksel.

Install mod_wsgi.

$ sudo apt_get install libapache2-mod-wsgi

Install Postgresql 9.3 (http://www.postgresql.org/download/linux/ubuntu/)

Create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository.

deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main

Import the repository signing key, and update the package lists.

$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install postgresql-9.3 postgresql-9.3-postgis-2.1

Configure PostgreSQL.

$ sudo vim /etc/postgresql/9.3/main/pg_hba.conf

#local   all             postgres                                peer
local   all             postgres                                md5

$ sudo service postgresql restart

If you are having problems with the access, set the config to local all trust. It enables direct access from the local machine. Change it to md5 after the password is reset.

$ sudo passwd postgres
$ sudo su -u postgres
$ psql

psql (9.3.2)
Type "help" for help.

postgres=# \password
Enter new password:
Enter it again:
postgres=# \q
$ exit

Create database and install postgis extension.

$ psql -U postgres
Password for user postgres:
psql (9.3.2)
Type "help" for help.

postgres=# CREATE DATABASE APP_NAME_django;
postgres=# \q
$ psql -U postgres APP_NAME_django
Password for user postgres:
psql (9.3.2)
Type "help" for help.

APP_NAME_django=# \q

Install and update pip.

$ sudo apt-get install python-pip
$ sudo pip install -U pip

Install Django and dependencies.

$ pip install django
$ sudo pip install django-bootstrap-toolkit django-chartit django-tables2

Create a location for the webapps, e.g., /home/www/

$ ls -lh /home/ | grep -i www
drwxr-xr-x 3 USERNAME     www-data 4.0K www

Create a root file.
$ echo -e '<!DOCTYPE html>\n<html>\n<head>\n\t<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n\t<title>TITLE</title>\n</head>\n\t<body>\n\t\t<h1>TEXT</h1>\n\t</body>\n</html>' > /home/www/index.html

Create the app directory.

$ mkdir /home/www/APP_NAME

Move all the files into the app directory: /home/www/APP_NAME
Create/change the wsgi.py file.

WSGI config for APP_NAME project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see

import os, sys
if not sys.platform == 'win32': #production

os.environ["DJANGO_SETTINGS_MODULE"] =  "APP_NAME.settings"

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Sync the app with the database.

$ python manage.py syncdb

Create a virtualhost for the server.

$ sudo vim /etc/apache2/sites-available/www.example.com

  DocumentRoot /home/www/

  Options FollowSymLinks
  AllowOverride None

  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all

WSGIScriptAlias /APP_NAME /home/www/APP_NAME/APP_NAME/wsgi.py

  Order deny,allow
  Allow from all

  Alias /static/admin/ "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/"
  Alias /static/ /home/www/APP_NAME/static/
<Location "/static/">
  Options -Indexes

Alias /media/ /home/www/APP_NAME/media/

  Order deny,allow
  Allow from all

Change settings.py

if sys.platform == 'win32': # development
    DEBUG = True
else: # production

Disable the default side, install the new configuration and reaload.

$ sudo a2dissite 000-default
$ sudo a2ensite www.example.com
$ sudo services apache2 reload
Tagged , , , , ,