Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:other-ext-apps:django-web-framework-with-gunicornn [2017/08/30 10:08]
Usman Nasir
litespeed_wiki:other-ext-apps:django-web-framework-with-gunicornn [2017/08/30 18:41] (current)
Usman Nasir [Step 6: Static Content!]
Line 3: Line 3:
 On a shared hosting environment with cPanel/WHM installed, we can use Litespeed Enterprise to host Django app on per account basis by proxying content to gunicorn. On a shared hosting environment with cPanel/WHM installed, we can use Litespeed Enterprise to host Django app on per account basis by proxying content to gunicorn.
  
-==== Requirments ==== 
- 
-  * You must have a server with cPanel/WHM installed. 
-  * One live domain. 
-  * Litespeed Enterprise installed on top of WHM. 
  
 ====== Step 1: Create Django Project ====== ====== Step 1: Create Django Project ======
Line 88: Line 83:
 PIDFile=/​run/​gunicorn/​pid PIDFile=/​run/​gunicorn/​pid
 User= someuser User= someuser
-Group=someuser+Group= someuser
 RuntimeDirectory=gunicorn RuntimeDirectory=gunicorn
-WorkingDirectory=/​root/litespeedtech+WorkingDirectory=/​home/litespeed/​public_html/​djangoproject
 ExecStart=/​usr/​bin/​gunicorn --pid /​run/​gunicorn/​pid ​  \ ExecStart=/​usr/​bin/​gunicorn --pid /​run/​gunicorn/​pid ​  \
-          --bind 127.0.0.1:​5003 ​litespeedtech.wsgi+          --bind 127.0.0.1:​5003 ​djangoproject.wsgi
 ExecReload=/​bin/​kill -s HUP $MAINPID ExecReload=/​bin/​kill -s HUP $MAINPID
 ExecStop=/​bin/​kill -s TERM $MAINPID ExecStop=/​bin/​kill -s TERM $MAINPID
Line 102: Line 97:
 This file contains four important elements. This file contains four important elements.
  
-*WorkingDirectory = /root/litespeedtech +  ​* WorkingDirectory = ''​/home/litespeed/​public_html/​djangoproject''​ (This is the directory where your Django project resides) 
-   *This is the directory where your Django project resides. +  * Second thing is ''​djangoproject.wsgi''​, replace ​''​djangoproject'' ​with your project name. 
-*Second thing is litespeedtech.wsgi, replace ​litespeedtech ​with your project name. +  * –bind 127.0.0.1:​5003 : Make sure you use the same address port combination that you have used in the socket file. 
-*–bind 127.0.0.1:​5003 : Make sure you use the same address port combination that you have used in the socket file. +  * Replace someuser with the user you want to run gunicorn as.
-*Replace someuser with the user you want to run gunicorn as.+
  
 ''/​etc/​tmpfiles.d/​gunicorn.conf''​ ''/​etc/​tmpfiles.d/​gunicorn.conf''​
Line 125: Line 119:
  
  
 +====== Step 4: Define External Webserver ======
 +
 +Now we've to create an External Webserver app from Litespeed ​ Webadmin console in the WHM.
 +
 +Go to: Configurations -> Server -> External App -> Add
 +
 +{{ :​litespeed_wiki:​other-ext-apps:​define-external-web-app.png?​600 |}}
 +
 +From drop drop down select ''​Webserver''​ click ''​Next''​
 +
 +{{ :​litespeed_wiki:​other-ext-apps:​create-webserver-app.png?​600 |}}
 +
 +Make sure your external webserver settings looks like these:
 +
 +{{ :​litespeed_wiki:​other-ext-apps:​external-web-app-settings.png?​600 |}}
 +
 +
 +Once done, click ''​Save'',​ you are now ready to tell litespeed to send Django related traffic to this external app.
 +
 +
 +
 +====== Step 5: Setup Rewrite Rules! ======
 +
 +We can now use rewrite rules to send traffic to gunicorn backend webserver we just created above.
 +
 +<​code>​
 +nano /​home/​litespeed/​public_html/​.htaccess
 +</​code>​
 +
 +Now paste following in your ''​.htaccess''​ file:
 +
 +<​code>​
 +RewriteEngine On
 +RewriteCond %{ORG_REQ_URI} !/static
 +RewriteRule ^(.*)$ http://​gunicorn/​$1 [P]
 +</​code>​
 +
 +This tells litespeed to send all traffic to gunicorn backend except the static content, in next step we will inform litespeed where to find the static content.
 +
 +
 +====== Step 6: Static Content! ======
 +
 +Like all other web applications Django have static files as well (CSS,​Javascript). Gunicorn will only process dynamic requests, it don't know how to serve static content, we will first collect static content of django app in one single folder.
 +
 +<​code>​
 +cd /​home/​litespeed/​public_html/​djangoproject
 +python manage.py collecstatic
 +</​code>​
 +
 +This will create a new folder ''​static''​ under ''/​home/​litespeed/​public_html/​djangoproject'',​ we can either use rewrite rules to move all requests for static content to this location or we can move this folder to the parent folder where Litespeed will actually look for the static files at ''/​home/​litespeed/​public_html''​
 +
 +<​code>​
 +mv static ..
 +</​code>​
 +
 +This will move folder ''​static''​ to its parent folder, and LiteSpeed can now easily serve static content. That's all, you can now visit your domain and it should start serving your Django project.
  • Admin
  • Last modified: 2017/08/30 10:08
  • by Usman Nasir