Deploying a Go application to your server

November 19, 2018

Alright, time to deploy our application to some host. I often use DigitalOcean, but feel free to use whatever provider you like. As long as you’ve got SSH access, you should be good.

If you feel like trying DigitalOcean, you can use my referral link to get a free $10 credit.

Let’s assume we’ve wrote this wicked application, and we can’t wait to open the doors to this to the public:

package main

import (
	"net/http"
)

func main() {
	http.HandleFunc("/", helloWorld)
	http.ListenAndServe(":8080", nil)
}

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

Pretty sweet, right? I thought so.

Alright, these are the steps that follow now:

  1. Build the application
  2. Copy it over to your desired server
  3. Run the application
  4. Start serving some hello’s to the world.

Let’s get started!

Build the application

Although you might be working on a Mac, or Windows, or something entirely different, we’re deploying our application to a 64-bit Linux server. (This might not be the case, but for sake of simplicity I am just assuming you are.)

In order to build the application for this specific OS and architecture, open up your terminal and navigate to the folder where the entrypoint (main.go) of your application resides.

For example:

$ cd github.com/machiel/hello-world

Perfect, now let’s build the application:

$ GOOS=linux GOARCH=amd64 go build

Right now there should be a binary in your working directory named after your working directory, so in my case this binary is called hello-world. That’s your application!

Moving your application to your server

Alright, onto the next step. Moving the application to your server. Okay, time to access that server. Get your password or SSH key ready, because we’re going to copy over that beautiful binary we just built.

$ scp my-beautiful-binary myuser@10.0.0.1:~

So replace my-beautiful-binary by, you guessed it, your binary name. Replace myuser by the name of the user you identify as on the remote machine. Oh, and then there’s the actual IP address of your server, (could be a domain name too), that one is important too. Replace 10.0.0.1 by your own remote address.

Perfect, you’ve now, if all went right, copied over your beautiful binary to the remote machine. Well done.

Launching the application

Show time. It’s time to launch this application. Get out that champagne and a couple (one?) of glasses. Perfect, once that’s ready, let’s get to work.

SSH into your remote machine:

$ ssh myuser@10.0.0.1

These values have to be replaced once again, I hope you get the gist by now. Perfect you’re in the machine.

Now, start the application:

$ ./my-beautiful-binary

Alright, that looks all mighty fine, now navigate to your application. In my case this would be http://10.0.0.1:8080, depending on the port your application is listening on.

Oh yeah, pop that champagne, rent a big mansion with a swimming pool. It’s time to throw a launch party!

If your application didn’t start or wasn’t accessible, read on.

Wait, but errr.. what?

Yes, yes, I know. You just shut down your SSH session and now the application is down too… That’s a pity. If you just don’t tell your angel investors, you can still have that awesome mansion launch party!

No, in all seriousness. This is definitely not a perfect way of deploying. We need to step up our game.

An article on keeping your application running is in the works, but in the meantime if you’re curious, you could try using supervisor or systemd. Or something else, there’s quite some tools that can help you with this.

Troubleshooting

So since you’ll be reading this article after I wrote it, and thus the problems that may occur will occur in the future, and I am not a psychic, it will be hard for me to help you debug your issues, but I am going to make an attempt nonetheless!

My application didn’t start at all

If your application didn’t start at all, you might have had a couple of errors, which might be able to help you debug your issue.

One reason I could think of: you’re using a port number equal or lower than 1024, and you’re trying to start it as a non-root user. These ports are restricted, so this might give you some issues.

My application did start, but wasn’t accessible

If you were able to access the machine by SSH it seems like it’s properly connected to the internet. Perhaps a firewall is blocking the port you’re trying to use?


Get Machiel Mail in your inbox!

Of course I have a newsletter, all the cool kids have them! Sign up if you want to get notified of the occasional blog post I write. They will be mostly about Go, or simply about projects I am working on. And it might just turn out that they are interesting!

And if you're wondering, will he send sp... No, I will never send you any spam!

comments powered by Disqus