WebFaction
Community site: login faq
0
1

Hi,

Is there an easy way to use Django with gunicorn?

Or there are plans to add an application running by default with gunicorn?

asked 04 Dec '10, 10:25

Leandro A
613
accept rate: 0%


Looking at the Guncorn installation, you should have no problems installing on on your account using easy_install (and optionally, virtualenv), and then running it as a "Custom app listening on port".

We do not have any immediate plans to offer Gunicorn as a Control Panel installer, but if you need to perform multiple installations of it, you can consider making a custom install script (see http://docs.webfaction.com/xmlrpc-api/tutorial.html#packaging-the-install-script-for-the-control-panel).

permanent link

answered 04 Dec '10, 10:35

tie
1.4k13
accept rate: 44%

Great, I'll try creating a single app and if i works as expected will create the script. Thanks.

(04 Dec '10, 10:39) Leandro A

BTW, if anyone already did the install script and want to share it would be amazing.

(04 Dec '10, 10:41) Leandro A

I created a fabric script just for this, for anybody else wanting to install django/gunicorn on webfaction. https://github.com/ashwoods/webfaction-django-fabfile

Even if you don't use it directly you can see how it's done.

permanent link

answered 06 Oct '11, 10:38

ashwoods
111
accept rate: 0%

Thanks for sharing!

(06 Oct '11, 11:18) seanf

I had a hard time finding & reading various articles and putting it all together. Below is a summary for a Flask app but a similar approach can be used for Django.

If you don't need to custom configure Nginx settings, you can make use of WebFaction's system Nginx instance. It will save you shared RAM and CPU.

Overview

  • Create a 'Custom app (listening on port)' application to serve gunicorn
  • Create a 'Symbolic link to static-only app' application to serve static files
  • Create a 'Website'
  • Configure gunicorn to start the Flask app (or Django)
  • Configure supervisord to manage gunicorn
  • Configure cron to start supervisord

Create a 'Custom app (listening on port)' application

  • From WebFaction's main menu, select Domains/Websites > Application > Add new application
  • Name: MY_APP
  • App category: Custom
  • App type: Custom app (listening on port)
  • Click Save

Re-open the newly added app and take note of the port number in the 'Port' section. This article will use a value of 12345, which you will need to replace.

This article will assume that the file /home/USERNAME/webapps/MY_APP/src/app/__init__.py contains a WSGI App named application that can be started using gunicorn app:application

Create a 'Symbolic link to static-only app' application to serve static files

  • From WebFaction's main menu, select Domains/Websites > Application > Add new application
  • Name: MY_APP_static
  • App category: Symbolic link
  • App type: Symbolic link to static-only app
  • Extra info: /home/USERNAME/webapps/MY_APP/src/app/static
  • Click Save

Substitute USERNAME and MY_APP/src/app/static according to your environment.

Create a 'Website'

  • From WebFaction's main menu, select Domains/Websites > Websites > Add new website
  • Under the 'Contents' section
  • Add an application > Reuse an existing application > MY_APP
  • Add an application > Reuse an existing application
    • Application: MY_APP_static
    • URL: static
  • Click Save

Configure gunicorn to start the Flask app

(Optionally) create a virtualenv:

mkvirtualenv MY_APP    # create new virtualenv
workon MY_APP    # switch to virtualenv

Install gunicorn:

pip install gunicorn

Make note of the full path to gunicorn:

which gunicorn

You should be able to start the Flask app with gunicorn like so:

gunicorn -w 2 -b 0.0.0.0:12345 app:application

Substitute port 12345 according to your environment.

Type Control-C to exit gunicorn.

Configure supervisord to manage gunicorn

Supervisor is used to restart gunicorn whenever it closes unexpectedly.

We recommend installing supervisor outside of any virtualenvs so it can manage multiple Flask applications if needed

deactivate   # exit any virtualenvs
pip install supervisor

Make a note of where supervisord is installed:

which supervisord

Create a default supervisord.conf file:

cd ~
echo_supervisord_conf > ~/supervisord.conf

Edit ~/supervisord.conf and add the following stanza at the bottom of this file:

[program:my_app]
user=USERNAME
directory=/home/USERNAME/webapps/MY_APP/src
command=/FULL/PATH/TO/gunicorn -w 2 -b 0.0.0.0:12345 "app:application"
autostart=true
autorestart=true
redirect_stderr=true

Make sure to use fully qualified paths and substitute FULL/PATH/TO, port 12345, USERNAME, and MY_APP/src according to your environment.

You can start supervisor with:

supervisord -c ~/supervisord.conf

Which should start gunicorn

Configure cron to start supervisord

Cron can start supervisord after the host system reboots by adding a @reboot entry to cron:

Edit the crontab with:

crontab -e

Add the following line at the top of the crontab file:

@reboot /home/USERNAME/bin/supervisord -c /home/USERNAME/supervisord.conf

Make sure to use fully qualified paths and substitute USERNAME according to your environment.

permanent link

answered 21 Sep '17, 19:57

lingthio
112
accept rate: 0%

edited 21 Sep '17, 20:21

I'd like to contribute some notes to this as well as it took me a bit of time to get it all the pieces of the puzzle together. Once working it ended up be pretty straight forward.

My setup was pipenv, django, gunicorn. I didn't go with the traditional approach of requirements.txt, but stuck strictly to the pipenv setup and it turned out pretty well. So with that said here goes:

Step 0 - Gunicorn

  • Include gunicorn in your pipenv project. This means its installed when you pipenv install in your project directory. Your pipfile keeps up with your gunicorn version. You are NOT using one installed outside your project. This step should happen in your dev environment, not on the server.

Step 1 - Install pipenv

  • ssh into your account
  • easy_install-3.6 pipenv

Step 2 - Create a 'Custom app (listening on port)' application

  • Assume "mydjangoproj" is your webfaction app name and django project name
  • Follow lingthio's steps above, make note of the port number
  • Whatever your workflow is, get your app into this folder. For good measure assume /home/username/webapps/mydjangoproj directory contains your pipenv files, manage.py, and the directory mydjangoproj with wsgi.py. This is essentially a the django 2 layout.
  • Configure whatever settings you have to get your app ready... be sure to run pipenv install

Step 3 - Setup Server Script

I've modified a version of this script to start/stop/restart gunicorn with pipenv. I added this file to my project.

#!/bin/sh

# Usage: ./server_ctl { start | stop | restart }
# server_ctl is a management script for gunicorn with pipenv
# It is designed to work on the WebFaction platform with minimal effort.
# Be sure gunicorn is installed within your pipenv project

# Default params
RUNAPP="mydjangoproj.wsgi"
RUNHOST="0.0.0.0:12345"
RUNARGS="-w 3" # http://docs.gunicorn.org/en/stable/settings.html
PIDFILE="gunicorn.pid"

# Don't edit below here --------------------------------------------------------
THISDIR="$(cd "$(dirname "$0")"; pwd)" # get full dir path to this script

cd $THISDIR

PYPATH="$(pipenv --venv)"
RUNARGS="$RUNARGS --pythonpath $PYPATH --daemon -b $RUNHOST -p $PIDFILE $RUNAPP"

start_server () {
  if [ -f $1 ]; then
    if [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
       echo "A server is already running on $RUNHOST"
       return
    fi
  fi
  echo "starting ${RUNHOST}"
  RUNSRV="pipenv run gunicorn $RUNARGS"
  echo $RUNSRV
  eval $RUNSRV
}

stop_server (){
  if [ -f $1 ] && [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
    echo "stopping server $RUNHOST"
    kill -9 `cat $1`
    rm $1
  else
    if [ -f $1 ]; then
      echo "server $RUNHOST not running"
    else
      echo "No pid file found for server $RUNHOST"
    fi
  fi
}

case "$1" in
'start')
  start_server $PIDFILE
  ;;
'stop')
  stop_server $PIDFILE
  ;;
'restart')
  stop_server $PIDFILE
  sleep 2
  start_server $PIDFILE
  ;;
*)
  echo "Usage: $0 { start | stop | restart }"
  ;;
esac

exit 0

You should only need to modify the first 4 variables. , primarily the RUNAPP to change "mydjangoproj" and RUNHOST to change the port number. You may need to chmod +x server_ctl.sh

You should now be able to run ./server_ctl.sh start to start your gunicorn.

Step 4 - Create a 'Symbolic link to static-only app'

  • Just like in lingthio's post above point it to your static folder

Step 5 - Create a Website

  • Just like in lingthio's post above create a website with these two applications

Step 6 - Setup Crontab

  • As per Webfaction's recommended way to make sure the custom app (gunicorn) stays running setup a crontab to call this server_ctl.sh script every so often. The script will only start gunicorn if its not running. This checks every 5 minutes, but you can check as often as you like.

*/5 * * * * /home/username/webapps/mydjangoproj/server_ctl.sh start > /dev/null 2>&1

permanent link

answered 11 hours ago

trentrichardson
32
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×899
×12

question asked: 04 Dec '10, 10:25

question was seen: 5,962 times

last updated: 11 hours ago

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM