Posts Tagged ‘mod_wsgi’

Apache and PostgreSQL (postgis) for Django deploy in Ubuntu Server 14.04 (trusty) – Hidralerta Project

The Hidralerta project is deployed at http://giss02.di.fct.unl.pt/. This posts details the server setup and application deploy procedures.

Hidralerta is deployed on a Ubuntu 14.04 LTS server. We use Apache for both the application and its static files. The application runs on PostgreSQL database, with the PostGIS extension for spatial data support.

This is the current setup:

$ uname -a
Linux giss02 3.13.0-55-generic #92-Ubuntu SMP Sun Jun 14 18:32:20 UTC 2015 x86_64 
x86_64 x86_64 GNU/Linux
$ lsb_release -a
LSB Version:    core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:core-3.0-noarch:
core-3.1-amd64:core-3.1-noarch:core-3.2-amd64:core-3.2-noarch:core-4.0-amd64:
core-4.0-noarch:core-4.1-amd64:core-4.1-noarch:security-4.0-amd64:security-4.0-noarch:
security-4.1-amd64:security-4.1-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:        14.04
Codename:       trusty

Apache setup

The server is not exclusive for this application. We’ll install LAMP to prepare for other projects that require MySQL.

$ sudo apt-get update
$ sudo tasksel

Install lamp server with tasksel.

tasksel_lamp

Install mod_wsgi

To enable Apache support for python code we’ll use mod_wsgi.

$ sudo apt-get install libapache2-mod-wsgi  

Now apache is able to load any python code with wsgi support, which in django applications is declared in the wsgi.pyfile,

Install Postgresql 9.3 and PostGIS 2.1 (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/ trusty-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   all   peer 
local   all   all   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 postgres 
$ psql
psql (9.3.2) Type "help" for help.
postgres=# \password
Enter new password:
Enter again:
postgres=# \q 

Create database and install the postgis extension.

$ psql -U postgres 
Password for user postgres: 
psql (9.3.2) Type "help" for help.
postgres=# CREATE DATABASE hidralerta;
CREATE DATABASE 
postgres=# \c hidralerta
You are now connected to database "hidralerta" as user "postgres".
hidralerta_django=# CREATE EXTENSION postgis;
CREATE EXTENSION
hidralerta_django=# \q

Setup pip and virtualenv

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

Add the following to ~/.bashrc and reload the shell:

export WORKON_HOME=$HOME/.virtualenvs
. /usr/local/bin/virtualenvwrapper.sh

Reload the terminal, and create a virtualenv for the project

$ mkvirtualenv hidralerta

Create a working copy of the application

If git is not installed:

$ sudo apt-get install git

Clone the the project files from the bitbucket repository. You may need to get authorization from the project development team.

You need to register the www-data ssh keys with bitbucket. You’ll probably need to generate www-data user set of keys:

$ sudo -u www-data ssh-keygen -t rsa

The keys are stored at /var/www/.ssh.

To build the clone:

$ cd /var/www/
$ sudo -u www-data git clone git@bitbucket.org:andresabino/hidralerta.git

Commands executed in the project directory need the sudo -u www-data prefix.

Install project dependencies

Go to the project directory and install all requirements. These are maintained in the requirements.txt file.

$ pip install -r requirements.txt 

You may need to solve some external dependencies:

  • GDAL: must be installed before all requirements – $ sudo apt-get install gdal libgdal1-dev. The python package must be built with numpy support. Please install numpy before gdal. For details, please refer to the package documentation.

Build the database

To quickly build the database, use the fabric rule:

$ fab local_reset

This will create an admin user for you, and ask for a password.

 

Setup Apache’s virtualhost for the project

Create a virtualhost for the server. This expect a local user amgs, which has the project virtualenv.

[/etc/apache2/sites-available/hidralerta.conf]

WSGIPythonPath /var/www/hidralerta/app:/var/www/hidralerta/app/lib:
/home/user/.virtualenvs/hidralerta/lib/python2.7/site-packages
WSGIScriptAlias / /var/www/hidralerta/app/hidralerta/wsgi.py

Alias /media/ /var/www/hidralerta/app/media/
Alias /static/ /var/www/hidralerta/app/static/

<Directory /var/www/hidralerta/app/static>
Require all granted
</Directory>

<Directory /var/www/hidralerta/app/media>
Require all granted
</Directory>

<Directory /var/www/hidralerta/app/hidralerta>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>

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

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