Professional Documents
Culture Documents
CONTENTS
Prerequisites
Conclusion
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
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
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.
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 :
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
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:
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
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
})
http.ListenAndServe(":9990", nil)
}
Now let’s go through what the preceding code snippet will do, starting from the first line.
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
The package main tells the Go compiler to compile this file as an executable program
instead of as a shared library.
~/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.
~/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.
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.
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.
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.
Now that you’ve written the service unit file, start your Go web service by running:
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.
In this step, you will create an Nginx server block and set up an Nginx reverse proxy to
expose your application to the internet.
$ 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 :
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.
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.
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.
In this step, you will test your application over a secure connection to make sure
everything is working.
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.
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
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!!"
You can type !ref in this text area to quickly search our full set of tutorials,
documentation & marketplace offerings and insert the link!
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
allow 9990
to ufw
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
Reply
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
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
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
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
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
Become a
contributor
You get paid; we donate to tech
nonprofits.
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
DigitalOcean Products
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.
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
Tutorials
Q&A
CSSrTricks
Currents Research
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
Leadership Droplets
Blog Kubernetes
Careers Paperspace
Partners Functions
Press Marketplace
DO Impact Pricing
Nonprofits Documentation
Release Notes
Uptime
Solutions Contact
Support
Sales
Report Abuse
System Status
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
Game Development
Streaming
VPN
SaaS Platforms
Startup Resources
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04 Page 22 of 22