Configure bottle framework for Apache

Follow these steps to port over your local bottle framework onto an apache server that would run on http 80

1. Install the bottle framework on the server

2. Install mod_wsgi on the server

sudo apt-get install libapache2-mod-wsgi

This will install a .so module in Apache’s module directory:

/usr/lib/apache2/modules/mod_wsgi.so

It will also automatically configure Apache to load the mod_wsgi module upon restart. You can confirm the presence of the module in Apache’s available modules directory… as well as Apache’s enabled modules directory:

/etc/apache2/mods-available/wsgi.conf
/etc/apache2/mods-available/wsgi.load

/etc/apache2/mods-enabled/wsgi.conf
/etc/apache2/mods-enabled/wsgi.load

More details about installation and configuration on http://webpy.org/cookbook/mod_wsgi-apache-ubuntu

3. Under the apache configuration – add a new file under sites-enabled and call this restapi. The contents of the restapi file would be like this:

<VirtualHost *:80>
        ServerName      server.domain.com
        ServerAlias     www.your-alias.domain.com
        ServerAlias     www.your-alias.domain.net
        ServerAlias     www.your-alias.domain.pvt

    DocumentRoot    /var/www

        WSGIScriptAlias /               /var/www/restapi/restapi.wsgi
        WSGIDaemonProcess       restapi    threads=25
        WSGIScriptReloading     Off


    <Directory /var/www>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /var/www/restapi>
        WSGIProcessGroup restapi
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

4. Navigate to the restapi directory and create a file called restapi.wsgi. The contents of the restapi.wsgi should be something like this:

import sys, os, bottle

sys.path = ['/var/www/restapi/'] + sys.path

# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))

import apiserver         # This loads the REST framework that you have implemented as apiserver.py (The file that handles get/post requests)

# ... build or import your bottle application here ...
# Do NOT use bottle.run() with mod_wsgi
application = bottle.default_app()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s