前言

在 CentOS 9 x64 零碎上,能够通过以下步骤来部署 Golang 服务。

1. 装置必要的软件包

装置以下软件包:

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

能够通过以下命令来装置:

yum -y updateyum 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 webcd web# or `git clone https://...`git clone git@github.com:your_name/your_repo.gitcd /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_repocommand=/web/your_repo/dist/app-linux-amd64 -param1="value1" -param2="value2"autostart=trueautorestart=truestderr_logfile=/web/your_repo/log/app.errstdout_logfile=/web/your_repo/log/app.logenvironment=ENV_VAR1="value3",ENV_VAR2="value4"

启动 Supervisor 并查看状态:

systemctl start supervisordsystemctl status supervisordsystemctl enable supervisordps -ef|grep supervisord

后续更新重启 app

# Startsupervisorctl start app# Stopsupervisorctl stop app# Restartsupervisorctl 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 nginxsystemctl status nginx

当初,Golang 利用曾经胜利部署到 CentOS 服务器上了。

版权申明

本博客所有的原创文章,作者皆保留版权。转载必须蕴含本申明,放弃本文残缺,并以超链接模式注明作者后除和本文原始地址:https://blog.mazey.net/3696.html

(完)