How To: Run Google Go Web Apps Behind Apache

Sunday 29 January 2012, 17:02

In my last blog post I showed how to make a website using Google Go. However if your VPS or dedicated server has not got any spare IP addresses then you are stuck. Most servers come with only one or two IP addresses and binding your Google Go website to port 80 of a particular IP address will mean that Apache cannot do so. In this blog post I am going to explain how you can run a Google Go website behind an Apache webserver using mod_proxy.

We are going to use mod_proxy to proxy requests via the Apache webserver to our Google Go web app which will be listening on a different port. This method adds a little bit of overhead but is a simple way to run multiple websites from a single IP address. Here is the web app that we're going to attempt to run behind an Apache webserver.

file: http.go

package main

import(
    "http"
    "log"
    "os"
)

const resp = `<html>
        <head>
        <title>Simple Web App</title>
      </head>
      <body>
      <h1>Simple Web App</h1>
      <p>Hello World!</p>
      </body>
      </html>`

func handler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(resp))
}

func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":8080", nil)

    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
}

Notice that this web app listens on port 8080, you can use any free port instead. This example is very basic but the technique will work for any web app. If you compile and run this code and browse to http://localhost:8080/ in your web browser you will see your web app.

Next enable mod_proxy, mod_proxy_http and mod_rewrite for your apache installation. On Ubuntu you do this with the following commands.

$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo a2enmod rewrite
$ sudo service apache2 restart

Once all the modules have been enabled we can setup a new virtual host for the Google Go web app. We are going to use instruct Apache to send all requests to localhost on port 8080.

file: gowebsite

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

        RewriteEngine on
        RewriteRule ^(.*)$ http://localhost:8080/$1 [P,L]
    </Directory>
</VirtualHost>

On Ubuntu virtual host files are put in the /etc/apache2/sites-available directory and enabled by placing a link these files from in the /etc/apache2/sites-enabled directory. This can either be done manually or by using an apache commandline utility.

$ sudo a2ensite gowebsite
$ sudo service apache2 restart

You can now visit www.example.com and see your website. Provided ofcourse that the Google Go application is running. See my previous blog post for tips on how to make a daemon to allow a Google Go website to run in the background.