How to Deploy a sample GO application in CentOS 7

Go Language

Go is a statically typed, compiled programming language Designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency. One of Golang’s biggest advantages is that it offers the clarity and ease-of-use that other languages lack. Golang’s advantages make it easy for new programmers to quickly understand the language and for seasoned veterans to easily read each other’s code.

Table of content

  1. Installation of Go
  2. Creating a sample Go project
  3. Creating Systemd Service
  4. Setting up an Nginx Reverse Proxy Server

Step 1: Installation of Go

To download the Go binary use either wget or curl :

wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz

Next Extract the file using tar.

tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz

Next Adjust the Path Variable.

vi  ~/.bash_profile

Add Follwing line.

export PATH=$PATH:/usr/local/go/bin

Save the file, and load the new PATH environment variable into the current shell session with the following command.

source ~/.bash_profile

Step 2: Creating a sample Go project

Create the workspace directory.

mkdir ~/go

Create a simple “Hello World” Go file.

mkdir -p ~/go/src/hello

And in that directory create a file named hello.go and add following lines in hello.go.

package main

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)
}

We need to add services http, https in the firewall And port number of hello.go 9990/tcp

firewall-cmd --permanent --zone=public  --add-service=http
firewall-cmd --permanent --zone=public  --add-service=https
firewall-cmd --permanent --zone=public  --add-port=9990/tcp

Then reload the firewall following this command

firewall-cmd --reload

Next,

Build the hello.go

cd ~/go/src/hello
go build

Run the executable by typing.

./hello

Output :

Hello World

Step 3: Creating Systemd Service

Create a unit file with the extension . Service within the /etc/systemd/system directory.

vi  /etc/systemd/system/hello.service
[Unit]
Description=sample app
After=multi-user.target

[Service]
User=root
Group=root
ExecStart=/root/go/src/hello/hello

[Install]
WantedBy=multi-user.target

We can now start our script by simply running.

 systemctl start hello.service  
 systemctl enable hello.service

Step 4: Setting up an Nginx Reverse Proxy Server

Install nginx using the command.

yum install nginx -y

Start and enable nginx service

systemctl start nginx
systemctl enable nginx

Check the status of nginx

systemctl status nginx

Create a directory /etc/nginx/sites-available and add file name hello.

vi  /etc/nginx/sites-available/hello
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /root/go/src/hello/hello {
        proxy_pass http://localhost:9990;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

We need to create a symbolic link of our conf file to the sites-enabled directory.

ln -s /etc/nginx/sites-available/hello /etc/nginx/sites-enabled

We can restart the Nginx process to read the our new config.

systemctl restart nginx

Search your ip with that port number : http://your_ip_:9990

Conclusion

We hope this article helped you learn how to deploy a go application on your VPS server. Golang, is very fast and brilliant initiative by Google, is poised to benefit businesses of various industries. Switching over to Golang may be decisive in shaping your software strategy and delivery for the near future. TheStack is one of the best web hosting providers that provide premium shared web hosting, cheap dedicated server hosting, managed VPS server, WordPress hosting and server management. Be assured with our services and reach out to our team of experts to know more.

Related Articles