How to setup Django with OpenLitespeed on Ubuntu 20.04

This tutorial will show you how to set up Django with OpenLiteSpeed on Ubuntu 20.04.

We will assume that you have already installed OpenLiteSpeed and Python 3.8 on your server. If not, please refer to the OpenLiteSpeed installation guide and the Python installation guide.

Create a Python virtual environment

First, we will create a Python virtual environment for our Django project.

virtualenv -p python3.8 /path/to/venv

Activate the virtual environment

Next, we will activate the virtual environment so that we can install Django in it.

source /path/to/venv/bin/activate

Install Django

Now that the virtual environment is activated, we can install Django.

pip install django

Create a Django project

Once Django is installed, we can create a new Django project.

django-admin startproject myproject

Create an OpenLiteSpeed configuration file

Next, we will create an OpenLiteSpeed configuration file for our Django project.

We will name it “django.conf” and place it in the “/usr/local/lsws/conf/vhosts/mysites” directory.

ServerName DocumentRoot /path/to/myproject/myproject 
<Directory /path/to/myproject/myproject> Options +FollowSymLinks AllowOverride All Require all granted </Directory> 

<IfModule mod_rewrite.c> 
RewriteEngine On RewriteRule ^(.*)$ /$1 [PT,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /$1 [PT,L] RewriteRule ^static/(.*)$ /static/$1 [L] 

<FilesMatch "\.(jpg|jpeg|png|gif|css|js)$">
Header set Cache-Control "public, max-age=3600"

<FilesMatch "\.(woff|woff2|ttf|eot|svg)$"> 
Header set Cache-Control "public, max-age=31536000" 
 WSGIDaemonProcess myproject python-path=/path/to/myproject:/path/to/venv/lib/python3.8/site-packages WSGIProcessGroup myproject WSGIScriptAlias / /path/to/myproject/myproject/ 

Restart OpenLiteSpeed

Finally, we will restart OpenLiteSpeed to apply the new configuration.

systemctl restart lsws

Your Django project should now be accessible at