How to setup DJANGO application on CyberPanel/OpenLiteSpeed

DJANGO is a web application framework built with Python. DJANGO seen a tremendous growth recently, and a lot of developers are moving towards this framework. DJANGO official slogan says “the web framework for perfectionists with deadlines”. CyberPanel is also built on top of DJANGO framework, apart from that Instagram also uses DJANGO framework.

OpenLiteSpeed recently added support to run DJANGO applications at VHOST level. Now there is no need to reverse proxy your DJANGO application to gunicorn server. This also leads to increased performance.

In this tutorial we will see how we can setup a DJANGO application. (OpenLiteSpeed version 1.4.42+ is required)

Step 1: Install CyberPanel and Create Website

Before moving forward you need to install CyberPanel and Create Website. If CyberPanel is already installed, make sure to upgrade OpenLiteSpeed.

Upgrade OpenLiteSpeed on CentOS

yum update
yum upgrade openlitespeed

Upgrade OpenLiteSpeed on Ubuntu

apt-get update
apt-get upgrade openlitespeed

Install python 3.6 on CentOS

yum install python36-devel
yum install python36-pip

Install python 3.6 on Ubuntu

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

Step 2: Install LiteSpeed Implementation of WSGI

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

Step 3: Setup virtualevn for DJANGO Application

Note: In this tutorials we are going to use django.cyberpanel.net as example domain and cyberpanel as project name.

We will setup virtualevn in the document root of the application:

virtualenv --system-site-packages /home/django.cyberpanel.net/public_html
source /home/django.cyberpanel.net/public_html/bin/activate
pip3 install django

Step 4: Setup or Upload DJANGO Application

If you already have DJANGO application you can upload it to document root, we will create a brand new DJANGO project.

cd /home/django.cyberpanel.net/public_html
django-admin startproject cyberpanel

Open your project settings and make sure allowed hosts looks like

ALLOWED_HOSTS = ['*']

settings.py for this project will be located at /home/django.cyberpanel.net/public_html/cyberpanel/cyberpanel/settings.py

To set path for static files please add following at the end of settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/home/django.cyberpanel.net/public_html/static'

And create folder to start to collect your static files using

cd /home/django.cyberpanel.net/public_html/cyberpanel
python manage.py collectstatic

Create admin super user

python manage.py migrate
python manage.py createsuperuser

Note: Always replace django.cyberpanel.net with your domain name.

Since you are modifying/creating files via command line, you can go to File manager and click Fix Permissions so that correct permissions settings are in place.

Step 5: Setup DJANGO Application Context

Go to website launcher at https://:8090/websites/django.cyberpanel.net so that you can edit vhost Conf (Replace django.cyberpanel.net with your domain).

Scroll down and click vhost Conf and paste following at the end of the box:

context / {
  type                    appserver
  location                /home/django.cyberpanel.net/public_html/cyberpanel
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             cyberpanel/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/django.cyberpanel.net/public_html/bin/python
  env                     PYTHONHOME=/home/django.cyberpanel.net/public_html/
}

env LS_PYTHONBIN=/home/django.cyberpanel.net/public_html/bin/pyhton is the location of python inside your virtualenv. Make sure you replace domain and project name in above settings accordingly.

Once done click Save and you can visit your DJANGO application at yourdomain.com, in this case, its django.cyberpanel.net. It should look like:

You can also check admin panel at django.cyberpanel.net/admin

django

1 Like

It seems like this does not work on latest version of CyberPanel.

ERR_TOO_MANY_REDIRECTS

image

it works perfectly fine

by seeing your posted error post , for problem check file permissions or ssl issue

I have tried everything i can.
I have also tried running it internally from the website list.
It gives the same issue.
Goes to 403 for some reason.

Internal SSL is let’s encrypt and external SSL is CloudFlare.

image

i have 10 Django sites hosted on cyber-panel its perfectly fine.
can you post detailed info about server?

Contabo Centos 7

6 vCPU Cores

