Deploying a Tornado project in production using Github and WebFaction.

In the following I’m using a server from Webfaction, and my Tornado source code is hosted on Github. Some things are specific to those providers, but should be very easy to adapt. The following is organized into two sections.

The first section (basic server setup) assumes a fresh production server without anything installed. You may skip this step.

The next section (running tornado in production) shows how to I configure Python, install Supervisord to manage a tornado server and start up the server.

Basic server setup

(Webfaction server and Github website) Configure SSH keys

Start by logging into your webfaction instance via SSH.

ssh you@your-webfaction-ip-address

Next, generate an SSH key to use with Github:

ssh-keygen -t rsa -C "me@example.com"
chmod 600 ~/.ssh/id_rsa.pub

Add key to your Github SSH key settings

(Webfaction server) Install pip

I use Python 2.7. You can adjust the following to match other versions.

Because Webfaction is shared hosting, I create a temp directory inside my home directory. This solves some problem when installing packages with pip (installed next):

mkdir -p $HOME/tmp
vi ~/.bash_profile
# add line to end of file: export TEMP=$HOME/tmp
# save file and quit vi
source ~/.bash_profile

Install pip:

easy_install-X.Y pip

Running tornado in production

(Webfaction server) Install python packages supervisor and tornado

Install supervisor and tornado using pip into server Python 2.7. I normally don’t bother with a virtual environment:

pip-2.7 install supervisor
pip-2.7 install tornado

(Webfaction control panel and Webfaction server) Create basic tornado website

In the WebFaction control panel: Create a custom application that listens on a port. Note the port that it gives you (in the following assume that the application was assigned to port 22222).

This creates a folder under ~/webapps/APPLICATION-NAME. Let us assume that APPLICATION-NAME is “my_tornado”.

Next create a website and assign the application to that website.

Clone the github code into the webapp/my_tornado directory:

cd ~/webapps/
git clone git@github.com:your-account/your-repository.git my_tornado

Assume that the code has a simple my_tornado.py that looks like this:

import os.path
 
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
 
from tornado.options import define, options
 
define("port", default=8080, help="run on the given port", type=int)
 
class IndexHandler(tornado.web.RequestHandler):
 
	def get(self):
		self.write('Hello, World')
 
if __name__ == '__main__':
		tornado.options.parse_command_line()
 
		app = tornado.web.Application(
			[
				(r'/', IndexHandler)
			], **settings
		)
		http_server = tornado.httpserver.HTTPServer(app)
		http_server.listen(options.port)
		tornado.ioloop.IOLoop.instance().start()

Try starting the server:

python2.7 tornado_server.py --port=22222

You should now be able to browse the website. Press ctrl-c to quit the server again. We’ll start it up using supervisor in the following.

(Webfaction server) Configure supervisor to keep the tornado server running

Make a directory to hold supervisor configuration files:

mkdir -p ~/etc/supervisord

Edit main supervisor configuration file:

vi ~/etc/supervisord.conf

Add the following content:

[unix_http_server]
file=/home/you/tmp/supervisor.sock
 
[supervisord]
logfile=/home/you/tmp/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10 
loglevel=info
pidfile=/home/you/tmp/supervisord.pid supervisord.pid)
 
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 
[supervisorctl]
serverurl=unix:///home/you/tmp/supervisor.sock
 
[include]
files = /home/you/etc/supervisord/*.ini

Create an ini-file for the tornado server:

vi ~/etc/supervisord/my_tornado.ini

Add the following content:

[program:my_tornado]
command=python2.7 /home/kostask/webapps/my_tornado/code/my_tornado.py --port=22222

Start supervisord:

supervisord

You should now be able to connect to your simple Tornado server again, displaying the message “Hello, World”.

What to do if your code is updated

Pull the latest code from Github:

cd ~/webapps/my_tornado
git pull

You need to restart the tornado server for the changes to take effect:

supervisordctl restart my_tornado

Improving the setup

In order to get better performance out of Tornado, I’ll use nginx as a load balancer between several tornado processes. To do this:

  1. Install nginx in home directory
  2. Start multiple tornado servers
  3. Configure nginx to load balance between the tornado servers

I’ll expand this section when I have more experience with it.

3 Replies to “Deploying a Tornado project in production using Github and WebFaction.”

  1. Hi good day,

    nice tutorial!

    any chance u have an updated version that includes running the site via Nginx and using Supervisor for monitoring ?

    Thanks and keep up the great work !

Leave a Reply