Python LSAPI¶
LiteSpeed SAPI is the easiest and fastest way to run web applications with OpenLiteSpeed Web Server. OpenLiteSpeed supports LSAPI for Python applications through WSGI. WSGI (Web Server Gateway Interface) is a low-level interface between web servers and web applications. It allows Python applications designed for WSGI to be used in a variety of settings.
Tip
While most Python applications are WSGI-compatible, not all are.
Requirements¶
- Working version of Python installed
- OpenLiteSpeed version 1.4.42+
- Virtual host set up for running Python
Initial Setup¶
Install Python3 Library¶
If you haven't already, please install the Python3 dev package (Python 3.5 or higher):
yum install python3-devel
yum install python3-pip
You may need to use:
yum install python36-devel
yum install python36-pip
apt install build-essential
apt-get install python3-dev
Install WSGI¶
The easiest and fastest way to run web applications with LiteSpeed Web Server is to install and compile wsgi-lsapi.
Warning
Python2 and Python3 compiles cannot be used together. If you are using Python2, please replace python3
in the code below with python2
.
Replace VERSION
in the code below with the desired Python LSAPI version. You can get a list of versions here.
curl -O http://www.litespeedtech.com/packages/lsapi/wsgi-lsapi-VERSION.tgz
tar xf wsgi-lsapi-VERSION.tgz
cd wsgi-lsapi-VERSION
python3 ./configure.py
make
cp lswsgi /usr/local/lsws/fcgi-bin/
Verify WSGI Script¶
Set up Context¶
This example assumes you are using the default document root + /python/
. Navigate to Web Admin > Virtual Hosts > Context > Add and set the following values:
- Type =
App Server
- URI =
/python/
- Location =
/usr/local/lsws/Example/python/
- Binary Path =
/usr/local/lsws/fcgi-bin/lswsgi
- Application Type =
WSGI
Note
The URI is not used to find the default initial script. It always uses wsgi.py
or whatever file name is specified in the Startup File configuration value.
Create wsgi.py script¶
Create a python
directory under your document root. Then, create a wsgi.py
file with the following content:
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
message = 'It works!\n'
version = 'Python %s\n' % sys.version.split()[0]
response = '\n'.join([message, version])
return [response.encode()]
Verification¶
Visit http://Your_Server_IP:Port/python/
in your browser and you should see:
It works!
Python x.x.x
Django Setup¶
Create Project¶
If you haven't installed Django, please do so through pip3
pip3 install django
Create a demo
project under the /var/www/html/
folder. Create the folder if it doesn't exist.
mkdir -p /var/www/html/
cd /var/www/html/
django-admin startproject demo
Django Settings¶
Edit settings.py
inside the inner folder (may be demo/demo
) to allow hosts:
ALLOWED_HOSTS = ['*']
The STATICFILES_DIRS
tells Django where to look for static files that are not tied to a particular app. In this case, we are instructing Django to also look for static files in a folder called static
in our root folder, not just in our apps.
STATIC_URL = '/python/static/'
STATIC_ROOT = '/var/www/html/demo/public/static'
Notes
/python/static/
depends on your context settings- Putting the
static
folder underpublic
is required
Collect static files in the directory where manage.py
exists (may be one level up):
mkdir -p public/static
python3 manage.py collectstatic
Create an admin super user:
python3 manage.py migrate
python3 manage.py createsuperuser
Change owner¶
chown -R nobody:nobody /var/www/html/demo
chown -R nobody:nogroup /var/www/html/demo
This example assumes you are using the default document root + /python/
. Navigate to Web Admin > Virtual Hosts > Context > Add, and set the following values:
- Type =
App Server
- URI =
/python/
- Location =
/var/www/html/demo/
- Binary Path =
/usr/local/lsws/fcgi-bin/lswsgi
- Application Type =
WSGI
- Startup File =
demo/wsgi.py
Django Setup in a Virtual Environment¶
Create Virtual Environment¶
Create the folder for the virtual environment django project if it doesn't exist:
mkdir -p /var/www/html/
Note
Putting the virtual environment outside of the web server config and document root folders is recommended.
Use the virtualenv
tool to create the virtual environment:
virtualenv /var/www/html/
Or specify the python version and give the virtual environment access to the system-site-packages directory:
virtualenv -p python3 --system-site-packages /var/www/html/
Access and Activate the virtual environment:
source /var/www/html/bin/activate
Verify that python loaded from the virtual environment:
which python
Create Project¶
pip
and python
already link to python version 3 in virtulenv, so if you haven't installed Django, try to install through pip
:
pip install django
Create the demo
project under /var/www/html/
:
cd /var/www/html/
django-admin startproject demo
Django Settings¶
Edit settings.py
inside the inner folder to allow hosts:
ALLOWED_HOSTS = ['*']
The STATICFILES_DIRS
tells Django where to look for static files that are not tied to a particular app. In this case, we are instructing Django to also look for static files in a folder called static
in our root folder, not just in our apps:
STATIC_ROOT = '/var/www/html/demo/public/static'
!!! note Putting the static
folder under public
is required.
Collect static files:
mkdir -p public/static
python manage.py collectstatic
Create admin super user
python manage.py migrate
python manage.py createsuperuser
Change owner¶
chown -R nobody:nogroup /var/www/html/demo
Set up Context¶
This example assumes you are using the default document root + /
. Navigate to Web Admin > Virtual Hosts > Context > Add, and set the following values:
- Type =
App Server
- URI =
/
- Location =
/var/www/html/demo/
- Binary Path =
/usr/local/lsws/fcgi-bin/lswsgi
- Application Type =
WSGI
- Startup File =
demo/wsgi.py
- Environment =
PYTHONPATH=/var/www/html/lib/python3.8:/var/www/html/demo
- Environment =
LS_PYTHONBIN=/var/www/html/bin/python
Be sure to substitute the listed python version (python 3.8) with your current one.
Verify Django¶
Visit http://Server_IP:Port/admin
in your browser and you should see the admin page, which displays the text Django administration
and asks you to enter a Username and Password.
Set up Flask with a Virtual Environment¶
Create Virtual Environment¶
Create the virtual environment:
virtualenv venv
Or do so while specifying the python version:
virtualenv -p python3 venv
Access and activate the virtual environment:
source venv/bin/activate
Verify that python loaded from the virtual environment:
which python
Create Project¶
pip
and python
already link to python version 3 in virtulenv, so if you haven't installed Flask, please try to install through pip
:
pip3 install flask
Create a demo
project under the virtual host root /var/www/html/
mkdir /var/www/html/demo
cd /var/www/html/demo
Flask example code¶
Create a directory named app
:
mkdir app
Create a file in the app
directory and name it __init__.py
.
vi /var/www/html/demo/app/__init__.py
Insert the flask example code into the file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello Flask!"
Create a WSGI file in the project and insert the following content, which will add your python files to the python path:
vi /var/www/html/demo/wsgi.py
#!/usr/bin/env python
import sys
sys.path.insert(0, '/var/www/html/demo')
from app import app as application
Change owner¶
chown -R nobody:nogroup /var/www/html/demo
Set up Context¶
This example assumes you are using the default document root + /
. Navigate to Web Admin > Virtual Hosts > Context > Add and set the following values:
- Type =
App Server
- URI =
/
- Location =
/var/www/html/demo/
- Binary Path =
/usr/local/lsws/fcgi-bin/lswsgi
- Application Type =
WSGI
- Startup File =
wsgi.py
- Environment =
PYTHONPATH=/var/www/html/lib/python3.8:/var/www/html/demo
- Environment =
LS_PYTHONBIN=/var/www/html/bin/python
Be sure to substitute the listed python version (python3.8) with your current one.
Verify Flask¶
Visit http://Your_Server_IP/
in your browser and you should see Hello Flask!
Optional Settings¶
Custom Binary Path¶
If you want to change the LSWSGI path, just update the Context and set Binary Path = /usr/lswsgi
.
Custom Startup File Name¶
If you want to change the default WSGI file name from passenger_wsgi.py
to example_wsgi.py
, just update the Context and set Startup File = example_wsgi.py
.