Running a Python Application with LSAPI

LiteSpeed SAPI is the easiest and fastest way to run web applications with LiteSpeed Web Server. LiteSpeed 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. It is important to note that, while most Python applications are WSGI-compatible, not all are.

This wiki will go over how to set up LSWS to run Python applications with LWSGI. This wiki assumes that you already have a working version of Python installed and virtual hosts set up to run it on.

Download the WSGI LSAPI module

The module can be downloaded from our site here.

Compile Python with the Module and Prepare the Executable

Download the module to your server root, /usr/local/lsws. To recompile Python, go into the module directory and run the following commands:

cd /usr/local/lsws/wsgi-lsapi-1.6
python ./configure.py
make

Copy the file lswsgi to /usr/local/lsws/fcgi-bin/. (This directory is where all of your LSAPI executables go.)

cp lswsgi /usr/local/lsws/fcgi-bin/

Now your LSWSGI executable is ready for LiteSpeed Web Server.

Setting up the External Application

This process is the same as with any other LSAPI application, except with a different executable and suffix.

Go to the External App tab (WebAdmin console > Configuration > External App) and add an external application. (This can be done at the server or virtual host level.)

Select type LSAPI App.

Give it a name and a socket. Most importantly, for the Command setting, specify the location of your lswsgi executable: /usr/local/lsws/fcgi-bin/lswsgi. (You also have to pick values for Max Connections, Initial Request Timeout, and Retry Timeout.)

In the Environment field put:

LSAPI_CHILDREN=35

Set Instance to 1.

You now have an LSWSGI external application.

The configuration looks like:

  <extProcessor>
    <type>lsapi</type>
    <name>wsgiapp</name>
    <address>uds://tmp/lshttpd/lswsgi.sock</address>
    <maxConns>35</maxConns>
    <env>LSAPI_CHILDREN=35</env>
    <initTimeout>60</initTimeout>
    <retryTimeout>0</retryTimeout>
    <respBuffer>0</respBuffer>
    <autoStart>1</autoStart>
    <path>/usr/local/lsws/fcgi-bin/lswsgi</path>
    <instances>1</instances>
  </extProcessor>

Setting up the Script Handler

You'll need to create a WSGI script handler to tell LSWS to send .wsgi scripts to your LSWSGI application. Go to the Script Handler tab (WebAdmin console > Configuration > Script Handler) and click Add. (This, also, can be done at the server or virtual host level.)

Stipulate wsgi as the suffix of your WSGI Python apps. Pick Handler Type LiteSpeed SAPI and, for Handler Name, choose the LSWSGI external application you just created. (If you want scripts other than .wsgi to use Python, you can make extra script handlers to handle those scripts.)

Now your LSWS can handle .wsgi scripts and knows where to send them.

Graceful Restart to Apply Changes

To test this, use the simple hello world script below:

def application(environ, start_response):
  start_response('200 OK', [('Content-Type', 'text/plain')])
  return ['Hello World!\n']

Name your file hello.wsgi and save it to /usr/local/lsws/DEFAULT/html. This is the document root for LSWS's default vhost, “Example.”

Now, when you point your browser to localhost:8088/hello.wsgi, you will see the correct output displayed. It works!

  • Admin
  • Last modified: 2020/08/14 18:38
  • by Jackson Zhang