Differences

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

Link to this comparison view

Next revision
Previous revision
litespeed_wiki:other-ext-apps:django-web-framework-with-gunicornn [2017/08/30 10:04]
Usman Nasir created
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 53: Line 48:
 That means your Django project is working fine. On the console press ''​CONTROL-C''​ to stop the development server so that we can continue with our setup. Please note that this development server does not have to do anything with our setup, it is just to make sure that our Django project is running fine so that later we don’t run into any issues. That means your Django project is working fine. On the console press ''​CONTROL-C''​ to stop the development server so that we can continue with our setup. Please note that this development server does not have to do anything with our setup, it is just to make sure that our Django project is running fine so that later we don’t run into any issues.
  
 +
 +====== Step 2: Install Gunicorn ======
 +
 +One of the easiest ways to setup Django project in a production environment is by using gunicorn to handle the dynamic content and let LiteSpeed take care of static content and user connections.
 +
 +<​code>​easy_install gunicorn</​code>​
 +
 +Note: Can also use pip
 +
 +This will install gunicorn.
 +
 +====== Step 3: Setup Gunicorn! ======
 +
 +I am going to use gunicorn socket and enable gunicorn at system startup, you can have your own setup of gunicorn but LiteSpeed setup will not change, so it is completely up to you. We’ve to create 3 files.
 +
 +''/​etc/​systemd/​system/​gunicorn.socket''​
 +<​code>​
 +Description=gunicorn socket
 +[Socket]
 +ListenStream=127.0.0.1:​5003
 +[Install]
 +WantedBy=sockets.target
 +</​code>​
 +127.0.0.1:​5003 is the address we are going to send traffic to, you can change the port as per your liking.
 +
 +''/​etc/​systemd/​system/​gunicorn.service''​
 +<​code>​
 +[Unit]
 +Description=gunicorn daemon
 +Requires=gunicorn.socket
 +After=network.target
 +
 +[Service]
 +PIDFile=/​run/​gunicorn/​pid
 +User= someuser
 +Group= someuser
 +RuntimeDirectory=gunicorn
 +WorkingDirectory=/​home/​litespeed/​public_html/​djangoproject
 +ExecStart=/​usr/​bin/​gunicorn --pid /​run/​gunicorn/​pid ​  \
 +          --bind 127.0.0.1:​5003 djangoproject.wsgi
 +ExecReload=/​bin/​kill -s HUP $MAINPID
 +ExecStop=/​bin/​kill -s TERM $MAINPID
 +PrivateTmp=true
 +
 +[Install]
 +WantedBy=multi-user.target
 +</​code>​
 +This file contains four important elements.
 +
 +  * WorkingDirectory = ''/​home/​litespeed/​public_html/​djangoproject''​ (This is the directory where your Django project resides)
 +  * Second thing is ''​djangoproject.wsgi'',​ replace ''​djangoproject''​ 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.
 +  * Replace someuser with the user you want to run gunicorn as.
 +
 +''/​etc/​tmpfiles.d/​gunicorn.conf''​
 +
 +<​code>​d /​run/​gunicorn 0755 someuser somegroup -</​code>​
 +Substitute ''​user''​ and ''​group''​ under which gunicorn runs.
 +
 +With all these files completed, we can finally start this socket, start socket using:
 +
 +<​code>​systemctl start gunicorn.socket</​code>​
 +
 +Your socket is now ready to accept connections you can test the socket using:
 +
 +<​code>​curl http://​127.0.0.1:​5003</​code>​
 +
 +
 +You should have HTML printed on your console.
 +
 +
 +====== 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:04
  • by Usman Nasir