rafamds
By:
rafamds

How do I deploy a golang app with Apache installed on Ubuntu 16.04?

October 27, 2016 227 views
Apache Go Applications Ubuntu 16.04

Hi everyone,
I am learning Go at the moment and I have built really simple webapps following some tutorials with the net/http package. I have created a simple wishlist, where I add an item and than it does to a simple table of things I want, pretty simple.

Now I want to deploy this app to my Digital Ocean droplet, but I just don't know how. I have some php websites with different domains already with Apache behind it.

I am really a begginer on this "servers configuration" thing, usually with php is pretty easy on webhosts and I didn't need this much experience. Can you point me on the right direction to make my Go app available at a domain I own, without the ports bit? Preferably with Apache.

Thanks :)

2 Answers
xMudrii October 27, 2016
Accepted Answer

Hello.

Just like you would install Go on local machine, you need to do on server.

For help about it you can refer to How to Install Go 1.6 on Ubuntu 16.04.

We need now to install Golang app.
In case you use GitHub for hosting, you can use go get to pull it:

  • go get github.com/your-username/repo

If not, you need to copy from your local machine $GOPATH/src/... to servers one. In tutorial you will set $GOPATH to ~/work so you need to unpack it in ~/work/src/....
Then go in that directory and run:

  • go install

When you run app it should be working.
You can access it via http://DropletIP:port.

To make it work on port 80, you need to make Reverse proxy with Apache or Nginx.

If you already have Apache on Droplet, go with it. But if you setup new Droplet for it, I'll recommend Nginx as it's easier to setup for Reverse proxy and less memory intensive.

Tutorial how to do it on Nginx.
About Apache you'll have to refer to Apache docs.

If you have any questions, feel free to ask, we will try to answer them :D

Go is a modern programming language developed at Google. It is increasingly popular for many applications and at many companies, and offers a robust set of libraries. This tutorial will walk you through downloading and installing Go 1.6, as well as building a simple Hello World application.
  • Hi @xMudrii,
    thanks for your reply. I made it to http://DropletIp:port and its working, but if I close the terminal the application shuts down. How could I make it run on background?

    • You can try setting up systemd service.
      Try something like this. We will first create service file:

      • sudo nano /etc/systemd/system/yourapp.service

      With following content:

      sudo nano /etc/systemd/system/yourapp.service
      [Unit]
      Description=App description 
      After=network.target
      
      [Service]
      User=root
      ExecStart=/opt/yourapp/app
      
      [Install]
      WantedBy=multi-user.target
      

      Save that. I would create folder yourapp under /opt and in it store app. Red highlighted is location where app is located (app is executable file name).
      You can find executable under $GOPATH/bin.

      Now when you:

      • sudo systemctl start yourapp

      And try to access it should work.
      To enable it on boot use:

      • sudo systemctl enable yourapp

      Please note, I'm not on my PC so I didn't test it but it should works. If you get problem I will try to correct it. :)

      • It's not working, should I change more than ExecStart on that file? I've only changed that.

        Thanks again

        EDIT:
        ~~Should I paste the content in src anywhere?~~

        EDIT2:
        I've added:

        WorkingDirectory=/root/work/src/github.com/myvendor/myapp
        

        To the service file and it is working now.

        Thanks for everything

Have another answer? Share your knowledge.