centos

CentOS Golang项目如何部署

小樊
43
2025-03-07 10:58:07
栏目: 编程语言
Centos服务器限时活动,0元免费领! 查看>>

在CentOS上部署Golang项目通常涉及以下几个步骤:

  1. 安装Go环境
  1. 配置环境变量
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
  1. 编写和编译Go应用程序
  1. 使用systemd管理服务(可选但推荐):
[Unit]
Description=My Go Application
After=syslog.target

[Service]
Type=simple
User=root
WorkingDirectory=/path/to/your/app
ExecStart=/path/to/your/app/myapp
Restart=always
Environment=ENV_VAR1=value1 ENV_VAR2=value2

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start myapp.service
systemctl status myapp.service
systemctl enable myapp.service
  1. 配置Nginx反向代理(可选)
server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        proxy_pass http://localhost:8080; # 假设你的Go应用运行在8080端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
systemctl restart nginx
systemctl status nginx
  1. 访问你的应用

请注意,上述步骤中的路径和文件名应根据你的实际情况进行调整。此外,确保你的防火墙允许外部访问你设置的端口。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:CentOS中Golang项目如何部署

0
看了该问题的人还看了