CentOS 9 x64 使用 Nginx、Supervisor 部署 Go/Golang 服务

前言

在 CentOS 9 x64 系统上,可以通过以下步骤来部署 Golang 服务。

1. 安装必要的软件包

安装以下软件包:

  1. Golang:Golang 编程语言
  2. Nginx:Web 服务器
  3. Supervisor:进程管理工具
  4. Git:版本控制工具
  5. EPEL:扩展软件包

可以通过以下命令来安装:

yum -y update
yum install nginx golang epel-release supervisor git -y

2. 生成 SSH 密钥[可选]

为 Git 生成 SSH 密钥,以便于进行代码管理。可以通过以下命令来生成:

cd ~
ssh-keygen -t rsa -C "your_email@example.com"
cat ~/.ssh/id_rsa.pub

将公钥添加到 Git 仓库中。

3. 下载代码

将代码下载到服务器上,可以使用 Git 命令来下载代码:

cd /
mkdir web
cd web
# or `git clone https://...`
git clone git@github.com:your_name/your_repo.git
cd /web/your_repo

4. 运行应用

在应用根目录下运行以下命令来初始化应用:

go run scripts/init/main.go

5. 编译应用

使用以下命令来编译应用:

GOOS=linux GOARCH=amd64 go build -o dist/app-linux-amd64 cmd/app/main.go

6. 配置 Supervisor

/etc/supervisord.d 目录下创建一个新的配置文件 app.ini,并添加以下内容:

[program:app]
directory=/web/your_repo
command=/web/your_repo/dist/app-linux-amd64 -param1="value1" -param2="value2"
autostart=true
autorestart=true
stderr_logfile=/web/your_repo/log/app.err
stdout_logfile=/web/your_repo/log/app.log
environment=ENV_VAR1="value3",ENV_VAR2="value4"

启动 Supervisor 并检查状态:

systemctl start supervisord
systemctl status supervisord
systemctl enable supervisord
ps -ef|grep supervisord

后续更新重启 app

# Start
supervisorctl start app
# Stop
supervisorctl stop app
# Restart
supervisorctl restart app

7. 配置 Nginx

/etc/nginx 目录下打开 nginx.conf 文件,并修改以下内容:

listen       80;
# listen       [::]:80;
include /etc/nginx/conf.d/*.conf;
# 指向 Golang 的 Nginx Server 配置
include /your_path/your_app.conf;

然后重新启动 Nginx 并检查状态:

systemctl restart nginx
systemctl status nginx

现在,Golang 应用已经成功部署到 CentOS 服务器上了。

版权声明

本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者后除和本文原始地址:https://blog.mazey.net/3696.html

(完)

posted @ 2023-07-02 15:05  后除  阅读(29)  评论(0编辑  收藏  举报