This page looks best with JavaScript enabled

How to setup Falcon in OpenLiteSpeed

 ·  ☕ 2 min read  ·  🐨 Puliyo

Objective

Falcon is Web Framework written in Python.

https://falcon.readthedocs.io/en/stable/index.html

You can run it using wsgi. Openlitespeed supports wsgi and virtualenv. Lets set it up.

Procedure

1. Install lswsgi

lswsgi is wsgi for Openlitespeed.

In below link, complete up to and including “Install WSGI”.

https://openlitespeed.org/kb/python-wsgi-applications/

Make sure to get latest lswsgi bin from here:

https://www.litespeedtech.com/open-source/litespeed-sapi/download

2. Create virtualenv and install falcon

1
2
3
4
5
6
mkdir -p /srv/vhadm
mkdir /srv/vhadm/html
cd /srv/vhadm
python3 -m venv venv
source ./venv/bin/activate
pip install falcon

3. Create sample wsgi.py script

Openlitespeed will by default look for wsgi.py script (you can change this if you want to in Context setting).

Also, it will try invoking application instance in wsgi.py script. So you must have your wsgi web instance named application in your python script.

1
2
cd /srv/vhadm/html
nano wsgi.py

wsgi.py will have below content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import falcon

class ThingsResource(object):
        def on_get(self, req, resp):
                """Handles GET requests"""
                resp.status = falcon.HTTP_200
                resp.body = ('\nHello World\n')

application = falcon.API()
things = ThingsResource()
application.add_route('/things', things)

4. Setup lswsgi

Access to lsws admin console.

You can use either server configuration or vhost configuration.

I’m going to use vhost with vhost root at /srv/vhadm and document root at /srv/vhadm/html.

vhost Basic Tab

Make sure “Enable Scripts/ExtApps” setting is enabled.

vhost basic tab

vhost General Tab

vhost general tab

vhost Context Tab

Open context tab and click on plus button. Select App Server. Next.

Define as below:

URI = /
Location = $DOC_ROOT
Binary Path = <path-to-lswsgi>  eg: /usr/local/lsws/fcgi-bin/lswsgi
Application Type = WSGI
Environment = LS_PYTHONBIN=/srv/vhadm/venv/bin/python

Save the changes.

NOTE: the lsws instruction page tells you to also set PYTHONPATH in Environment but this is not required as python searches packages by traversing back from its binary path.

5. Check file permission

In my setup, since External App Set UID Mode is Not Set in vhost configuration, it will use lsws user to run the script.

Change the file ownership so that lsws user can at least read the wsgi.py file.

In my case, lsws user is www-lsws.

1
2
chgrp -R www-lsws /srv/vhadm/html
chmod -R 750 /srv/vhadm/html

6. Configure listener to that vhost and graceful restart the lsws

7. Test if it works correctly

Sample script only accepts endpoint “/things”.

So to test, you will use below url:

http://<your-host>:<your-port>/things

You should receive response with body “Hello World” if successful.

Share on
Support the author with