I have been working on a project that requires a web server that can scale to tens of thousands of simultaneous connections. In my research I cam across Tornado an open source version of the scalable, non-blocking web server and tools that power FriendFeed.
One of the easiest ways to get started using Tornado is to use virtualization to install it on one of your favorite Unix like platforms. Let’s do a little storm chasing and get Tornado setup.
Here is the software we will be using in this article:
We will be installing this on a MacBook Pro 2.4 GHz i5 with 8 GB running OS X Lion (10.7.3)
We will be downloading the latest server version which at the time of writing is ubuntu-11.10-server-amd64.iso. We are going to save this iso to a folder in our home directory which for this article is ~/Projects/storm_chasing_tornado
Will will be grabbing the latest version which at the time of writing is VirtualBox-4.1.8-75467-OSX.dmg. We will also be saving this dmg to the same folder in our home directory and then running the installer.
* Choose the right installer for your platform if you are not using a Mac, but why are you not using a Mac?
Now that we have VirtualBox installed we need to create a new virtual machine.
From the Virtual Box Manager we will select New icon.
This will launch the New Virtual Machine Wizard. Click Continue.
Next we will name our VM, let’s use Ubuntu Server 11.10. This will make it easy to identify if we decided to install other Ubuntu versions. Click Continue.
For memory will go with the default 512 MB which will be more than enough for our purposes. Click Continue.
Now it is time to create the hard disk. When we click on Continue it wil launch the virtual disk creation wizard.
Since we will not be using any other virtualization software we will go with the default VDI (VirtualBox Disk Image). Click Continue.
We will keep the default of dynamically allocated virtual disk so our Ubuntu instance will only use disk space as it is needed. Click Continue.
Let’s bump up the default disk size from 8 MB to 16 MB for a good initial disk size. Click Continue.
Our virtual disk is ready to be created. Click Create.
Here is the summary of our virtual machine that will created. Click Create.
Now that our Virtual Machine is setup we need to configure one more thing before we install Ubuntu. To be able to access our Virtual Machine from our host OS we need configure the VM VirtualBox Network Adapter to use a Bridged adapter. From the main Virtual Box Manager click on the Network link then select the Network tab.
We are going to change “Attached to” from NAT to Bridged Adapter. Click Ok.
Installing Ubuntu Server
Excellent our Virtual Machine is installed and configured now let’s get Ubuntu installed. From the VirtualBox Manager we will click on the Start button.
This will launch the First Run Wizard. Click Continue.
Now we will select the ubuntu-11.10-server-amd64.iso that we downloaded earlier to ~/Projects/storm_chasing_tornado then click Continue.
With the media selected we can now click Start.
The next steps are the same regardless if installing Ubuntu Server on a VM or to bare metal: Installing Ubuntu Server Oneiric Ocelot.
Now that we have Ubuntu Server installed we are going to login directly to our VM within VirtualBox.
Ubuntu 11.10 webserver tty1 webserver login: carlos Password: Welcome to Ubuntu 11.10 (GNU/Linux 3.0.0-12-server x86_64) * Documentation: https://help.ubuntu.com/11.10/serverguide/C System information as of Sat Feb 18 22:20:31 MST 2012 System load: 0.03 Processes: 59 Usage of /: 6.8% of 15.00GB Users logged in: 1 Memory usage: 24% IP address for eth0: 192.168.0.15 Swap usage: 0% Graph this data and manage this system at https://landscape.canonical.com/ Last login: Sat Feb 18 18:46:05 2012 carlos@webserver:~$
To make things simpler we are going to install open ssh on our server which will allow us to SSH from our host OS just like if we were accessing a real remote server.
carlos@webserver:~$ sudo apt-get install openssh-server ...
Before we logout of our VM we just need to find out the IP address of this server by using the command ifconfig
carlos@webserver:~$ ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:bd:69:30 inet addr:192.168.0.15 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:febd:6930/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8641 errors:0 dropped:0 overruns:0 frame:0 TX packets:2465 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11582133 (11.5 MB) TX bytes:183986 (183.9 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) carlos@webserver:~$ logout ... webserver login:
Remote Server Access
Now that we have our VM server all setup we can now do everything locally from our host OS terminal. We just need to open a terminal window and SSH to our Ubuntu Server VM.
ssh email@example.com The authenticity of host '192.168.0.15 (192.168.0.15)' can't be established. RSA key fingerprint is 78:9f:c8:f2:23:5c:36:d3:9d:2c:2e:c3:c3:63:af:3c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.15' (RSA) to the list of known hosts. firstname.lastname@example.org's password: Welcome to Ubuntu 11.10 (GNU/Linux 3.0.0-12-server x86_64) * Documentation: https://help.ubuntu.com/11.10/serverguide/C System information as of Sat Feb 18 22:20:31 MST 2012 System load: 0.03 Processes: 59 Usage of /: 6.8% of 15.00GB Users logged in: 1 Memory usage: 24% IP address for eth0: 192.168.0.15 Swap usage: 0% Graph this data and manage this system at https://landscape.canonical.com/ Last login: Sat Feb 18 18:46:05 2012 carlos@webserver:~$
carlos@webserver: ~$ sudo apt-get install git carlos@webserver: ~$ git clone https://github.com/facebook/tornado.git carlos@webserver: ~$ cd ./tornado/
carlos@webserver:~/tornado$ python setup.py build carlos@webserver:~/tornado$ sudo python setup.py install
To ensure that everything is working we are going to create a simple test app.
carlos@webserver:~/tornado$ cd carlos@webserver: ~$ sudo nano chasing_tornado.py ... import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("""<h2>Storm Chasing Tornado...</h2> <pre> *********** ********* ******* ***** *** ** * </pre> """) application = tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
We will now start up Tornado to serve our application.
$ python chasing_tornado.py
Using the IP address of our VM, we just need to open a browser window
Now that we have our basic web server setup with Tornado we can start building more interesting applications..