Friday, February 22, 2013

Setting up Python and Bottle on Ubuntu 12.04

Here are the steps I followed to get Python and the Bottle web framework working on an Amazon Web Services (AWS) Ubuntu server:

  • Log in to AWS and create a new instance. Select the AMI Ubuntu Server 12.04.1 LTS, 64-bit. If you wish to minimize costs, set the instance type to T1 Micro. Accept AWS's default settings for the instance.
  • Configure the instance's security group to, at a minimum, allow access from your own IP address to ports 22, 80 and 5901.
  • Use an SSH client to connect to the instance, logging in as the user ubuntu.
  • This AMI comes with Python pre-installed. You can verify this by entering the command python. The response should indicate that Python 2.7.3 is running. Enter quit() to exit Python.
  • Install Bottle: sudo apt-get install python-bottle.
  • Install a VNC server so you'll be able to run a web browser on your Ubuntu server. See my blog post, VNC server for headless Ubuntu 12.04 and VNC client for Windows 7, for details.
  • Install a web browser: sudo apt-get install firefox.
  • Create a directory for your Python files. I called mine python_test: mkdir python_test.
  • Change to that directory: cd python_test.
  • Create a Bottle "hello world" application. I found an example here and modified it slightly. Use your favoirte text editor to create a file named hello.py as follows:
 from bottle import route, run

@route('/hello')
def hello():
    return "Hello World!"

run(host='localhost', port=8080)                                   
  • Execute this Python script: python hello.py.
  • Use a VNC client to connect to your server. In the VNC client, open Firefox. Visit http://localhost:8080/hello. You should see "Hello World!" in your browser.
  • In your SSH client, press Ctrl+C to stop the Bottle server.