How to Host DJANGO application on CyberPanel

YouTube reference

Get 200$ in Digital Ocean and Get 100$ in Vultr

Step 1: Install CyberPanel and Create Website

Note :: Make sure you have installed cyberpanel on ubuntu 20.04

with ubuntu 22.04/23.04 django will not work.

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

A )

Creating website is easy on CyberPanel.

Main > Websites > Create Website

Then fill in all the fields, they are self-explanatory, but let see some example configuration:

  1. Select the package for this website, CyberPanel comes with a default package however you can create your own.
  2. Select Owner for this website, if an owner is other than admin than this site can be managed by that selected user as well.
  3. Enter your domain.
  4. Enter email.
  5. Select the PHP version, you can also later change this.
  6. If SSL is checked CyberPanel will try to obtain Let’s Encrypt SSL if it fails self-signed SSL will be issued which can also be changed later. (CyberPanel will fail to obtain SSL if domain is not yet pointed to the server).

Finally press Create Website and website will be created.

B )

Upgrade OpenLiteSpeed on Ubuntu

apt-get update
apt-get upgrade openlitespeed

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 or Upload DJANGO Application And Setup Virtualenv for DJANGO Application

Note: In this tutorials we are going to use dj.1yt.in as example domain and mytest as project name.

We will setup virtualevn in the document root of the application:
also
If you already have DJANGO application you can upload it to document root, we will get sample from my new DJANGO project from github.

cd /home/dj.1yt.in/public_html

git clone https://github.com/ServerGuyFluxx/django_web_sample
mv django_web_sample/* .

pip install virtualenv 
mkdir venv
virtualenv venv

source venv/bin/activate
pip install -r  requriments.txt

cd mytest
python manage.py migrate
python manage.py createsuperuser

Step 4: Setup DJANGO Application Context

Go to website launcher at https://:8090/websites/dj.1yt.in so that you can edit vhost Conf (Replace dj.1yt.in with your domain).

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

context / {
  type                    appserver
  location                /home/dj.1yt.in/public_html/mytest
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             mytest/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/dj.1yt.in/public_html/venv/bin/python
  env                     PYTHONHOME=/home/dj.1yt.in/public_html/venv/
}

full config should look like following

docRoot                   $VH_ROOT/public_html
vhDomain                  $VH_NAME
vhAliases                 www.$VH_NAME
adminEmails               [email protected]
enableGzip                1
enableIpGeo               1

index  {
  useServer               0
  indexFiles              index.php, index.html
}

errorlog $VH_ROOT/logs/$VH_NAME.error_log {
  useServer               0
  logLevel                WARN
  rollingSize             10M
}

accesslog $VH_ROOT/logs/$VH_NAME.access_log {
  useServer               0
  logFormat               "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
  logHeaders              5
  rollingSize             10M
  keepDays                10  
  compressArchive         1
}

scripthandler  {
  add                     lsapi:fin8084 php
}

extprocessor fin8084 {
  type                    lsapi
  address                 UDS://tmp/lshttpd/fin8084.sock
  maxConns                10
  env                     LSAPI_CHILDREN=10
  initTimeout             600
  retryTimeout            0
  persistConn             1
  pcKeepAliveTimeout      1
  respBuffer              0
  autoStart               1
  path                    /usr/local/lsws/lsphp81/bin/lsphp
  extUser                 fin8084
  extGroup                fin8084
  memSoftLimit            2047M
  memHardLimit            2047M
  procSoftLimit           400
  procHardLimit           500
}

phpIniOverride  {
php_admin_value open_basedir "/tmp:$VH_ROOT"
}

module cache {
 storagePath /usr/local/lsws/cachedata/$VH_NAME
}

rewrite  {
 enable                  1
  autoLoadHtaccess        1
}

context /.well-known/acme-challenge {
  location                /usr/local/lsws/Example/html/.well-known/acme-challenge
  allowBrowse             1

  rewrite  {
     enable                  0
  }
  addDefaultCharset       off

  phpIniOverride  {

  }
}


vhssl  {
  keyFile                 /etc/letsencrypt/live/dj.1yt.in/privkey.pem
  certFile                /etc/letsencrypt/live/dj.1yt.in/fullchain.pem
  certChain               1
  sslProtocol             24
  enableECDHE             1
  renegProtection         1
  sslSessionCache         1
  enableSpdy              15
  enableStapling           1
  ocspRespMaxAge           86400
}
context / {
  type                    appserver
  location                /home/dj.1yt.in/public_html/mytest
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             mytest/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/dj.1yt.in/public_html/venv/bin/python
  env                     PYTHONHOME=/home/dj.1yt.in/public_html/venv/
}

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 dj.1yt.in . It should look like: