How to setup Django with OpenLitespeed on Ubuntu 20.04

You will learn how to set up Django on an OpenLiteSpeed web server in this quick tutorial. I will assume that your server runs Ubuntu/Debian and that you have root access to it via SSH.

LiteSpeed SAPI is the fastest and easiest way to run web applications on OpenLiteSpeed Web Server. WSGI is supported by OpenLiteSpeed for Python applications. An interface for integrating web servers and web applications is called Web Server Gateway Interface (WSGI). The module allows Python applications written for WSGI to operate in a variety of environments. It is important to note that not all Python applications are WSGI-compatible.


Using LWSGI to run Python applications with OLS will be discussed in this guide. I assume that you have already installed and set up Python and virtual hosts.


VPS (virtual private server)

I recommend you to use Linode VPS (virtual private server): start at $5/month. You can create an account at Linode via my referral link to get a $100 credit.

You can also use free resources such as Oracle Cloud Infrastructure Or AWS (EC2) for 12-months of the free tier services.

Read: How To Create Always FREE VPS in Oracle Cloud

Initial Setup

Install Python3 Library

If you haven’t already, please install the Python3 dev package (note that it must be Python 3.5 or higher):

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.

For python2 users, please use e.g. python, python2.7 to compile.

curl -O
tar xf wsgi-lsapi-1.6.tgz
cd wsgi-lsapi-1.6
python3 ./
cp lswsgi /usr/local/lsws/fcgi-bin/

How to Verify WSGI Script

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 script

Create a python directory under your document root.

Then, create a 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()]


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

It works!

Python x.x.x


Setup Django Project

Create Project

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

pip3 install django

Create demo project under the /var/www/html/ folder.

Create the folder if it doesn’t exist.

mkdir -p /var/www/html/

cd /var/www/html/

django-admin startproject demo

Django Settings

Edit inside the inner folder (maybe demo/demo) to allow hosts


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 = '/var/www/html/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 in the directory where exists (maybe one level up)

mkdir -p public/static
python3 collectstatic

Create admin superuser

python3 migrate
python3 createsuperuser

Change owner Permission

chown -R nobody:nogroup /var/www/html/demo

Navigate to Web Admin > Virtual Hosts > Context > Add

Type = App Server
URI = /python/
Location = /var/www/html/demo/
Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
Application Type = WSGI
Startup File = demo/

Leave a Comment