You are on page 1of 22

How To Deploy a Go Web Application Using Nginx on Ubuntu 18.

04 | DigitalOcean 9/14/23, 3:41 PM

CONTENTS

Prerequisites

Step 1 : Building the Go Web Application

Step 2 : Creating a Systemd Unit File

Step 3 : Setting Up a Reverse Proxy with Nginx

Step 4 : Testing the Application

Conclusion

!" Tutorial !"

How To Deploy a Go Web Application Using Nginx on


Ubuntu 18.04
Published on July 24, 2019

Nginx Go Applications Ubuntu 18.04 Open Source Programming Project

By Michael Okoh

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 1 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

The author selected the Tech Education Fund to receive a donation as part of the Write
for DOnations program.

Introduction

Go is a general-purpose programming language that is gradually becoming one of the


most popular web back-end programming languages. By focusing on simplicity, the
designers of Go created a language that is both easy to learn and faster than many
other languages for web applications, leveraging efficient features like its ability to
handle multiple requests at a time due to its concurrency. Because of this, deploying a
web application in Go will be useful to many back-end developers.

Nginx is one of the most popular web servers in the world due to its lightweight resource
usage and its reliability under load. Many of the largest and most highly trafficked sites
on the internet depend on Nginx to serve their content. In deployment, Nginx is often
used as a load balancer or a reverse proxy to increase security and make the application
more robust. In conjunction with a Go web back-end, Nginx can serve up a powerful and
fast web application.

In this tutorial, you will build a Hello World web application in Go and deploy it on an
Ubuntu 18.04 server using Nginx as a reverse proxy.

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 2 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Prerequisites

To follow this tutorial, you will need the following:

One Ubuntu 18.04 server set up by following this initial server setup for Ubuntu
18.04 tutorial, including a sudo non-root user and a firewall.
The Go programming language installed by following How To Install Go and Set Up
a Local Programming Environment on Ubuntu 18.04.
Nginx installed by following How To Install Nginx on Ubuntu 18.04. Do not follow
Step 5 ' Setting Up Server Blocks; you will create an Nginx server block later on in
this tutorial.
A domain name pointed at your server, as described in How To Set Up a Host Name
with DigitalOcean. This tutorial will use your_domain throughout. This is necessary
to obtain an SSL certificate for your website, so you can securely serve your
application with TLS encryption.

Additionally, in order to achieve a production-grade deployment of your Go web


application, it’s important that you keep your server secure by installing a TLS/SSL
certificate. This step is strongly encouraged. To secure your Go web application, follow
How To Secure Nginx with Let’s Encrypt on Ubuntu 18.04 after Step 3 of this tutorial to
obtain the free TLS/SSL certificate.

Step 1 — Building the Go Web Application

In this step, you will build a sample Go web application that displays Hello World at
your_domain and greets the user at your_domain /greet/ . If you would like to learn more
about the basics of programming in Go, check out our How To Write Your First Program
in Go article.

First, create a new directory in your GOPATH directory to hold the source file. You can
name the folder whatever you like, but this tutorial will use go-web :

$ mkdir $GOPATH/ go-web Copy

Following the file structure suggested in the prerequisite tutorial How To Install Go and
Set Up a Local Programming Environment on Ubuntu 18.04, this will give your directory

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 3 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

the path of ~/go/go-web .

Next, run the following to change directory to your newly created folder in your GOPATH :

$ cd $GOPATH/go-web Copy

Use nano or your preferred text editor to create a file named main.go , which will contain
the source code for your web application:

$ nano main.go Copy

To create the functionality of the Hello World application, add the following Go code
into the newly created main.go file:

~/go/go-web/main.go

package main Copy

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
})

http.HandleFunc("/greet/", func(w http.ResponseWriter, r *http.Request)


name := r.URL.Path[len("/greet/"):]
fmt.Fprintf(w, "Hello %s\n", name)
})

http.ListenAndServe(":9990", nil)
}

Now let’s go through what the preceding code snippet will do, starting from the first line.

First, you wrote the entry point into your application:

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 4 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

~/go/go-web/main.go

package main Copy


...

The package main tells the Go compiler to compile this file as an executable program
instead of as a shared library.

Next, you have the import statements:

~/go/go-web/main.go

... Copy

import (
"fmt"
"net/http"
)
...

This snippet imports the necessary modules required for this code to work, which
include the standard fmt package and the net/http package for your web server.

The next snippet creates your first route in the main function, which is the entry point of
any Go application:

~/go/go-web/main.go

... Copy
func main () {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
})
...
}
...

A parent route / is created within func main , which will return the text Hello World
when requested.

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 5 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

The second route as shown in the following snippet accepts a URL parameter, in this
case a name, to display accompanied by a greeting.

~/go/go-web/main.go