16 GB RAM

400 GB SSD

2 Snapshots

32 TB TrafficUnlimited Incoming

All packages up to date
sudo yum check-update
sudo yum update

Error messages from OpenLieSpeed fail to load “Error redirect too many times”.
Other than error messages and django, everything else works.

Hi all,

I’ve read this same tutorial on 3 websites and a youtube video and all have “pyhton” written with the same typo. Is this really a typo or is there some other hidden reason for it?

I do think it’s a typo, but i still can’t get the tutorial to work.
Standard blank CyberPanel HTML shows up, but nothing else.
I have tried both with the typo and without.

That strange as I’ve got it to work quite easily on mine. And it works whether the typo is there or not.

Are you inserting the context part at the bottom of your vhosts file? I first made the mistake of replacing it entirely.

And then of course there’s the 5 parts of that config that you need to change.

can you please tell me how can I upload my existing django project in document root.

This tutorial is not working with Django 3, can you update it?

An internal server error has occured. Internal Server Error 500
can you help me please

Restart your server and post the errors you are getting here https://<IP Address>:8090/serverstatus/cyberCPMainLogFile

1 Like

I am facing the same issue. Everything works on localhost smoothly.
but on VPS I am getting 503 error and 500 error.
error 500 : when I remove env PYTHONHOME=/home/t.typrep.com/public_html/ from vHOST config and error 503 when I add same line to vHost Config
Here are the last 50 lines of cyberCPMainLogFile

