Waliki Installation

Waliki is a simple wiki app for Django. We can write wiki contents with reStructuredText and the contents will be stored in a Git repository. In this post, I would like to introduce the instructions to install Waliki.

Getting Started

First, install the related Ubuntu packages:

$ sudo apt-get install git python3 python-virtualenv \
      libpython3-dev libxml2-dev libxslt1-dev

Second, create a new Python virtual environment:

$ mkdir waliki
$ cd waliki
$ virtualenv ENV --no-site-packages --python=/usr/bin/python3
$ source ENV/bin/activate

Third, install the Python packages:

(ENV)$ pip install waliki rst2pdf hovercraft \
                   django-sendfile django-allauth

Fourth, create a new Django project:

(ENV)$ django-admin startproject waliki_site
(ENV)$ cd waliki_site

Fifth, edit the site settings:

(ENV)$ vim waliki_site/settings.py

Add following lines to waliki_site/settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    'allauth.account.context_processors.account',
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
)

AUTHENTICATION_BACKENDS = (
    'allauth.account.auth_backends.AuthenticationBackend',
    'django.contrib.auth.backends.ModelBackend',
)

SITE_ID = 1

Add following Django apps to INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # BEGIN OF CHANGE - Extra Django Apps for Waliki
    'django.contrib.sites',
    'waliki',
    'waliki.git',
    'waliki.attachments',
    'waliki.pdf',
    'waliki.slides',
    'allauth',
    'allauth.account',
    # END OF CHANGE
)

Add following lines to waliki_site/urls.py:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'waliki_site.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),

    # BEGIN OF CHANGE
    url(r'^accounts/', include('allauth.urls')),
    url(r'^', include('waliki.urls')),
    # END OF CHANGE
)

Sixth, migrate the database:

(ENV)$ python manage.py migrate

Seventh, create a super user:

(ENV)$ python manage.py createsuperuser
Username (leave blank to use 'username'):
Email address: username@example.com
Password:
Password (again):

Finally, start the development server with:

(ENV)$ python manage.py runserver

We should be able to see Waliki at http://127.0.0.1:8000 now.

Customize the Authentication

Since I am using Waliki to take my personal notes, I don't want the wiki to be edited by the others. To disable the sign up feature, we have to customize the account adapter.

First, add following lines to settings.py:

ACCOUNT_ADAPTER = 'waliki_site.auth.AccountAdapter'

Second, create waliki_site/auth.py with following contents:

from allauth.account.adapter import DefaultAccountAdapter

class AccountAdapter(DefaultAccountAdapter):
    def is_open_for_signup(self, request):
        return False

After these changes, the sign up feature will be disabled. You can only login with account created with python manage.py createsuperuser.

Deploy Waliki with Apache WSGI

We would like to deploy our Waliki site with Apache and WSGI so that all users can have access to our Waliki.

First, we have to install Apache and its WSGI module with:

$ sudo apt-get install apache2 libapache2-mod-wsgi-py3

NOTE: AFAIK, due to the design of WSGI module, we can't install WSGI module for both Python 2.x and 3.0. It is possible to have some problem if you have installed libapache2-mod-wsgi at the same time. Remove libapache2-mod-wsgi or try to install Waliki with Python 2 if you have encountered any problems.

Second, we have to change waliki_site/wsgi.py to workaround the problem related to virtualenv:

$ vim waliki_site/wsgi.py

Add following lines to the beginning of wsgi.py and change /PATH/TO/DIR/ENV to the virtual environment:

import os
import site
import sys

ENV_DIR = '/PATH/TO/DIR/ENV'

def activate_virtualenv(env_dir):
    ver = sys.version_info
    ver = 'python' + str(ver[0]) + '.' + str(ver[1])

    # Path to executables and site-packages.
    bin_dir = os.path.join(env_dir, 'bin')
    site_packages_dir = os.path.join(env_dir, 'lib', ver, 'site-packages')

    # Add bin_dir to executable search path.
    os.environ['PATH'] = bin_dir + os.pathsep + os.environ['PATH']

    # Add site_packages_dir to the front of sys.path.
    prev_sys_path = list(sys.path)
    site.addsitedir(site_packages_dir)

    new_sys_path = []
    for item in list(sys.path):
        if item not in prev_sys_path:
            new_sys_path.append(item)
            sys.path.remove(item)

    sys.path[:0] = new_sys_path

activate_virtualenv(ENV_DIR)

Third, create a new virtual host configuration file:

$ sudo vim /etc/apache2/sites-available/waliki.conf

And add following lines and change /PATH/TO/DIR and USERNAME accordingly:

<VirtualHost *:80>
    Define name waliki_site
    Define base /PATH/TO/DIR
    Define site ${base}/${name}
    Define user USERNAME

    WSGIDaemonProcess ${name} processes=1 threads=15 user=${user} display-name=%{GROUP} home=${site} python-path=${site}:${base}/ENV/lib/python2.7/site-packages
    WSGIProcessGroup ${name}
    WSGIScriptAlias / ${site}/${name}/wsgi.py

    Alias /robots.txt ${site}/static/robots.txt
    Alias /favicon.ico ${site}/static/favicon.ico

    Alias /media/ ${site}/media/
    Alias /static/ ${site}/static/

    <Directory ${site}/static>
        Require all granted
    </Directory>

    <Directory ${site}/media>
        Require all granted
    </Directory>

    <Directory ${site}/${name}>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

Fourth, enable the site with:

$ sudo a2ensite waliki
$ sudo service apache2 reload

Finally, you should be able to see your Waliki installation!