Subdomains and Google Apps with Gandi and Linode

Multithreaded JavaScript has been published with O'Reilly!

A few months ago, around the time of all that GoDaddy SOPA stuff, I finished transferring all of my domain names to my new registrar, Gandi. I had been sick of GoDaddy for a long time. When buying a domain with GoDaddy, you're forced to go through a half dozen screens where they try to up-sell you. When logging into your account, you are presented with a bunch of marketing BS. If you want to see your list of domains, you have to dig through a bunch of dropdown menus. Gandi has been a much better experience, their tagline is even “No Bullshit”.

At the time, I kept my hosting on a shared account at Mediatemple (I had used their VPS for my NeoInvoice project but wasn't too impressed). After a couple months of having my sites die whenever a neighbor would use too many resources, I finally opted to transfer all of my domains to be hosted on a VPS at Linode.

I didn't want to lose any emails if my server were to die (which is frequent with my abuse), so I figured the best way to handle this would be to have the emails never hit the server. By using Google Apps for Domains, one can set a bunch of DNS entries (MX Records) to tell servers where to send the emails to.

A note about DNS; this stuff is cached on a bunch of network equipment between you and the server. For the quickest turnaround time, you shouldn't visit your website for several hours before making changes, or for an hour afterwards. Otherwise, you'll get the old DNS and it will take seemingly forever to see the updates (although you can check it on your phone if you goof up ;).

To set these DNS settings, you'll want to login to your Gandi account, click on a domain name, and edit the zone file for the domain (creating a copy of the existing zone file if needed). For this, you'll have to have the nameservers set to point to Gandi. You can't edit the active zone file with Gandi, you need to first copy it, then edit it, then enable it. Also, if you set it to expert mode, you can paste these lines in much quicker.

I use the following settings for this website. The # lines represent my comments (which you'll want to not enter; I don't know if zone files have a comment syntax). You'll want to change the IP Address to the one provided to you by Linode. The MX lines are okay to copy as-is.

# Main Domain
@ 3600 IN A
# Sub Domains
ip 3600 IN A
www 3600 IN A
# Google Apps for Domains

Once all these network devices know what server your hosted on, the web server itself needs to know how to serve up your files. If you're using Apache, you'll want to check out their VHOSTS configuraiton otpions. Personally, I'm a bigger fan of lighttpd since it requires less resources, so I use that. This is a truncated part of my configuration file:

$HTTP["host"] =~ "(^|\.)thomashunter\.name$" {
    server.document-root = "/var/www/tlh"
    server.errorlog = "/var/log/lighttpd/tlh/error.log"
    accesslog.filename = "/var/log/lighttpd/tlh/access.log"
    server.error-handler-404 = "/index.php"
$HTTP["host"] =~ "^ip\.thomashunter\.name$" {
    server.document-root = "/var/www/ip-tlh"

There's a few things to notice in here. The lighttpd configuration file uses a proprietary syntax (although it kinda looks like a cross between PHP and JSON). The Tilde Equal lines are a regular expression which match the requested domain name and apply the rules within that section. The document root folder is where the URL to filesystem mapping begins. This directory should be readable by the user which runs your web server (on a Debian server this defaults to www-data). The error and access logs are where logging information is written, these directories should be writable by the web server user. If you are running a WordPress site, you'll want that 404 line, as it's the easiest way to enable pretty URLs. The second domain doesn't have as many settings, it just serves up a simple script and I don't care as much about it.

If you're running lighttpd and you change the lighttpd.conf file, you'll now need to restart lighttpd:

$ sudo service lighttpd restart
Stopping web server: lighttpd.
Starting web server: lighttpd.

If everything went ok, your server should be serving up your pages from the document root and your emails should be read by your Google Apps account. If your site isn't responding, or is returning content from your previous host, your DNS settings are likely cached. If you're getting the wrong content back from lighttpd, it may be misconfigured (check your regex). If your emails aren't being received, make sure you have things properly setup in your Google Apps account (it has the ability to check your DNS settings and confirm it looks right).

If you're looking for a great VPS host, use Linode. If you're looking for a great Registrar, use Gandi.

Thomas Hunter II Avatar

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript and is an O'Reilly published author.