Go/Golang Share Instance

When you have more than one Go/Golang application and would like to share the same Amazon EC2 instance but unable to have both listening at port 80. It can be done by having Apache proxying it to them.

I’m going to assume you have Apache and Go/Golang installed. If not you can read it here

Ubuntu Install Apache
Ubuntu Install Go/Golang

Let’s say “one.go” is an application running with a subdomain “one.oofnivek.com” and “two.go” is another application running at “two.oofnivek.com”. Both of these subdomains are sharing the same Amazon EC2 instance.

It will be easier to serve web pages in Go/Golang using gorilla/mux. To install it run the command below.

go get -u github.com/gorilla/mux

Create a file named “one.go” and paste the contents below into it.

package mainimport (
"net/http"
"log"
"github.com/gorilla/mux"
)
func YourHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("One!\n"))
}
func main() {
r := mux.NewRouter()
// Routes consist of a path and a handler function.
r.HandleFunc("/", YourHandler)
// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8001", r))
}

Now create another file named “two.go” and copy the contents below into the file.

package mainimport (
"net/http"
"log"
"github.com/gorilla/mux"
)
func YourHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Two!\n"))
}
func main() {
r := mux.NewRouter()
// Routes consist of a path and a handler function.
r.HandleFunc("/", YourHandler)
// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8002", r))
}

Now run it with the commands below. I prefer to run it in tmux instead of running it in background so that I can reattach the session and end it with Ctrl+C.

tmux new -s one -d 'go run one.go'
tmux new -s two -d 'go run two.go'

Now test it locally with curl.

Image for post
Image for post

Next we create the config file for the subdomains.

sudo vim /etc/apache2/sites-available/one.oofnivek.com.conf

Copy the contents below into the config file.

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8001/
ProxyPassReverse / http://localhost:8001/
ServerName one.oofnivek.com
</VirtualHost>

Now create another config file “two.oofnivek.com.conf”.

sudo vim /etc/apache2/sites-available/two.oofnivek.com.conf

Copy the contents below into the config file.

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8002/
ProxyPassReverse / http://localhost:8002/
ServerName two.oofnivek.com
</VirtualHost>

Now enable Apache proxy mod and new sites by running the commands below.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2ensite one.oofnivek.com
sudo a2ensite two.oofnivek.com
sudo systemctl restart apache2

Lastly configure your subdomain to point to your instance IP.

Image for post
Image for post

Assuming you already open port 80 in your security group. You should see a screen similar to this below when you browse directly to the IP.

Image for post
Image for post

Now let’s test out the subdomains.

Image for post
Image for post
Image for post
Image for post

< Back to all the stories I had written

Written by

A software engineer, a rock climbing, diving & rollerblading enthusiast, a husband, a father.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store