Menu

Server_setup

Raik Gruenberg

How to set up a Labhamster server

1. Setup server operating system

  • download Ubuntu 11.10 64 bit server edition
  • create new virtual machine (VMWare / using easy_install)

2. Install SSH server and utilities

sudo apt-get install ssh
sudo apt-get install zsh emacs subversion zip unzip

Now let's switch to a more convenient shell (zsh) with cool tab-completion and other features:

cd
wget http://biskit.pasteur.fr/install/mirror/zsh_settings.zip
unzip zsh_settings.zip

which zsh
    /usr/bin/zsh

chsh
Password: 
Changing the login shell for raik
Enter the new value, or press ENTER for the default
Login Shell [/bin/bash]: /usr/bin/zsh

You can now look up the (dynamic) IP of your server with:

ifconfig

(look for "inet addr:..."). Now use this IP to ssh into the server -- which is much more convenient than working within the virtual machine window.

This setup may still not allow you to run X window programs (such as emacs). A basic xwindow system is pulled in if you install xterm.

sudo apt-get install xterm dbus-x11

(dbus-x11 removes error messages ala "No D-BUS daemon running")

3. Perform system update

Perform an online update to get the latest security patches and bugfixes (There are also options to only install security updates):

sudp aptitude update
sudo aptitude upgrade

A less conservative full upgrade would look like that:

sudo aptitude full-upgrade

A more conservative security-only upgrade would look like that:

sudo aptitude safe-upgrade

4. Set Hostname and time zone

Details are described here:
http://library.linode.com/frameworks/django-apache-mod-wsgi/ubuntu-10.04-lucid

The default hostname is "ubuntu", probably not what you want. Let's change it to "labhamster". Edit /etc/hostname or directly:

sudo echo "labhamster" >! /etc/hostname
sudo hostname -F /etc/hostname

Now make sure it is not overriden by DHCPD. If it exists, edit the file /etc/default/dhcpcd to comment out the "SET_HOSTNAME" directive:

#SET_HOSTNAME='yes'

Now edit (using vi or non-x emacs) /etc/hosts into something like this:

127.0.0.1       localhost.localdomain  localhost
127.0.1.1       labhamster.localdomain labhamster
192.168.20.129  labhamster.iric.ca     labhamster

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Now configure the time zone:

sudo dpkg-reconfigure tzdata

5, Install Server dependencies

sudo apt-get install python-setuptools postgresql libapache2-mod-wsgi 
sudo apt-get install postgresql-client
sudo apt-get install python-psycopg2 python-django python-django-south

6. Create database users/roles

Create a database user for your current login and create an empty database for django:

sudo -u postgres createuser -s $USER
createdb labhamster

Verify database setup:

psql -d labhamster
postgres=# \l
postgres=# \du

This will list databases and users with their permissions.

Configure postgresql, edit /etc/postgresql/9.1/main/postgresql.conf and uncomment two lines:

listen_addresses = '*'

password_encryption = on

Restart Database

sudo /etc/init.d/postgresql restart

In this setup, the database will be accessed with a user-role that has the same name and password as the unix user who created the database. That's perhaps not ideal.

7. Install the labhamster project

Login as 'django' and then in /home/django:

make py
cd py
svn checkout svn://svn.code.sf.net/p/labhamster/code/trunk labhamsterproject
    A   labhamsterproject/manage.py
    ...
    Checked out revision 2.

Adapt your python / django environment by adding the following lines to ~/.zshenv

export PYTHONPATH=~/py:$PYTHONPATH
export DJANGO_SETTINGS_MODULE=labhamsterproject.settings_local

Link the labhamster app into the PYTHONPATH

cd py
ln -s labhamsterproject/labhamster .

Adapt the database settings of the labhamster app. Create a copy of settings.py and name it settings_local.py. Then edit the file settings.py. The following worked for me:

DATABASES = {'default': 
         {'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'labhamster',
         'USER': 'django',
         'PASSWORD': 'insert_user_password',
         'HOST': '',
         'PORT': ''}}

Now create django databases:

python manage.py syncdb

Test the setup using the development server:

python manage.py runserver put.your.vm.ip.here:8000

replacing put.your.vm.ip.here by the IP address of your VM (see ifconfig). You can now access the django web server at put.your.vm.ip.here:8000/admin

8. Setup Apache / ModWSGI

create a configuration file for mod_wsgi in labhamsterproject/config/:

import os
import sys
sys.path += ['/home/django/py']

os.environ['DJANGO_SETTINGS_MODULE'] = 'labhamsterproject.settings_local'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Edit the apache config file /etc/apache2/sites-available/default and insert the following block into the VirtualHost:80 section:

## configuration for Django:
WSGIScriptAlias / /home/django/py/labhamsterproject/config/django.wsgi

<Directory /home/django/py/labhamsterproject/labhamster>
       Order deny,allow
       Allow from all
</Directory>
## end of custom config

(This should go after the <directory var="" www=""> ... </directory> section.)

In theory this would be it. So you can restart apache:

sudo apache2ctl restart

... and then steer your browser to the IP address of your host + /admin and Django should greet you with a (unformatted, more on that below) login.

8.1 Configure serving of static media (including page templates)

Locate the admin media files of your local django installation:

sudo updatedb
locate base_site.html

Should bring up a folder like this:

/usr/share/pyshared/django/contrib/admin/...

The media folder within contains static page elements needed for the django admin application. We can use apache to serve these files directly. First link this folder into /var/www:

sudo ln -s /usr/share/pyshared/django/contrib/admin/media /var/www

Edit /etc/apache2/sites-available/default:

Alias /media/ /var/www/media/

WSGIScriptAlias / /home/django/py/labhamsterproject/config/django.wsgi
<Directory /home/django/py/labhamsterproject/labhamster>
   Order deny,allow
   Allow from all
</Directory>

Note, the only new thing is the first line. Ensure that labhamsterproject/settings_local.py contains the following setting:

ADMIN_MEDIA_PREFIX = '/media/'

... restart apache and verify that the Django layout is fine.

8.2 Troubleshooting Database access

In practise, on ubuntu-server, I received an ugly OperationalError: complaining that the database could not be accessed by the user. Read more about postgresqp authentication issues here:
http://www.depesz.com/2007/10/04/ident/

The problem is resolved by editing pg_hba.conf (located in /etc/postgresql/9.1/main) and adding a line right after the first "local" statement:

# Database administrative login by Unix domain socket
local   all             postgres                                peer
## customization for django user
local   labhamster      all                                     trust

This basically means that all users arriving from within the server (rather than through TCP/IP from outside) can use the labhamster database without password authentification. Now restart postgrespql and apache:

sudo /etc/init.d/postgresql restart
sudo apache2ctl restart