Django

The OpenLiteSpeed Django One-Click app automatically installs performance web server openlitespeed, and Django. This image tends to be up to 2.5 times faster than Django on Nginx with Gunicorn! Developers can update files directly without using the built-in development server. OpenLiteSpeed features easy setup for SSL and RewriteRules. OLS is flexible and also supports Node.js and Ruby apps, as well as CMSs like WordPress.

Quick Start

Step 1.

DigitalOcean

Use the "OpenLiteSpeed Django One-Click app" to create a Droplet with any plan you want

AWS
Method 1. Through Subscribe
  1. Subscribe to Django
  2. Click Continue to Launch and Launch
Method 2. Through EC2 console
  1. Open the Amazon EC2 console
  2. Locate the product by searching for "WordPress with LiteSpeed Cache" from AWS Marketplace
  3. Click Select,Continue,Preview and launch (the default is the most recent version)

Step 2.

Visit the default script by entering http://Server_IP:80/python/ on your browser and you should see Hello, world!. You can also enter http://Server_IP:80/python/admin/ to get the Django administration page

Step 3.

Start editing the Django project under /usr/local/lsws/Example/

demo
|-demo
   │-settings.py
   │-urls.py
   │-wsgi.py
   │-__init__.py
|-manage.py
|-public
   |-static
|-db.sqlite3
|-app
|-stderr.log

Update system software.

sudo apt-get update && sudo apt-get upgrade -y

Your system is installed and ready to use!

Components

The OpenLiteSpeed Django One-Click Droplet installs several packages and performs other actions on your system.

Package Installation

Component Version
Linux Ubuntu 18.04.1
OpenLiteSpeed Latest from LiteSpeedtech Repo
Django Latest from APT
ACME Latest from GitHub

Other Actions

Enable Firewall

This droplet enables the UFW firewall to allow only SSH (port 22), HTTP (port 80) and HTTPS (port 443) access.

Set Up Demo Django Project

The droplet executes the following steps in order to set up a demo Django project called demo, and a Django app called app. These steps are taken automatically. You don't need to do anything. * Create a demo project called demo and Django app called app:

django-admin startproject demo
cd demo
python3 manage.py startapp app

  • Edit settings.py to allow hosts:

    ALLOWED_HOSTS = ['*'] 
    

  • Edit settings.py to look for static files in a directory called static in our root directory:

    STATIC_URL = '/python/static/'
    STATIC_ROOT = '/usr/local/lsws/Example/demo/public/static'
    

  • Collect static files:

    mkdir -p public/static
    python3 manage.py collectstatic
    

  • Create a view which returns Hello, world! as an HTTP response:

    def index(request):
        return HttpResponse("Hello, world!")
    

  • Set a default url:
    urlpatterns = [
        path('', views.index, name='index'),
        path('admin/', admin.site.urls),
    ]
    
  • Change owner:

    chown -R nobody:nogroup /usr/local/lsws/Example/demo
    

  • Set up a context via Web Admin > Virtual Hosts > Context > Add:

    • Type = App Server
    • URI = /python/
    • Location = /usr/local/lsws/Example/demo/
    • Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
    • Application Type = WSGI
    • Startup File = demo/wsgi.py

Benchmark Comparison

Use the following command to test from an x-CPU plan server(ab) to an x-CPU plan server(DOMAIN)

ab -n 100000 -k -H "Accept-Encoding: gzip,deflate" -c 100 http://DOMAIN/

Requests per Second (The larger the number, the better)

Nginx+Gunicorn Openlitespeed
700 1750

How to Access the Installed Software

From a terminal on your local computer, connect to the server as root, like so:

ssh root@use_your_server_ip
AWS
ssh ubuntu@use_your_server_ip

Be sure to substitute the server’s public IP address for use_your_server_ip.

Web Server Control Panel Access

Get the WebAdmin admin password:

cat .litespeed_password
Visit https://use_your_droplet_ip:7080 to access WebAdmin in a browser.

By default, WebAdmin uses port 7080. To allow access to 7080 from your IP(e.g. 1.2.3.4):

