前言
在 CentOS 9 x64 系統上,可以通過以下步驟來部署 Golang 服務。
1. 安裝必要的軟件包
安裝以下軟件包:
- Golang:Golang 編程語言
- Nginx:Web 服務器
- Supervisor:進程管理工具
- Git:版本控制工具
- 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
(完)