jonwear.com

jon wear's personal website

Your own Go blog

19 September 2015

This post is based on a talk I gave at the Philly Go Meetup on September 8th .

Now that I have my blog running using the same blog engine that http://blog.golang.org uses, I thought I'd write up a post to explain how I did it.

Get a server

You gotta host it somewhere. I used www.scaleway.com. I got a Linux server with enough power to run my lowly blog:

  • 4 Cores @1.3GHz
  • 2GB memory
  • 50 GB SSD
  • 1 public IP
  • Costs €2.99/month which is currently $3.34

Turn it on and ssh in. Be sure to run:

apt-get update
apt-get upgrade

Now you have an up to date server.

Install Go

Scaleway.com servers use ARM processors so I found it easiest to just install Go on the server and do my builds there. You could install Go 1.5 locally, target your build and copy it up to your server however you wish. But that's not how I did it.

apt-get install golang

As of now that installs version 1.3.3:

go version go1.3.3 linux/arm

Be sure to set your GOPATH

Get the code

Download the code from here:

I recommend that you make your own git repo with the release code. Be sure to keep the BSD license in place.

From inside the directory run:

go get

Build the binary

go build blog.go local.go rewrite.go

Now you could launch the freshly built binary, but you don't have a way to get to it from the outside yet.

Install nginx

apt-get install nginx
service nginx start

Make sure you see the nginx startup page by navigating to the public IP address of your server.

Configure nginx

Our blog binary is going to be listening on port 8080. We want to serve requests/responses on port 80. We need to tell nginx to proxy between 80 and 8080.

Put this inside the html { } section of nginx.conf file.

server {
 listen         80;

 server_name    IP_Address_or_URL;

 access_log   /var/log/nginx/blog/access.log ;
 error_log    /var/log/nginx/blog/error.log info ;

 location / {
    proxy_pass  http://127.0.0.1:8080;
    }
 }

Daemonize our blog binary

Simple right? We're almost there. We could start our go binary right now by just invoking it from the command line, but if it crashes for some reason we want it to start back up. If we reboot our server, we want the blog server to start up again without us having to do it. So let's daemonize.

If you've never seen a System V script before, this may make you a little glassy eyed, but don't worry, it's mostly boiler plate. There's only one thing to change, the dir= part. Put in the path to the directory that contains the blog binary there. Keep the enclosing double quotes.

#!/bin/sh
### BEGIN INIT INFO
# Provides: blog
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

dir="PATH_TO_DIRECTORY_THAT_CONTAINS_BINARY"
cmd="./blog"
user="root"

name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"

get_pid() {
cat "$pid_file"
}

is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
start)
if is_running; then
    echo "Already started"
else
    echo "Starting $name"
    cd "$dir"
    if [ -z "$user" ]; then
        sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
    else
        sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
    fi
    echo $! > "$pid_file"
    if ! is_running; then
        echo "Unable to start, see $stdout_log and $stderr_log"
        exit 1
    fi
fi
;;
stop)
if is_running; then
    echo -n "Stopping $name.."
    kill `get_pid`
    for i in {1..10}
    do
        if ! is_running; then
            break
        fi

        echo -n "."
        sleep 1
    done
    echo

    if is_running; then
        echo "Not stopped; may still be shutting down or shutdown may have failed"
        exit 1
    else
        echo "Stopped"
        if [ -f "$pid_file" ]; then
            rm "$pid_file"
        fi
    fi
else
    echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
    echo "Unable to stop, will not attempt to start"
    exit 1
fi
$0 start
;;
status)
if is_running; then
    echo "Running"
else
    echo "Stopped"
    exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0

Start the service!

We're almost there!

Now, at the terminal prompt on your server type:

service blog start

Your blog server should be running. Check your IP address http://YOUR_IP and you should see the sample content. Set your domain via DNS to your IP and you're all done.

Comment on my subreddit.

By Jon Wear

Related articles