Set up Django With LSWS in CyberPanel

Django is a web application framework built with Python. Django has seen tremendous growth recently, and a lot of developers are moving towards this framework. Django's official slogan is “the web framework for perfectionists with deadlines.”

In this tutorial, we will set up a Django application for LiteSpeed Web Server with CyberPanel. We will use mod_passenger directives in a virtual host's document root .htaccess to define the application.

NOTE: These instructions also apply if you have manually set LSWS to read Apache config. They do not, however, apply to LSWS with other control panels (like cPanel or Plesk). For those, we recommend using CloudLinux Python selectors.

  • Up-to-date installation of CyberPanel
  • Up-to-date installation of LiteSpeed Web Server

CentOS

yum install python3-devel
yum install python3-pip

Ubuntu

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

Create your domain on CyberPanel. For this example, let's use cyberdjangotest1.com, put it in the /home/cyberdjangotest1.com/ directory and make the owner be cyberdj:cyberdj, like so:

drwx--x--x 5 cyberdj    cyberdj    4096 Aug  3 16:30 cyberdjangotest1.com

Download the latest version of wsgi-lsapi.

Run the following commands:

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

We will setup virtualenv in the user’s home directory. To adhere to best practices, don't put virtualenv in the domain’s document root.

Su - cyberdj
virtualenv --system-site-packages /home/cyberdjangotest1.com/virtualenv/
source /home/cyberdjangotest1.com/virtualenv/bin/activate
pip3 install django

If you already have a Django application, you can create a project folder, for example app1, at /home/cyberdjangotest1.com/app1/, and move your existing application into it.

For this example, we will create a brand new Django project named app1.

cd /home/cyberdjangotest1.com/
django-admin startproject app1

Edit the project settings:

vi /home/cyberdjangotest1.com/app1/app1/settings.py
  

Make sure allowed hosts looks like this:

ALLOWED_HOSTS = ['*']

Create a static file directory. Normally static files are placed inside the document root, so you can create a /static/ directory in doc root:

mkdir /home/cyberdjangotest1.com/public_html/static

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

STATIC_URL = '/static/'
STATIC_ROOT = '/home/cyberdjangotest1.com/public_html/static'  

Visit your project directory. Run a few commands to start collecting static files and create super user:

cd /home/cyberdjangotest1.com/app1/
python3 manage.py collectstatic
python3 manage.py migrate
python3 manage.py createsuperuser

To define a Django application, LSWS will use mod_passenger directives in the virtual host's doc root .htaccess. These directives can be used by LSWS:

  • PassengerBaseURI
  • PassengerAppRoot
  • PassengerAppEnv
  • PassengerAppType
  • PassengerStartupFile
  • PassengerPython
  • PassengerRuby
  • PassengerNodejs
  • PassengerUser
  • PassengerGroup

Place something like the following in .htaccess (create the file if it doesn't exist):

PassengerAppRoot "/home/cyberdjangotest1.com/app1/"
PassengerBaseURI "/"
PassengerPython "/usr/local/lsws/fcgi-bin/lswsgi"
PassengerAppEnv "Production"
# Add SetEnv to pass extra environments to the application
#SetEnv DJANGO_SETTINGS_MODULE ...
LS_PYTHONBIN=/home/cyberdjangotest1.com/virtualenv/bin/python
PYTHONHOME=/home/cyberdjangotest1.com/virtualenv/
  • Admin
  • Last modified: 2020/08/17 19:29
  • by Lisa Clarke