一、环境的配置
-
1、在
centos
服务器上安装go
wget https://dl.google.com/go/go1.17.1.linux-amd64.tar.gz https://dl.google.com/go/go1.20.2.linux-amd64.tar.gz
-
2、解压到目录下
sudo tar -C /usr/local -xf go1.17.1.linux-amd64.tar.gz
-
3、修改
~/.bash_profile
文件,ubuntu
系统中是.bashrc
# go程序 export PATH=$PATH:/usr/local/go/bin
-
4、配置生成有效
source ~/.bash_profile
-
5、查看安装是否成功
go env
-
6、开启
mod
及配置代理go env -w GO111MODULE=on # 配置代理 go env -w GOPROXY=https://goproxy.cn,direct
-
7、安装
supervisor
sudo yum -y install epel-release yum install supervisor
-
8、如果你项目依赖了
mysql
、redis
请自行安装
二、项目打包
方式一直接使用git
在服务器上拉取代码
-
1、直接在服务器上使用
git
拉取代码 -
2、同步项目的依赖包
go mod tidy
-
3、打包项目
go run build # or go build -o admin-api main.go
方式二用bee
工具打包
-
1、在
beego
官方根据方式安装bee
工具,连接地址 -
2、进入项目目录下直接打包
bee pack -be GOOS=linux
-
3、将项目上传到
centos
服务器上# window系统下服务器可以安装这个工具 yum install lrzsz # mac电脑就直接使用scp
-
4、在服务器上解压项目
tar -xvf gin-admin-api.tar.gz
三、使用supervisor
部署项目
-
1、查看
supervisor
的配置地址cat /etc/supervisord.conf
表示自己创建的
xx.ini
文件只要放在supervisord.d/
目录下就可以; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. [include] files = supervisord.d/*.ini [root@iZwz9et2qekjwu8mwmxgxhZ supervisord.d]# pwd /etc/supervisord.d [root@iZwz9et2qekjwu8mwmxgxhZ supervisord.d]#
-
2、在
/etc/supervisord.d/
目录下创建一个gin-admin-api.ini
的文件,复制的时候先按下set paste# gin-admin-api表示项目名 [program:gin-admin-api] # 项目文件夹 directory = /home/gin-admin-api # 项目可执行文件位置 command = /home/gin-admin-api/main autostart = true startsecs = 5 user = root # 执行程序的用户 autorestart=true # 是否自动重启 redirect_stderr = true # 输出日志文件的位置 stdout_logfile = /home/gin-admin-api/logs/supervisor.log port=127.0.0.1:9090
-
3、重启配置文件
supervisord -c /etc/supervisord.conf supervisorctl reload
这里如果说端口占用并不是值
9090
被占用了,而是supervisord
的端口,这时候就要杀了supervisord
进程-
查看进程,然后直接
kill
就可以ps aux | grep supervisord
-
-
4、可以查看
supervisord
的信息supervisorctl
-
查看所有的状态
status
-
开始所有的配置
start all
-
更新配置
update
-
重新读取配置
reread
-
-
5、查看当前启动的端口号,确保9090已经启动
netstat -ntlp
-
6、直接在客户端访问(注意阿里云要开放9090的端口)
-
7、自行配置
nginx
反向代理到9090
端口 -
8、查看日志
tail -f 文件名
-
9、参考gin项目,可以使用这个项目来练习部署
-
10、常见的命令
supervisorctl status # 查看状态
supervisorctl shutdown # 关闭所有任务
supervisorctl start # 启动任务
supervisorctl stop # 关闭任务
supervistorctl reload # 重启任务