Running Python WSGI Apps with LSAPI

You are here:
< Back

Python WSGI Apps with LSAPI

LiteSpeed SAPI is the easiest and fastest way to run web applications with OpenLiteSpeed Web Server. OpenLiteSpeed supports LSAPI for Python applications through WSGI. WSGI (Web Server Gateway Interface) is a low-level interface between web servers and web applications. It allows Python applications designed for WSGI to be used in a variety of settings. It is important to note that, while most Python applications are WSGI-compatible, not all are.

This wiki will go over how to set up OLS to run Python applications with LWSGI. This wiki assumes that you already have a working version of Python installed and virtual hosts set up to run it on.

  1. Initial Setup
  2. How to Verify WSGI Script
  3. How to Set up for Django
  4. How to Set up Django with Virtual Environment
  5. How to Verify Django
  6. Optional Settings

Requirements

  • OpenLiteSpeed version 1.4.42+

Initial Setup

Install Python3 Library

If you haven’t already, please install the Python3 dev package:
CentOS

yum install python3-devel

Ubuntu

apt install build-essential
apt-get install python3-dev

Install WSGI

The easiest and fastest way to run web applications with LiteSpeed Web Server is to install and compile wsgi-lsapi.

curl -O http://www.litespeedtech.com/packages/lsapi/wsgi-lsapi-1.4.tgz
tar xf wsgi-lsapi-1.4.tgz
cd wsgi-lsapi-1.4
python3 ./configure.py
make
cp lswsgi /usr/local/lsws/fcgi-bin/

How to Verify WSGI Script

Set up Context

This example assumes you are using the default document root + URI: /python/

Navigate to Web Admin > Virtual Hosts > Context > Add

  • Type = App Server
  • URI = /python/
  • Location = /usr/local/lsws/Example/python/
  • Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
  • Application Type = WSGI

Create wsgi.py script

Create a python directory under your document root.

Create a wsgi.py file with the following content:

import os
import sys
sys.path.insert(0, os.path.dirname(__file__))

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'It works!\n'
    version = 'Python %s\n' % sys.version.split()[0]
    response = '\n'.join([message, version])
    return [response.encode()]

Verification

Visit http://Server_IP:Port/python/ in your browser and you should see:

It works!

Python x.x.x

How to Set up for Django

Create Project

If you haven’t installed Django, please do so through pip3

pip3 install django

Create demo project under e.g. Virtual Host Root: /usr/local/lsws/Example/

cd /usr/local/lsws/Example/
django-admin startproject demo

Django Settings

Edit settings.py inside the inner folder to allow hosts

ALLOWED_HOSTS = ['*'] 

Edit settings.py inside the inner folder for managing static files

The STATICFILES_DIRS tells Django where to look for static files that are not tied to a particular app. In this case, we just told Django to also look for static files in a folder called static in our root folder, not just in our apps.

STATIC_URL = '/python/static/'
STATIC_ROOT = '/usr/local/lsws/Example/demo/public/static'
  • Note 1: /python/static/ depends on your context settings
  • Note 2: putting the static folder under public is required

Collect static files

mkdir -p public/static
python3 manage.py collectstatic

Create admin super user

python3 manage.py migrate
python3 manage.py createsuperuser

Change owner

chown -R nobody:nogroup /usr/local/lsws/Example/demo

Set up Context

This example assumes you are using the default document root + URI: /python/

Navigate to Web Admin > Virtual Hosts > Context > Add

  • Type = App Server
  • URI = /python/
  • Location = /usr/local/lsws/Example/demo/
  • Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
  • Application Type = WSGI
  • Startup Filedemo/wsgi.py

How to Set up Django with Virtual Environment

Create Virtual Environment

virtualenv venv

Access and Activate the virtual environment

source venv/bin/activate

Check if python loaded from virtual environment

which python

Create Project

pip and python already link to python version 3 in virtulenv, so if you haven’t install Django, please try to install through pip,

pip install django

Create demo project under e.g. Virtual Host Root: /usr/local/lsws/Example/

cd /usr/local/lsws/Example/
django-admin startproject demo

Django Settings

Edit settings.py inside the inner folder to allow hosts

ALLOWED_HOSTS = ['*']

Edit settings.py inside the inner folder for managing static files

The STATICFILES_DIRS tells Django where to look for static files that are not tied to a particular app. In this case, we just told Django to also look for static files in a folder called static in our root folder, not just in our apps.

STATIC_URL = '/python/static/'
STATIC_ROOT = '/usr/local/lsws/Example/demo/public/static'
  • Note 1: /python/static/ depends on your context settings
  • Note 2: putting the static folder under public is required

Collect static files

mkdir -p public/static
python3 manage.py collectstatic

Create admin super user

python manage.py migrate
python manage.py createsuperuser

Change owner

chown -R nobody:nogroup /usr/local/lsws/Example/demo

Set up Context

This example assumes you are using the default document root + URI: /python/

Navigate to Web Admin > Virtual Hosts > Context > Add

  • Type = App Server
  • URI = /python/
  • Location = /usr/local/lsws/Example/demo/
  • Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
  • Application Type = WSGI
  • Startup Filedemo/wsgi.py
  • EnvironmentPYTHONHOME=/usr/local/lsws/Example/venv/
    LS_PYTHONBIN=/usr/local/lsws/Example/venv/bin/python

How to verify Django

Visit http://Server_IP:Port/python/admin in your browser and you should see admin page

 

 

Optional Settings

Custom Binary Path

If you want to change the LSWSGI path, just update the Context and set Binary Path = /usr/lswsgi.

Custom Startup File Name

If you want to change the default WSGI file name from passenger_wsgi.py to example_wsgi.py, just update the Context and set Startup File = example_wsgi.py.