[03.16.2023_02-00-06] SSL exists for localhost. Checking if SSL will expire in 15 days..
[03.16.2023_02-00-06] SSL exists for localhost and is not ready to renew, skipping..
[03.16.2023_02-00-06] Checking SSL for t.typrep.com.
[03.16.2023_02-00-06] SSL exists for t.typrep.com. Checking if SSL will expire in 15 days..
[03.16.2023_02-00-06] SSL exists for t.typrep.com and is not ready to renew, skipping..
[03.16.2023_02-00-06] Checking SSL for cdn1.rankiq.in.
[03.16.2023_02-00-06] SSL exists for cdn1.rankiq.in. Checking if SSL will expire in 15 days..
[03.16.2023_02-00-06] SSL exists for cdn1.rankiq.in and is not ready to renew, skipping..
[03.16.2023_02-00-06] Checking SSL for mail.localhost.
[03.16.2023_02-00-06] SSL exists for mail.localhost. Checking if SSL will expire in 15 days..
[03.16.2023_02-00-06] SSL exists for mail.localhost and is not ready to renew, skipping..
[03.16.2023_02-00-06] Checking SSL for mail.t.typrep.com.
[03.16.2023_02-00-06] SSL exists for mail.t.typrep.com. Checking if SSL will expire in 15 days..
[03.16.2023_02-00-06] SSL exists for mail.t.typrep.com and is not ready to renew, skipping..
[03.16.2023_02-00-06] Restarting mail services for them to see new SSL.
[03.16.2023_02-00-06] [Errno 2] No such file or directory: 'postmap'. [ProcessUtilities.normalExecutioner.Base]
[03.16.2023_14-01-40] typrep.com
[03.16.2023_14-03-27] Status Code: Unknown for: http://www.t.typrep.com/.well-known/acme-challenge/t.typrep.com. Error: HTTPConnectionPool(host='www.t.typrep.com', port=80): Max retries exceeded with url: /.well-known/acme-challenge/t.typrep.com (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fba38123430>: Failed to establish a new connection: [Errno -2] Name or service not known'))
[03.16.2023_14-03-27] Status Code: 200 for: http://t.typrep.com/.well-known/acme-challenge/t.typrep.com
[03.16.2023_14-03-29] /root/.acme.sh/acme.sh --issue -d t.typrep.com -d www.t.typrep.com --cert-file /etc/letsencrypt/live/t.typrep.com/cert.pem --key-file /etc/letsencrypt/live/t.typrep.com/privkey.pem --fullchain-file /etc/letsencrypt/live/t.typrep.com/fullchain.pem -w /usr/local/lsws/Example/html -k ec-256 --force --server letsencrypt
[03.16.2023_14-03-34] Failed to obtain SSL for: t.typrep.com and: www.t.typrep.com
[03.16.2023_14-03-34] Trying to obtain SSL for: t.typrep.com
[03.16.2023_14-03-37] Successfully obtained SSL for: t.typrep.com
[03.16.2023_14-19-41] 'list' object has no attribute 'find'
[03.16.2023_14-19-57] 'list' object has no attribute 'find'
[03.16.2023_14-20-17] 'list' object has no attribute 'find'
[03.16.2023_16-13-26] Status Code: 200 for: http://www.t.typrep.com/.well-known/acme-challenge/t.typrep.com
[03.16.2023_16-13-26] Status Code: 200 for: http://t.typrep.com/.well-known/acme-challenge/t.typrep.com
[03.16.2023_16-13-29] /root/.acme.sh/acme.sh --issue -d t.typrep.com -d www.t.typrep.com --cert-file /etc/letsencrypt/live/t.typrep.com/cert.pem --key-file /etc/letsencrypt/live/t.typrep.com/privkey.pem --fullchain-file /etc/letsencrypt/live/t.typrep.com/fullchain.pem -w /usr/local/lsws/Example/html -k ec-256 --force --server letsencrypt
[03.16.2023_16-13-35] Failed to obtain SSL for: t.typrep.com and: www.t.typrep.com
[03.16.2023_16-13-35] Trying to obtain SSL for: t.typrep.com
[03.16.2023_16-13-38] Successfully obtained SSL for: t.typrep.com
[03.17.2023_00-00-12] [Errno 2] No such file or directory: '/home/cyberpanel/git'. [IncScheduler.git:90]
[03.17.2023_02-00-07] Running SSL Renew Utility
[03.17.2023_02-00-07] Checking SSL for localhost.
[03.17.2023_02-00-07] SSL exists for localhost. Checking if SSL will expire in 15 days..
[03.17.2023_02-00-07] SSL exists for localhost and is not ready to renew, skipping..
[03.17.2023_02-00-07] Checking SSL for cdn1.rankiq.in.
[03.17.2023_02-00-07] SSL exists for cdn1.rankiq.in. Checking if SSL will expire in 15 days..
[03.17.2023_02-00-07] SSL exists for cdn1.rankiq.in and is not ready to renew, skipping..
[03.17.2023_02-00-07] Checking SSL for t.typrep.com.
[03.17.2023_02-00-07] SSL exists for t.typrep.com. Checking if SSL will expire in 15 days..
[03.17.2023_02-00-07] SSL exists for t.typrep.com and is not ready to renew, skipping..
[03.17.2023_02-00-07] Checking SSL for mail.localhost.
[03.17.2023_02-00-07] SSL exists for mail.localhost. Checking if SSL will expire in 15 days..
[03.17.2023_02-00-07] SSL exists for mail.localhost and is not ready to renew, skipping..
[03.17.2023_02-00-07] Restarting mail services for them to see new SSL.
[03.17.2023_02-00-07] [Errno 2] No such file or directory: 'postmap'. [ProcessUtilities.normalExecutioner.Base]
[03.17.2023_05-05-13] 'list' object has no attribute 'find'
[03.17.2023_05-05-55] 'list' object has no attribute 'find'

And following the error log