... Copy
func main () {
...
http.HandleFunc("/greet/", func(w http.ResponseWriter, r *http.Request)
name := r.URL.Path[len("/greet/"):]
fmt.Fprintf(w, "Hello %s\n", name)
})
...
}
...

This uses Go’s URL.Path to store the value right after /greet/ and pass it down as the
name from the URL parameter.

Finally, you instantiate your server:

~/go/go-web/main.go

... Copy
func main () {
...
http.ListenAndServe(":9990", nil)
}

The preceding snippet starts the server and exposes your application via port 9990
using Go’s inbuilt http server.

Once you are finished examining the code in main.go , save the file and quit your text
editor.

Next, build the binary executable of your application by running:

$ go build main.go Copy

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 6 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

The preceding command will compile main.go to produce an executable titled main .

You have created your sample Go web application. Next, you will create a systemd unit
file to keep your application running in the background even when you are not accessing
your server.

Step 2 — Creating a Systemd Unit File

In this step, you will create a systemd unit file to keep your application running in the
background even when a user logs out of the server. This will make your application
persistent, bringing you one step closer to a production-grade deployment.

First, create a new file in /lib/systemd/system directory named goweb.service using


nano or you preferred text editor:

$ sudo nano /lib/systemd/system/goweb.service Copy

To set the parameters of the service, add the following snippet into the file.

/lib/systemd/system/goweb.service

