Deploy Golang Application on Digital Ocean Server Ubuntu 16.04

Golang and sample http rest service

3 min readFeb 22, 2019


I like to use Golang to write application in server for handle service, such as Api, Realtime, and background processing…, If you would like to learn Golang i recommend visit official page

In this tutorial i just created a sample pie of code to to handle HTTP request , create a file main.go


if we run command in development mode

go run main.go

output: Server is running: 3001

type in browser http://localhost:3001/api we should see “Hello!”

in real project you have alot of files .go depend on how is your complex of your project. In this article i would like to show you how to build your Golang project into one file and deploy it on your linux VPS , I use and like Digital Ocean.

Step 1: Create a deploy file

i usually create a bash file , this is contain custom commands so we can run it one to save time

open your command line, create file and change permission execute able.

chmod u+x

now edit with following content

To build project just in your commandline in same directory with and run this command


It will complied your project into one file for example myapp

that is name of your project file we will copy this file over Digital Ocean VPS.

Step 2: Setup Digital Ocean VPS

Many of hosting providers but for me i do like Digital Ocean(DO) so much because they are flexible in price and UI control, if you don’t have an account let create new one

After creating an account on Digital Ocean you should have access as root , open terminal and login to your VPS with

ssh root@123456

123456 is your Server IP address

Step 3: Setup Nginx

We do use nginx for proxy handler. For example out Golang application is running in server port is 3001, but in Nginx we can setup and handle http request on port 80 or 443 if you have SSL setup.

sudo apt-get update
sudo apt-get install nginx
sudo ufw allow 'Nginx HTTP'

Step 4: Setup HTTPS use Let’s Encrypt

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx
sudo certbot --nginx

Create Cert file for your domain

sudo certbot --nginx certonly

Enter your domain example :

then enable auto renew

sudo certbot renew --dry-run

Step 5: Move your application file to server

change to your project folder, remember we did build project to a file myapp

let copy it to over server, we can copy it to any folder in your server, but i do recommend store that file in /var/www

use ssh to copy that file

scp ./myapp root@123456:/var/www/

123456 is your Digital Ocean VPS IP address you can copy ip address in Digital Ocean dashboard.

Step 6: Create a service to run Golang application

We need create a sytem service to run our applion

let create new file /etc/systemd/system/myapp.service with following content remember change ExecStart=/var/www/myapp and Environment=GOPATH=/var/www/ to correct and match your application root folder and appcation file name,

in this demo i store myapp file in /var/www

Description= instance to serve api

in this service file i had require mysql if you don’t use MYSQL server you can remove it.

Now to start service (Start and running our Golang App) by run this command

sudo systemctl start myapp

To stop:

sudo systemctl stop myapp

Step 7: Create Nginx Configure file to handle proxy for our application

you need change your to your domain name

create a file /etc/nginx/sites-enabled/

server {
listen 80;
return 301 https://$server_name$request_uri;
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/;
ssl_certificate_key /etc/letsencrypt/live/;
root /var/www/html;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
location /ws {proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Remember change your domain, in this tutorial i had included websocket config as well you can see location /ws

Restart NGINX server

systemctl restart nginx

It’s done. Thank you for reading.




i’m from a nice city in Vietnam called Danang, where live and did my studies. I love programming, coffee and spending my free time teaching myself new skills.