2023-03-17 05:58:23.093907 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:58:23.093917 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:58:23.094311 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 879
2023-03-17 05:58:23.094348 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:33.007054 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] ExtConn timed out while processing.
2023-03-17 05:58:33.007129 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:33.007136 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:58:33.007206 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:58:33.007218 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:58:33.007706 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 880
2023-03-17 05:58:33.007778 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:43.020178 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:43.020191 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:58:43.020246 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:58:43.020254 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:58:43.020633 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 881
2023-03-17 05:58:43.020675 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:43.020722 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] No request delivery notification has been received from LSAPI application, possible dead lock.
2023-03-17 05:58:53.033842 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] No request delivery notification has been received from LSAPI application, possible dead lock.
2023-03-17 05:58:53.033895 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:58:53.033902 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:58:53.033952 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:58:53.033965 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:58:53.034334 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 882
2023-03-17 05:58:53.034370 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:03.047248 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] No request delivery notification has been received from LSAPI application, possible dead lock.
2023-03-17 05:59:03.047509 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:03.047517 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:59:03.047578 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:59:03.047587 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:59:03.047940 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 883
2023-03-17 05:59:03.047978 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:13.061088 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] No request delivery notification has been received from LSAPI application, possible dead lock.
2023-03-17 05:59:13.061144 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:13.061150 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:59:13.061197 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:59:13.061206 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:59:13.061571 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 884
2023-03-17 05:59:13.061603 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:23.087142 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] No request delivery notification has been received from LSAPI application, possible dead lock.
2023-03-17 05:59:23.087194 [INFO] [416] [wsgi:t.typrep.com:/]: locked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:23.087199 [INFO] [416] [wsgi:t.typrep.com:/] remove unix socket for detached process: /tmp/lshttpd/t.typrep.com:_.sock
2023-03-17 05:59:23.087245 [NOTICE] [416] [LocalWorker::workerExec] VHost:t.typrep.com suExec check uid 65534 gid 65534 setuidmode 0.
2023-03-17 05:59:23.087254 [NOTICE] [416] [LocalWorker::workerExec] Config[wsgi:t.typrep.com:/]: suExec uid 65534 gid 65534 cmd /usr/local/lsws/fcgi-bin/lswsgi -m /home/t.typrep.com/public_html/typrep/typrep/api/wsgi.py, final uid 65534 gid 65534, flags: 0.
2023-03-17 05:59:23.087643 [NOTICE] [416] [wsgi:t.typrep.com:/] add child process pid: 886
2023-03-17 05:59:23.087677 [INFO] [416] [wsgi:t.typrep.com:/]: unlocked pid file [/tmp/lshttpd/t.typrep.com:_.sock.pid].
2023-03-17 05:59:33.000335 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] ExtConn timed out while processing.
2023-03-17 05:59:33.000445 [NOTICE] [416] [106.213.84.181:1830:HTTP2-3#t.typrep.com] oops! 503 Service Unavailable
2023-03-17 05:59:33.000451 [INFO] [416] [106.213.84.181:1830:HTTP2-3] Content len: 0, request line: 'GET /favicon.ico HTTP/1.1'
2023-03-17 05:59:33.000455 [INFO] [416] [106.213.84.181:1830:HTTP2-3] Redirect: #1, URL: /_typrep/api/wsgi.py

And Following is the vHOST context

context / {
  type                    appserver
  location                /home/t.typrep.com/public_html/typrep
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             typrep/api/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/t.typrep.com/public_html/bin/python3
  env                     PYTHONPATH=home/t.typrep.com/public_html/lib/python3.10:/home/t.typrep.com/public_html/typrep
env                     PYTHONHOME=/home/t.typrep.com/public_html/
}

wsgi.py file is inside the typrep/api/wsgi.py.

i have 10 Django sites hosted on cyber-panel its perfectly fine.

It will be great if you share whats going wrong in my case. :grinning:

I run into this error. Somebody should help please.

x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -O0 -g3 -I. -I/usr/include/python3.10 -I. -I/usr/include/python3.10 -c -o wsgi.o wsgi.c
wsgi.c:30:10: fatal error: node.h: No such file or directory
30 | #include <node.h>
| ^~~~~~~~
compilation terminated.
make: *** [: wsgi.o] Error 1

I am facing the same issue. Everything works on localhost smoothly..

.

@carlzdooms
this issue is in ubuntu 22.04 and 22.10
try using ubuntu 20.44 LTS