关于发布,mac开发,部署到centos7 我的测试是在src下,一个单main.go文件和一个静态文件夹,步骤如下
src下的build,生成了可以在64位的linux下跑的二进制文件,这
只是打包了.go文件
.
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go // 打包linux环境
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o main .
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go // 打包win环境 64位
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build main.go // 打包win环境 32位
静态文件的处理,无论如何,静态文件目录需要
绝对路径
,这样就需要在开发环境调试通以后,把路径改成绝对路径.这个获取绝对路径的代码是这样的:
1 | // 获取绝对路径 |
再记录个获取随机数1
2
3
4
5
6// RandomInt 0~5的随机数
func RandomInt() int {
o := rand.New(rand.NewSource(time.Now().UnixNano()))
num := o.Intn(6)
return num
}
go run main.go 执行生成的二进制文件在一个随机目录里,停了就删掉了…所以静态文件目录只能是相对路径.
…. go build main.go 之后,静态文件的目录应该scp到服务器上.目录要一致.
再下来运行,后台运行..
nohup ./main &
再下来是ningx的代理….就这样.
如何更新
停止nohup ,一般就查看端口,然后停止
lsof -i:10080 # 查看10080端口的pid 或者:netstat -anp | grep 10080 kill -9 13969 # 停止占用端口的pid nohup main & # 启动
关于守护进程的工具 supervisor,安装后,有一个echo_supervisord_conf 的工具,命令生成一个配置文件,(安装要py2.7,看官网吧)
echo_supervisord_conf > supervisord.conf
- 到这个配置文件所在目录,
cd \etc\supervisor\
- mkdir conf.d
- cd conf.d
- 创建自己需要启动的.ini文件,一个启动一个文件
- cd.. ,打开supervisord.conf,取消最后的
[include]
和files=xxx
的注释,把4创建的ini文件路径写进来 - 启动 supervisord -c supervisord.conf
- 查看状态:执行 supervisorctl ,status,重启:reload
报错:基本上都是自己的错.也可能是端口被占,如下
错误提示 [root@xxxxx supervisor]# supervisord -c supervisord.conf Unlinking stale socket /tmp/supervisor.sock 或者 Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord. 执行 unlink /tmp/supervisor.sock 状态提示 BACKOFF Exited too quickly (process log may have details) supervisorctl supervisorctl> reload