Dave Heinemann

Deploying and Running a Go Web Server in NearlyFreeSpeech.NET

Lately I've been dabbling a little more in Go. This time, I'm creating a simple web server to run this blog.

This blog is mostly pure HTML, but with a handful of PHP scripts to handle things like generating the list of posts, Atom feed, etc. PHP does the job, but I never found it very ergonomic, so I'll be using Go to replace those features.

But first, I needed to be sure I could run a Go web server on my hosting provider, NearlyFreeSpeech.NET (NFSN). This blog post is a quick guide on how to deploy and run a very simple Go web server in NearlyFreeSpeech.NET.

NearlyFreeSpeech.NET

NearlyFreeSpeech.NET (NFSN) is a DIY web hosting solution. For a very low price1, you are provided with a FreeBSD shell and a large collection of pre-installed programming languages and software available for your use. Most people will probably gravitate toward Apache, MySQL, and PHP, but you can deploy and run just about any type of web server you want.

We'll be deploying our own Go web server.

The Program

For simplicity, our web server will be a very basic Hello World program:

package main

import (
    "net/http"
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, world!"))
}

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", helloWorld)

    _ = http.ListenAndServe(":8080", mux)
}

When any URL on the web server is requested, it will simply respond with "Hello, world!".

Compiling

Your NFSN service runs FreeBSD on the AMD64 architecture. Go supports cross-compilation, so you don't need to have a virtual machine handy—you just need to tell the compiler which operating system and architecture you're targeting:

$ GOOS=freebsd GOARCH=amd64 go build

NFSN Site Configuration

First, create a new NFSN site if you don't already have one. A new site is recommended for test purposes. Set the site's Server Type to Custom.

In the Site Information page for your site, refer to the SSH/SFTP Information, and use it to deploy your Go program to /home/protected/.

Next, you need to tell your site how to run your web server:

  1. Open the Site Information page for your site.
  2. Under the Daemons list, click Add a Daemon.
  3. Set the Tag to something memorable.
  4. Set Command Line to the path to your program. For example, /home/protected/helloworld.
  5. Click Add Daemon to save the changes.
  6. Under the Daemons list, click Start to start your web server.

Now your web server should be running, but it won't be accessible yet. NFSN only allows specific ports to be exposed to the Internet. Our example program above uses port 8080, and your site needs to be told to forward ports 80 and 443 to it:

  1. Open the Site Information page for your site.
  2. Under the Proxies list, click Add a Proxy.
  3. Set the Protocol to HTTP.
  4. Set the Target Port to the port that your web server listens on (8080 in our example).
  5. Click Save Changes.

Now double-check that your web server daemon is running, then try viewing your website. You should see "Hello, world!".

Do you have any thoughts or feedback? Let me know via email!
  1. Hosting this blog costs me less than $10 USD per year.↩

#Golang #Programming