This is an old revision of the document!


Python Django: How to set up Django when LSWS reads apache conf

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 official slogan says “the web framework for perfectionists with deadlines”.

In this tutorial, we will see how we can setup a DJANGO application when LSWS reading apache configuration instead of native, such as a Cyberpanel environment. LSWS will use mod_passenger directives in virtual host's doc_root .htaccess to define the application.

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

Install python3 on CentOS

yum install python3-devel
yum install python3-pip

Install python3 on Ubuntu

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

Create your domain on Cyberpanel, such as cyberdjangotest1.com, it should locate at /home/cyberdjangotest1.com/ and owned by user “cyberdj:cyberdj”

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

Download the latest version of wsgi-lsapi from https://www.litespeedtech.com/open-source/litespeed-sapi/download

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 virtualevn in the user’s home directory. For best practice, virtualevn should not be in domain’s document_root.

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

If you already have DJANGO application you can create a project folder, for example app1, at /home/cyberdjangotest1.com/app1/ and move your exiting application to to it. We will create a brand new DJANGO project named “app1”

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

Edit your project settings:

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

make sure allowed hosts looks like:

ALLOWED_HOSTS = ['*']

Create static files directory, normally static files be placed inside doc_root, you can create /static/ directory at doc_root first.

mkdir /home/cyberdjangotest1.com/public_html/static

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

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

then you can go to your project directory and run a few commands to start collect 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 django application, LSWS will use mod_passenger directives in virtual host doc_root .htaccess.

You can create .htaccess if it doesn't exist yet.

You can place the following in .htaccess:

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/14 20:19
  • by Jackson Zhang