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.
Next, generate an SSH key to use with Github:
ssh-keygen -t rsa -C "firstname.lastname@example.org" 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
Running tornado in production
(Webfaction server) Install python packages
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 email@example.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:
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:
Add the following content:
[program:my_tornado] command=python2.7 /home/kostask/webapps/my_tornado/code/my_tornado.py --port=22222
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:
- Install nginx in home directory
- Start multiple tornado servers
- Configure nginx to load balance between the tornado servers
I'll expand this section when I have more experience with it.