Golang Builds and Deploys Executable Files

Introduction

I used Golang + Gin before, combined with gorilla/websocket to build a simple chat room backend service. Next, let me introduce how I deploy the service written in go code to the server.

Solution

  1. build

Execute it in the project root directory

Package the Linux executable file under Windows

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go

Packaging Linux executables under Mac

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

Generate main file (without suffix)

  1. Upload to the server

Using an FTP tool such as FileZilla, transfer the main file to any empty directory on the server, such as /software/go-api

Fix Permission denied issue

cd /software/go-api
chmod 777 main
  1. start
cd /software/go-api
#Keep running in the background
nohup ./main >main.out 2>&1 &

After execution, Ctrl+C can exit the interaction

Check whether the startup is successful

ps aux|grep main

Printing out information like this means that the startup is successful

root 15448 0.0 0.0 241388 4280 pts/0 T 11:21 0:00 sudo nohup ./main
opensou+ 15479 0.0 0.0 112812 980 pts/0 S+ 11:21 0:00 grep --color=auto main

The second parameter 15448 indicates the process pid. If the program is updated, after uploading the new main file, the old process needs to be stopped. Use

kill -9 15448

Then restart the new process with nohup ./main >main.out 2>&1 &

  1. Modify the front-end ws address
// 100.2.3.4 is changed to your own server IP address
var ws = new WebSocket("ws://100.2.3.4:8448/ws");

If the websocket cannot be connected, first check whether the IP can be connected

ping 100.2.3.4

Then check whether the server security group has opened port 8448.

  1. nginx configuration (optional)

If you are only doing local testing, the IP address can be used. If you need to deploy and use it in production, you usually use the original nginx for forwarding.

Edit your own nginx configuration file to forward myapi.mydomain.com/ws requests to the internal 100.1.2.3:8448 address

First find your nginx configuration file, edit it with vim

cd /software/nginx/conf.d
vim myapi.mydomain.com.conf

Basic use of vim, i enters editing, Esc exits editing, Shift + : enters command mode In command mode, save and exit: wq, force exit: q!

Add your own configuration

upstream go_ws {
       #ip_hash;
       server 100.1.2.3:8448;
}

server {
     #listening IP port
     listen 80;
     #domain name
     server_name myapi.mydomain.com;
     #other configuration

     location /ws {
         proxy_pass http://go_ws/ws;

         proxy_set_header Host $host;
         proxy_set_header X-real-ip $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 1800s;
         proxy_read_timeout 600s;
         proxy_send_timeout 600s;
         #websockets
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
     }
}

Restart nginx, such as the docker I use

docker restart my-nginx

At this time, the front end modifies the address to test

// change myapi.mydomain.com to your own server IP address
var ws = new WebSocket("ws://myapi.mydomain.com/ws");

Conclusion

The above is my experience in packaging and publishing go back-end executable files. The tutorial is relatively basic. If there are any deficiencies, please point out.

Reference

Comments