SoFunction
Updated on 2025-03-05

Steps to deploy the gin project to the server and start the background

Preface

What should we do if the gin project we wrote wants to deploy on the server? Next, I will explain the deployment tutorial in detail.

1. First of all, we need to have a virtual machine and install the go framework on the virtual machine.
2. Upload the written project to the virtual machine.
3. Download the various dependencies required for the project to run.
4. Configure the startup project
5. Backend configuration startup project

1. Install Go environment

1.1 Unzip the standard installation package

Go provides compiled packages that can be used directly by decompressing them.

wget /dl/go1.18. -P /usr/local/src

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.

1.2 Configuring environment variables

Edit file "/etc/profile"

vim etc/profile
# Enter the following information and saveexport GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/go #(Optional settings)

Make environmental variables take effect in a timely manner

source /etc/profile

Variable explanation:
GOROOT: Similar to JAVA_HOME, the directory where Go's executable file is located
GOPATH: Starting from go 1.8, the GOPATH environment variable now has a default value if it is not set. It defaults to $HOME/go on Unix,
The $GOPATH directory convention has three subdirectories:

src stores source code (for example: .go .c .h .s, etc.)
The file generated after pkg is compiled (for example: .a)
bin compiled executable file
Starting from Go1.11, Go officially added Go Module support, and Go1.12 has become the default support; from then on, the source code must be placed in Gopath.

1.3 Set up a warehouse agent

Since Google is blocked, you need to set up a proxy

go env -w GOPROXY=,direct

You can also write GOPROXY to the environment variable configuration file "/etc/profile"

1.4 Check whether to install go

go version

2. Upload the project

You can upload the project to the server through WinScpJ, or upload the project using xftp
I won't talk about uploading files here

3. Download and run dependencies

Enter the project home folder and download various operation dependencies.
If the project uses the database or redis, it needs to be installed and configured on the server before it can be used.

go mod tidy

4. Configure the startup project

Start the project directly

go run 

5. Backend configuration startup project

Backend project

nohup go run  &

5. Summary

This is the article about the detailed operation guide for deploying gin projects to the server and launching the background. For more information about deploying gin projects to the server, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!