在 Debian 上部署 Go(Golang)通常有两种方式:
下面分别说明,并附上验证与简单部署示例。
sudo apt update
sudo apt upgrade -y
sudo apt install golang -y
go version
⚠️ Debian 仓库中的 Go 版本通常落后于官方版本,生产环境不建议。
先去官网查看最新版本:https://go.dev/dl/
cd /tmp
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
/usr/localsudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
编辑用户环境变量(推荐):
nano ~/.bashrc
在末尾添加:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
使配置生效:
source ~/.bashrc
go version
mkdir ~/hello && cd ~/hello
nano main.go
package main
import "fmt"
func main() {
fmt.Println("Hello Debian!")
}
运行:
go run main.go
编译:
go build -o hello
./hello
示例:
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=Go App
After=network.target
[Service]
ExecStart=/home/user/hello/hello
WorkingDirectory=/home/user/hello
User=user
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl enable myapp
location / {
proxy_pass http://127.0.0.1:8080;
}
PATH 是否包含 /usr/local/go/binsource ~/.bashrc可使用:
g 工具asdf如果你告诉我:
我可以给你更精确的部署方案。