ufw allow from 1.2.3.4 to any port 7080
You can also allow all IPs access to port 7080:
ufw allow 7080
We suggest turning this port off once you've finished setup:
ufw delete allow 7080

Django Administration Page

Run the following command in the project directory to setup an account and password:

python3 manage.py createsuperuser

Optional Setup

Enable HTTPS

Setting up an SSL certificate enables HTTPS on the web server, which secures the traffic between the server and the clients connecting to it. ACME is the protocol used by Let's Encrypt to set up free SSL certificates on a server.

Step 1. Register Domain

Before appling any certificate, you’ll need a registered domain name and DNS records:

  • An A record from the domain (e.g., example.com) to the server’s IP address

Step 2. Generate the Certificate

Once the DNS records are set up, you can generate the SSL certificate. Be sure to substitute the correct domain name in the following command:

acme.sh --issue -d example.com -w /usr/local/lsws/Example/html/
If certificate verification is a success, you should find your certificate files stored in .acme.sh/example.com/

Step 3. Set SSL for HTTPS

Screenshot

Navigate to OpenLiteSpeed Web Server WebAdmin > Listeners > SSL, and edit the following three items:

  • Private Key File = /root/.acme.sh/example.com/example.com.key
  • Certificate File = /root/.acme.sh/example.com/fullchain.pem
  • Chained Certificate = Yes

Save and perform a Graceful Restart.

Note

Change /root if you have different home folder

Now your server should support TLS1.1, TLS 1.2, and TLS 1.3.

Step 4. Redirect HTTP to HTTPS (Optional)

Screenshot

HTTPS traffic on port 443 is already allowed through the firewall. After you set up HTTPS, you can optionally rewrite all HTTP traffic to HTTPS.

Add the following rules to OpenLiteSpeed Web Server WebAdmin > Virtual Hosts > Rewrite > Rewrite Rules

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

Method for Uploading Files

You can serve files from the web server by adding them to the web root using SFTP or other tools.

Frequently Asked Questions

How do I Reset my Web Server WebAdmin Password?

If you forget your password, you may run the following command to reset it:

/usr/local/lsws/admin/misc/admpass.sh

It will ask for the WebAdmin username, which should be admin. Then, enter your new password.

How do I Create Additional Virtual Hosts?

By default we have an example virtual host already. You can create more virtual hosts if you like. See Create Virtual Hosts on OpenLiteSpeed.

How do I Create Additional Apps by Context?

How do I Change the Django Startup File?

If you want to change the default startup file name from wsgi.py to demo.js, just update the Context and set Startup File = demo.py.

How do I Create Django with a Virtual Environment?

How do I Fix Permission Issues?

chown -R nobody:nogroup /usr/local/lsws/Example/demo

Change /usr/local/lsws/Example/demo to your actual project path

How do I Import my Django Project From Git?

Let's use pyconbalkan as an example. To import the project, run the following command:

git clone https://github.com/PythonBalkan/pyconbalkan
Install your project's requirment:
pip3 install -r requirements.txt
Create an .env file, and the content required by the project.

Run migrate:

python3 manage.py migrate

Follow these instructions to set up and collect static files.

Follow these instructions to change the owner.

Follow these instructions to set up a context.

Following a successful import, you should see the following screen:

How do I Upgrade ACME?

You can update acme.sh to the latest code with the following command:

acme.sh --upgrade

API Creation

DigitalOcean

In addition to creating a Droplet from the Openlitespeed Django One-Click application using the control panel, you can also use the DigitalOcean API.

The following example creates an Openlitespeed Django 18.04 Droplet called “My-Droplet” in the NYC3 datacenter, with 1 GB RAM:

curl -X POST "https://api.digitalocean.com/v2/droplets" \
    -d'{"name":"My-Droplet","region":"nyc3","size":"s-1vcpu-1gb","image":"openlitespeed-django-18-04"}' \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json"

Note

This request, like any other request that makes a change to your account, requires that your token has “write” scope assigned to it.