Go project deployment and problems encountered

Small chat : This time Xiaobai will bring you Golang project deployment operations, personal problems and solutions to them. It is still writing the manuscript while performing the demonstration. If you encounter similar problems but have doubts, you can leave a message. The development environment is Windows, and the deployment environment is Linux. The development tool is GoLand, and the deployment server is Alibaba Cloud.


1. Package deployment

1.1. Packaging in the development environment

  • In the same-level directory of the project main.go, execute the following command to locally set the packaging environment
# 设置环境
set CGO_ENABLED=0
set GOOS=linux
set GOARCH=amd64
  • Execute the following command to generate the main-app package file
# 打包命令  main-linux 为打包文件名
go build -o main-app main.go

insert image description here

  • Connect to the server and put the main-app file in any directory of the deployment server
[root@iZwz9d9v06uh0jnrexcuk9Z sunnybook]# ll
-rw-r--r-- 1 root root 16535089 Nov 10 21:05 main-app

1.2. Run the command at server startup

  • Add executable permissions to project binaries
chmod +x main-app
// 或
chmod 773 main-app
[root@iZwz9d9v06uh0jnrexcuk9Z app]# chmod 773 main-app
[root@iZwz9d9v06uh0jnrexcuk9Z app]# ll
total 16148
-rwxrwx-wx 1 root root 16535089 Nov 10 20:07 main-app
  • Start execution (binary file, can be run directly)
./main-app
[root@iZwz9d9v06uh0jnrexcuk9Z app]# ./main-app 
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

Ctrl + C to stop running

  • background start
nohup ./main-app &    
回车
[root@iZwz9d9v06uh0jnrexcuk9Z app]# nohup ./main-app &
[1] 19400
[root@iZwz9d9v06uh0jnrexcuk9Z app]# nohup: ignoring input and appending output to ‘nohup.out’

view process

netstat -lntp

Stop running (PID --> process number)

kill PID

2. Go project deployment problem

2.1. Problem 1: The startup error reports that the file cannot be executed, and the packaging type is wrong

[root@iZwz9d9v06uh0jnrexcuk9Z app]# chmod +x main-app 
[root@iZwz9d9v06uh0jnrexcuk9Z app]# ./main-app 
-bash: ./main-app: cannot execute binary file: Exec format error

Go back to the development environment compiler, go envcheck the modified properties, and find that no environment has been modified successfully

go env

Use the global modification command

# 设置环境
go env -w CGO_ENABLED=0
go env -w GOOS=linux
go env -w GOARCH=amd64
# 打包命令  main-linux 为打包文件名
go build -o main-app main.go

go envCheck the modification status again : the modification is successful

go env

2.2. Problem 2: The startup error reports that the file with the relative path cannot be accessed

2022/11/10 20:22:35 Fail to parse ‘conf/app.ini’: open conf/app.ini: no such file or directory

The file cannot be read because of the following code:

Cfg, err = ini.Load("conf/app.ini")
if err != nil {
    
    
    log.Fatalf("Fail to parse 'conf/app.ini': %v", err)
    os.Exit(1)
}

The relative directory is used to read the file, which is no problem in the development environment, but once it is packaged into a binary file for official deployment, there will be problems.

Generally speaking, there are two methods, splicing paths and creating new folders.

Splicing paths is a bit more troublesome. It is not a common sense to splice the parent path with the current path. It is meaningless to do so.

It is much simpler to create a new folder. Isn’t it unable to find the relative path? We can directly copy the file resource to the corresponding server location to access it.

as follows:

[root@iZwz9d9v06uh0jnrexcuk9Z app]# ll
drwxr-xr-x 2 root root     4096 Nov  1 20:25 conf
-rwxr-xr-x 1 root root 16612864 Nov 10 20:33 main-app

2.3. Question 3: Use fresh hot deployment to continue development and report errors

fork/exec tmp\runner-build.exe: This version of %1 is not compatible with the version of Windows you’re running. Check your computer’s system information and then contact the software publisher.

GOOS=linuxChange GOOS=windowsback to normal

go env -w GOOS=windows
# 再次启动项目成功
fresh

You go env -w GOOS=linuxonly , and so on.


essay

insert image description here

Guess you like

Origin blog.csdn.net/m0_48489737/article/details/127796420