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%20A's gravatar image

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's gravatar image

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 Leandro%20A's gravatar image

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

(04 Dec '10, 10:41) Leandro A Leandro%20A's gravatar image

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's gravatar image

ashwoods
111
accept rate: 0%

Thanks for sharing!

(06 Oct '11, 11:18) seanf ♦♦ seanf's gravatar image

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, 19:57

lingthio's gravatar image

lingthio
112
accept rate: 0%

edited 21 Sep, 20:21

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:

×888
×12

question asked: 04 Dec '10, 10:25

question was seen: 5,391 times

last updated: 21 Sep, 20:21

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