Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
litespeed_wiki:other-ext-apps:python-django [2020/08/14 20:19] Jackson Zhang created |
litespeed_wiki:other-ext-apps:python-django [2020/08/17 19:29] (current) Lisa Clarke Copyediting |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Python Django: How to set up Django when LSWS reads apache conf ====== | + | ====== 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 official slogan says “the web framework for perfectionists with deadlines”. | + | 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 see how we can setup a DJANGO application when LSWS reading apache configuration instead of native, such as a Cyberpanel environment. LSWS will use [[litespeed_wiki:config:mod_passenger|mod_passenger directives]] in virtual host's doc_root .htaccess to define the application. | + | In this tutorial, we will set up a Django application for LiteSpeed Web Server with CyberPanel. We will use [[litespeed_wiki:config:mod_passenger|mod_passenger directives]] in a virtual host's document root ''.htaccess'' to define the application. |
- | ===== Install CyberPanel and Create Website ===== | + | **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. |
- | 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 ==== | + | ===== Prerequisites ===== |
+ | * Up-to-date installation of CyberPanel | ||
+ | * Up-to-date installation of LiteSpeed Web Server | ||
+ | |||
+ | ===== Install Python 3 ===== | ||
+ | |||
+ | ==== CentOS ==== | ||
yum install python3-devel | yum install python3-devel | ||
yum install python3-pip | yum install python3-pip | ||
- | ==== Install python3 on Ubuntu ==== | + | ==== Ubuntu ==== |
apt install build-essential | apt install build-essential | ||
apt-get install python3-dev | 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” | + | ===== Create a Domain ===== |
+ | 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 | + | drwx--x--x 5 cyberdj cyberdj 4096 Aug 3 16:30 cyberdjangotest1.com |
===== Install LiteSpeed Implementation of WSGI ===== | ===== Install LiteSpeed Implementation of WSGI ===== | ||
- | Download the latest version of wsgi-lsapi from https://www.litespeedtech.com/open-source/litespeed-sapi/download | + | [[https://www.litespeedtech.com/open-source/litespeed-sapi/download|Download the latest version of wsgi-lsapi.]] |
+ | |||
+ | Run the following commands: | ||
Cd root | Cd root | ||
Line 33: | Line 41: | ||
cp lswsgi /usr/local/lsws/fcgi-bin/ | cp lswsgi /usr/local/lsws/fcgi-bin/ | ||
| | ||
- | ===== Setup virtualevn for DJANGO Application ===== | + | ===== Set up virtualenv for Django Application ===== |
- | We will setup virtualevn in the user’s home directory. For best practice, virtualevn should not be in domain’s document_root. | + | 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 | Su - cyberdj | ||
- | virtualenv --system-site-packages /home/cyberdjangotest1.com/virtualevn/ | + | virtualenv --system-site-packages /home/cyberdjangotest1.com/virtualenv/ |
- | source /home/cyberdjangotest1.com/virtualevn/bin/activate | + | source /home/cyberdjangotest1.com/virtualenv/bin/activate |
pip3 install django | pip3 install django | ||
| | ||
- | ===== Setup or Upload DJANGO Application ===== | + | ===== Set up or Upload Django Application ===== |
- | 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” | + | 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/ | cd /home/cyberdjangotest1.com/ | ||
django-admin startproject app1 | django-admin startproject app1 | ||
- | Edit your project settings: | + | Edit the project settings: |
vi /home/cyberdjangotest1.com/app1/app1/settings.py | vi /home/cyberdjangotest1.com/app1/app1/settings.py | ||
| | ||
- | make sure allowed hosts looks like: | + | Make sure allowed hosts looks like this: |
ALLOWED_HOSTS = ['*'] | ALLOWED_HOSTS = ['*'] | ||
- | Create static files directory, normally static files be placed inside doc_root, you can create /static/ directory at doc_root first. | + | 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 | mkdir /home/cyberdjangotest1.com/public_html/static | ||
- | To set path for static files please add following at the end of settings.py | + | To set the path for static files, add the following at the end of ''settings.py'': |
STATIC_URL = '/static/' | STATIC_URL = '/static/' | ||
STATIC_ROOT = '/home/cyberdjangotest1.com/public_html/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. | + | Visit your project directory. Run a few commands to start collecting static files and create super user: |
cd /home/cyberdjangotest1.com/app1/ | cd /home/cyberdjangotest1.com/app1/ | ||
python3 manage.py collectstatic | python3 manage.py collectstatic | ||
Line 66: | Line 76: | ||
python3 manage.py createsuperuser | python3 manage.py createsuperuser | ||
| | ||
- | ===== Setup DJANGO Application in .htaccess ===== | + | ===== Set up Django Application in .htaccess ===== |
- | To define django application, LSWS will use mod_passenger directives in virtual host doc_root .htaccess. | + | 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: |
- | You can create .htaccess if it doesn't exist yet. | + | * ''PassengerBaseURI'' |
+ | * ''PassengerAppRoot'' | ||
+ | * ''PassengerAppEnv'' | ||
+ | * ''PassengerAppType'' | ||
+ | * ''PassengerStartupFile'' | ||
+ | * ''PassengerPython'' | ||
+ | * ''PassengerRuby'' | ||
+ | * ''PassengerNodejs'' | ||
+ | * ''PassengerUser'' | ||
+ | * ''PassengerGroup'' | ||
- | You can place the following in .htaccess: | + | Place something like the following in ''.htaccess'' (create the file if it doesn't exist): |
PassengerAppRoot "/home/cyberdjangotest1.com/app1/" | PassengerAppRoot "/home/cyberdjangotest1.com/app1/" |