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 "[email protected]"
cat ~/.ssh/id_rsa.pub

將公鑰添加到 Git 倉庫中。

3. 下載代碼

將代碼下載到服務器上,可以使用 Git 命令來下載代碼:

cd /
mkdir web
cd web
# or `git clone https://...`
git clone [email protected]: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

(完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章