Deploying Django with OpenLiteSpeed (Part 3)

| |

In this part we will test our WSGI Api created in Part 1 by setting up Context. Context is virtual location under the parent url of virtual host. We can point different locations to the sub urls. Here we use context to mount our WSGI application to “/” directory.

Set up Context

Click the virtual hosts section in Web Admin panel then open the virtual host we created earlier (social in our case). Navigate to Context and then click Add “+” icon.

Click Next then you’ll get configuration form. Choose URI that you want to map to this context. We will be uploading the project to Document Root folder (/usr/local/lsws/social/public).

Set the binary file as:

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

And finally from the dropdown choose Application Type as WSGI. The Binary path field is set to wsgi.py as default so you can also leave it blank for now. But when we will upload project you will need to map the location to wsgi.py file of the project. All other fields are optional. Now save the configuration.

Create WSGI.py file

Its time to get back to the console. Navigate to the root folder of the virtual host.

$ cd /usr/local/lsws/YOUR_VHOST/public

Now make a file named wsgi.py

$ nano wsgi.py

Paste this content and save

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()]

Now restart the Openlitespeed server.

$ /usr/local/lsws/bin/lswsctrl restart

Navigate to http://You_domain/ in the browser

Great!! Everything is working fine. Openlitespeed successfully served your simple Python wsgi app.

Review settings.py Before Deployment

Its time to refactor the settings.py file of your project. I suggest to set SECRET_KEY to environment variable instead of hardcoding. Another option is DEBUG which should be set to False during production.

ALLOWED_HOSTS option allow you to restrict the access of Django app within the domain provided to it as list of string. Setting it to [‘*’] will allow all domain to access the app. It is always recommended to use your exact domain. You can allow all the subdomains by adding a dot(.) before the main domain. Below here is snippet of the settings I have refactored.

import os 
SECRET_KEY = os.environ['SECRET_KEY']

DEBUG = False

ALLOWED_HOSTS = [ '.tejars.com' ]  #Replace this with your domain. Here I'm allowing all subdomains from tejars.com

Check out the official Django deployment checklist for better security and performance options.

If you were using sqlite during development but at production you might you other database backends, such as MariaDB, MySQL, Oracle, or PostgreSQL. In this tutorial we’ll use Postgres with Django. Lets change the DATABASE setting to use postgres instead of sqlite.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Save the settings.py file. Now you are a step behind for deployment of Django app. In Next Part we’ll upload our project using filezilla client, and install Postgres data base and configure it.

Previous

Deploying Django with OpenLiteSpeed (Part 2)

Deploying Django with OpenLiteSpeed (Part 4)

Next
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Paul Lovan

awesome

[…] We have configured a lot in Openlitespeed. Now we will check the WSGI with a simple python script in the Part 3. […]

2
0
Would love your thoughts, please comment.x
()
x