TurboGears - Deployment


To switch over from a development environment to a full-fledged production environment, application needs to be deployed on a real web server. Depending upon what you have, there are different options available to deploy a TurboGears web application.

Apache with mod_wsgi

The mod_wsgi is an Apache module developed by Graham Dumpleton. It allows WSGI programs to be served using the Apache web server.

Firstly, install Apache 2.X for your platform, if not done already. Once you have Apache installed, install mod_wsgi. Create and activate Python virtual environment on the server and install TurboGears in it.

Install your application within the application director, then create a script named app.wsgi.

Configure Apache installation as follows −

<VirtualHost *:80>
   ServerName www.site1.com
   WSGIProcessGroup www.site1.com
   WSGIDaemonProcess www.site1.com user = <username> 
      group = www-data threads = 4 python-path = <pythonpath>
   WSGIScriptAlias myapp/app.wsgi
   #Serve static files directly without TurboGears
   Alias /images
   Alias /css
   Alias /js

Restart Apache

Type http://www.site1.com/ on a browser to access the application.

TurboGears under Circus and Chaussette

Circus is a process & socket manager. It can be used to monitor and control processes and sockets. When paired with the Chaussette WSGI server, it can become a powerful tool to deploy your application and manage any related process your applications need.

TurboGears - GoogleAppEngine

Install the Google AppEngine SDK for Python from the following URL − https://cloud.google.coms

Install the Google AppEngine on your system. Then open Google Developer console and sign in with your Google Account − https://console.developers.google.com/start

Create a new project called mytgapp

Mytgapp Project

Using Google AppEngine Launcher, create a new application named mytgapp.

New Application

Google App Engine Launcher

The following files will be created in the specified directory −

  • app.yaml
  • favicon.ico
  • index.yaml
  • main.py

By default,the created application relies on the Webapp2 framework. To remove this dependency, edit the app.yaml file and delete the following part −

   - name: webapp2
   version: "2.5.2"

Create a temporary virtual environment in a directory named mytgapp and install TurboGears. Create a TurboGears application in it. Now we can proceed editing the main.py file which is started by AppEngine to run our application and actually write a TurboGears application there.

Add the following contents in main.py

import os
import site
site.addsitedir(os.path.join(os.path.dirname(__file__), 'packages'))
from tg import expose, TGController, AppConfig

class RootController(TGController):
   def index(self):
      return "<h1>Hello World</h1>"
config = AppConfig(minimal = True, root_controller = RootController())
app = config.make_wsgi_app()

Now run the application from AppEngine Launcher and click on browse button to see that application works properly on the localhost.

We have already created a project named mytgapp in the developer console. Now click on the deploy button in the Launcher. After the deployment process is over, http://mytgapp.appspot.com/ visit to view our application online.

mytgapp appspot