Deploying Django with OpenLiteSpeed (Part 3)

| |

In this part we will test our WSGI Api created in Part 1 by setting up Context. Context is virtual location under the parent url of virtual host. We can point different locations to the sub urls. Here we use context to mount our WSGI application to “/” directory.

Set up Context

Click the virtual hosts section in Web Admin panel then open the virtual host we created earlier (social in our case). Navigate to Context and then click Add “+” icon.

Click Next then you’ll get configuration form. Choose URI that you want to map to this context. We will be uploading the project to Document Root folder (/usr/local/lsws/social/public).

Set the binary file as:


And finally from the dropdown choose Application Type as WSGI. The Binary path field is set to as default so you can also leave it blank for now. But when we will upload project you will need to map the location to file of the project. All other fields are optional. Now save the configuration.

Create file

Its time to get back to the console. Navigate to the root folder of the virtual host.

$ cd /usr/local/lsws/YOUR_VHOST/public

Now make a file named

$ nano

Paste this content and save

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()]

Now restart the Openlitespeed server.

$ /usr/local/lsws/bin/lswsctrl restart

Navigate to http://You_domain/ in the browser

Great!! Everything is working fine. Openlitespeed successfully served your simple Python wsgi app.

Review Before Deployment

Its time to refactor the file of your project. I suggest to set SECRET_KEY to environment variable instead of hardcoding. Another option is DEBUG which should be set to False during production.

ALLOWED_HOSTS option allow you to restrict the access of Django app within the domain provided to it as list of string. Setting it to [‘*’] will allow all domain to access the app. It is always recommended to use your exact domain. You can allow all the subdomains by adding a dot(.) before the main domain. Below here is snippet of the settings I have refactored.

import os 
SECRET_KEY = os.environ['SECRET_KEY']

DEBUG = False

ALLOWED_HOSTS = [ '' ]  #Replace this with your domain. Here I'm allowing all subdomains from

Check out the official Django deployment checklist for better security and performance options.

If you were using sqlite during development but at production you might you other database backends, such as MariaDB, MySQL, Oracle, or PostgreSQL. In this tutorial we’ll use Postgres with Django. Lets change the DATABASE setting to use postgres instead of sqlite.

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '',
        'PORT': '5432',

Save the file. Now you are a step behind for deployment of Django app. In Next Part we’ll upload our project using filezilla client, and install Postgres data base and configure it.


Deploying Django with OpenLiteSpeed (Part 2)

Deploying Django with OpenLiteSpeed (Part 4)

Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Paul Lovan


[…] We have configured a lot in Openlitespeed. Now we will check the WSGI with a simple python script in the Part 3. […]

Would love your thoughts, please comment.x