[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/ user /go/go-web/main

[Install]
WantedBy=multi-user.target

The ExecStart=/home/ user /go/go-web/main variable specifies that the point of entry for
this service is through the main executable located in the /home/ user /go/go-web
directory, where user is the server non-root sudo account username. Restart=always
ensures that systemd will always try to restart the program if it stops. On the next line,
RestartSec=5s sets a five-second wait time between restart attempts. WantedBy=multi-

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 7 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

user.target specifies in what state your server will enable the service.

Save and exit the file.

Now that you’ve written the service unit file, start your Go web service by running:

$ sudo service goweb start Copy

To confirm if the service is running, use the following command:

$ sudo service goweb status Copy

You’ll receive the following output:

Blog Docs Get Support Contact Sales


Output
● goweb.service - goweb
Loaded: loaded (/lib/systemd/system/goweb.service; disabled; vendor preset: enabl
Active: active (running) since Wed 2019-07-17 23:28:57 UTC; 6s ago
Main PID: 1891 (main)
Tasks: 4 (limit: 1152)
Questions
CGroup: Learning Paths For Businesses Product Docs
/system.slice/goweb.service Social Impact
└─1891 /home/ user /go/go-web/main

To learn more about working with systemd unit file, take a look at Understanding
Systemd Units and Unit Files.

Now that you have your application up and running, you can set up the Nginx reverse
proxy.

Step 3 — Setting Up a Reverse Proxy with Nginx

In this step, you will create an Nginx server block and set up an Nginx reverse proxy to
expose your application to the internet.

First, change your working directory to the Nginx sites-available directory:

$ cd /etc/nginx/sites-available

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 8 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Copy
Create a new file with the name of the domain on which you wish to expose your
application. This tutorial will use your_domain :

$ sudo nano your_domain Copy

Add the following lines into the file to establish the settings for your_domain :

/etc/nginx/sites-available/your_domain

server {
server_name your_domain www.your_domain ;

location / {
proxy_pass http://localhost:9990;
}
}

This Nginx server block uses proxy_pass to serve the Go web application on your
server’s IP address indicated as localhost to make it run on port 9990 . server_name
indicates the domain name mapped to your IP address, in this case your_domain and
www.your_domain .

Next, create a symlink of this Nginx configuration in the sites-enabled folder by running
the following command:

Copy
sudo ln -s /etc/nginx/sites-available/ your_domain /etc/nginx/sites-enabled/ your_domain

A symlink is a shortcut of a file in another location. The newly created shortcut will
always reference the original file to adjust to updates when edits are made to it. Nginx
requires a copy of the configuration in both directories.

Next, reload your Nginx configurations by running the reload command:

$ sudo nginx -s reload Copy

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 9 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

To make sure that your deployment is working, visit http:// your_domain in your
browser. You will be greeted with a Hello World text string.

Note: As mentioned in the Prerequisites section, at this point it is recommended to


enable SSL/TLS on your server. This will make sure that all communication between the
application and its visitors will be encrypted, which is especially important if the
application asks for sensitive information such as a login or password. Follow How To
Secure Nginx with Let’s Encrypt on Ubuntu 18.04 now to obtain a free SSL certificate for
Nginx on Ubuntu 18.04. After obtaining your SSL/TLS certificates, come back and
complete this tutorial.

You have now set up the Nginx reverse proxy to expose your application at your domain
name, and secured your Go web application with SSL/TLS. In the next step, you will be
testing your application over a secure connection.

Step 4 — Testing the Application

In this step, you will test your application over a secure connection to make sure
everything is working.

Open your preferred web browser, visit https:// your_domain :

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 10 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

You will receive a simple Hello World message. Receiving this message when using
https:// in the URL indicates that your application is being served over a secure
connection.

Next, try visiting the second route https:// your_domain /greet/ your-name , replacing
your-name with whichever name you want your app to greet:

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 11 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

The application will return a simple greeting along with your-name , which is based on
the parameter passed to the URL.

Once you have received these results, you have successfully deployed your Go web
application.

Conclusion

In this tutorial, you created a simple web application with Go using its standard libraries,
set up a reverse proxy using Nginx, and used a SSL certificate on your domain to secure
your app. To learn more about Go, check their official documentation. Also, you can look
at our series How To Code in Go to learn more about programming in this efficient
language.

Thanks for learning with the DigitalOcean Community. Check out our offerings
for compute, storage, networking, and managed databases.

Learn more about us !"

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 12 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

About the authors

Michael Okoh Author

Timothy Nolan Editor

Senior Technical Editor

Editor at DigitalOcean, fiction writer and podcaster elsewhere, always searching


for the next good nautical pun!

Still looking for an answer? Ask a question

Search for more help

Was this helpful? Yes No

Comments
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 13 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Comments

4 Comments

Leave a comment!!"

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials,
documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Comment

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 14 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

cnwagu • August 3, 2019

I also had to add rule

allow 9990

to ufw

Show replies Reply

phungnet001 • August 24, 2023

i have a function export excel xlsx working good in localhost, download and
open ok, but when at nginx ubuntu , download is same size file… but when
open it error . what wrong ?

Reply

nviein • October 28, 2022

I’m getting (code=exited, status=2j. What is the reason for it?

Reply

jay179 • April 20, 2020

This comment has been deleted

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 15 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Try DigitalOcean for free


Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Popular Topics

Ubuntu
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0
International License.
Linux Basics

JavaScript

Python

MySQL
Docker

Kubernetes

All tutorials !"

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 16 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Talk to an expert !"

Congratulations on unlocking the whale ambience easter egg! Click the whale button
in the bottom left of your screen to toggle some ambient whale noises while you
read.

Thank you to the Glacier Bay National Park & Preserve and Merrick079 for the sounds
behind this easter egg.

Interested in whales, protecting them, and their connection to helping prevent climate
change? We recommend checking out the Whale and Dolphin Conservation.

Reset easter egg to be discovered again / Permanently dismiss and hide easter egg

Get our biweekly


newsletter
Sign up for Infrastructure as a
Newsletter.

Sign up !"

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 17 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Hollie's Hub for Good


Working on improving health and
education, reducing inequality,
and spurring economic growth?
We’d like to help.

Learn more !"

Become a
contributor
You get paid; we donate to tech
nonprofits.

Learn more !"

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 18 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Featured on Community

Kubernetes Course Learn Python 3 Machine Learning in Python

Getting started with Go Intro to Kubernetes

DigitalOcean Products

Cloudways Virtual Machines Managed Databases Managed Kubernetes

Block Storage Object Storage Marketplace VPC Load Balancers

Welcome to the
developer cloud
DigitalOcean makes it simple to launch in
the cloud and scale up as you grow –
whether you’re running one virtual
machine or ten thousand.

Learn more !"

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 19 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Get started for free


Email address Send My Promo
Enter your email to get $200
in credit for your first 60 New accounts only. By submitting your email you
days with DigitalOcean. agree to our Privacy Policy.

Company Products Community

Tutorials

Q&A

CSSrTricks

Write for DOnations

Currents Research

Hatch Startup Program

deploy by DigitalOcean

Shop Swag

Research Program

Open Source

Code of Conduct

Newsletter Signup

Meetups

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 20 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

About Products Overview

Leadership Droplets

Blog Kubernetes

Careers Paperspace

Customers App Platform

Partners Functions

Channel Partners Cloudways

Referral Program Managed Databases

Affiliate Program Spaces

Press Marketplace

Legal Load Balancers

Privacy Policy Block Storage

Security Tools & Integrations

Investor Relations API

DO Impact Pricing

Nonprofits Documentation

Release Notes

Uptime

Solutions Contact

Support

Sales

Report Abuse

System Status

Share your ideas

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 21 of 22
How To Deploy a Go Web Application Using Nginx on Ubuntu 18.04 | DigitalOcean 9/14/23, 3:41 PM

Website Hosting

VPS Hosting

Web & Mobile Apps

Game Development

Streaming

VPN

SaaS Platforms

Cloud Hosting for Blockchain

Startup Resources

© 2023 DigitalOcean, LLC.

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 22 of 22

You might also like