Go Web Application featured image

Deploying a Go Web Application using Nginx on Ubuntu 22.04

Go is an open-source general-purpose programming language. It was inspired by the productivity of Python while offering the ability of C. It was originally developed by Google to tackle issues they were facing with their codebase (code complexity and long compilation time). Like any other modern-day programming language, Go is suitable for all sorts of purposes. For example, it could be utilized for web development, command-line scripting, networking server apps, front-end development, etc.

In this guide, we will walk you through the steps of deploying a simple Go web application on Ubuntu 22.04 LTS. We will also implement an Nginx reverse proxy to handle the traffic load.


To perform the steps demonstrated in this guide, you will need the following components pre-configured:

If the service is to be accessible by a domain name, then you must have an FQDN pointed to the server. In that case, it’s recommended to secure Nginx with SSL. You can learn more about securing Nginx with Let’s Encrypt on Ubuntu from our tutorial.

Step 1 – Creating a Go Web App

We will build a simple Go web app that will print “Hello World” on the screen when accessing the domain. Furthermore, the app will greet the user when accessing <domain>/greet/<username>.

Start by creating a new project under $GOPATH. Create the project directory:

Go Web Application Make Dir

Change the current directory:

Go Web Application Change Dir

Then, initiate a project:

Go Web Application Init Project

Next, create main.go that will serve as the root of our Go application. It will host the source code for our web app:

Open the project in VS Code:

Paste the following code in the file main.go:

Go Web Application Main Go

Let’s have a quick breakdown of the Go program we just created:

  • The term package main serves as the entry point to our application. It also instructs the Go compiler to compile the file as an executable instead of a shared library.
  • The import statement imports all the additional modules required for the program to work. Here, we’ve imported the fmt (for printing texts) and net/http (for web server applications) packages.
  • Using the function http.HandleFunc, we established two routes:
    • The first route / serves as the parent route within func main.
    • The second route /greet/ accepts a URL parameter (a string, in this case). The string is then displayed accompanied by the greeting message.
      • URL Path is used for storing the value after /greet/. The value is then passed down as the name of the URL parameter.
    • The http.ListenAndServe function initiates the webserver. Here, it’s listening to port 9990.

Now that our program is ready, compile the file into an executable:

The Go compiler will create an executable with the same name as the file. Check out the output:

Go Web Application ls -lh

Step 2 – Configuring a systemd Unit File for Background Execution

Until now, if the current user logs out of the system, the Go web app will stop running. That’s a bad design for a web server. To solve this issue, we will create a systemd unit file to keep it running in the background, even when the user logs out of the server. It brings the configuration one step closer to being a production-grade deployment.

Create a dedicated systemd file goweb.service under /lib/systemd/system:

Add the following snippet in the unit file:

go web service


  • The variable ExecStart indicates the point of entry for the service through the main executable located in our project directory ( $GOPATH/go-web). Note that instead of mentioning $GOPATH, we typed the full path to the executable file. It’s because the $GOPATH variable is only visible to the user.
  • The variable Restart tells systemd what to do in the event the program stops running. The value always tells systemd to restart the program every single time it stops executing.
  • The variable RestartSec tells systemd to wait a specific amount of time before attempting to restart the program. Here, the value is set to 5s (5 seconds).
  • The statement WantedBy=multi-user.target specifies in what state systemd will enable the service.

Save the file and close the editor. Start the service:

go web start

Verify if the service is up and running:

go web status

As the output suggests, the application is up and running. We are now ready to use Nginx as the reverse proxy.

Step 3 – Configuring Nginx as a Reverse Proxy

Now, we will create a server block for Nginx to act as a reverse proxy. The web app will be exposed to the internet through the reverse proxy (for additional performance and security).

Change the current active directory to sites-available:


Now, create a file with the domain name you wish to expose your application. For demonstration, we will be using example.com:


Enter the following code in the block file:


Here, we are using proxy_pass to serve the Go web app on the server’s IP address (localhost) on port 9990.

To enable the Nginx block, create a symlink (also known as a soft link) of the file to sites-enabled folder:

create sym

Reload the Nginx configuration to take the changes into effect:

reload nginx

Step 4 – Verification

If everything went as expected, then the web application should now be accessible on localhost:9990 and the domain we configured. Open the URL in a web browser:

Hello World

As expected, the application is printing “Hello World” on the screen. This is the job of the primary route. Next, test the second route by visiting the following URL:

Hello Cloud

The application is successfully returning the simple greeting message with the name from the parameter received from the URL.

Voila! We have successfully deployed a Go web application on our local server!

Final Thoughts

In this guide, we demonstrated deploying a simple Go web application with the help of Go standard libraries. We also deployed a reverse proxy using Nginx. While the demonstration was performed on a local server, for a public server, it’s strongly recommended to implement an SSL certificate for secure exposure to the internet. For more Go programming, you can check out our tutorial on Building and Installing Go Programs.

Happy Computing!