delicious
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o delicious
myapp
二进制文件、配置文件和静态文件等上传到服务器的/var/www/delicious.yangpeiyuan.com
目录下。目录结构如下:.
|-- config
| `-- config.toml
|-- delicious
|-- static
| |-- css
| | |-- bulma.css
| | `-- style.css
| |-- favicon.ico
| `-- imgs
| |-- apple-touch-icon.png
| |-- favicon-32x32.png
| `-- idev.png
`-- templates
|-- add.html
|-- close.html
|-- detail.html
|-- footer.html
|-- head.html
|-- index.html
|-- login.html
`-- partial.html
/usr/lib/systemd/system/
创建一个新的 systemd 服务文件:sudo vim /usr/lib/systemd/system/delicious.service
在打开的编辑器中,添加以下内容:
[Unit]
Description=My Golang Web Application
After=network.target
[Service]
ExecStart=/var/www/delicious.yangpeiyuan.com/delicious
Restart=always
User=nobody
Group=nogroup
Environment="GO_ENV=production"
WorkingDirectory=/var/www/delicious.yangpeiyuan.com
[Install]
WantedBy=multi-user.target
对于 Golang 程序的 systemd 服务配置,User 和 Group 通常设置如下: 如果是 web 应用或需要网络访问的服务:
Group=www-data
如果是系统级服务或后台程序:
Group=nogroup
如果是特定用户运行的应用:
User=<specific_username>
Group=<specific_groupname>
如果需要 root 权限:
User=root
Group=root
这里需要注意Environment="GO_ENV=production"
,指在 systemd 服务文件设置程序所需的环境变量,以配合 gin 框架的 release 模式
//设置 Gin 为发布模式
if models.GetEnvironment() == "production" {
gin.SetMode(gin.ReleaseMode)
} else {
fmt.Println(models.GetEnvironment())
}
func GetEnvironment() string {
env := os.Getenv("GO_ENV")
if env == "" {
env = "development"
}
return env
}
//重新加载systemd
sudo systemctl daemon-reload
//启动服务
sudo systemctl start delicious
//启用服务自动启动
sudo systemctl enable delicious
//检查服务状态
sudo systemctl status delicious
//查看日志 (可选)
sudo journalctl -u delicious
//重启服务
sudo systemctl restart delicious
此时就可以打开浏览器输入http://服务器公网ip:端口
查看应用程序的展示效果了。
//增加二进制文件,可执行权限
sudo chmod +x delicious
sudo journalctl -u memory.service -n 50 --no-pager
编辑/etc/nginx/sites-available/yangpeiyuan.com.conf
server {
listen 80;
server_name delicious.yangpeiyuan.com;
location / {
proxy_pass http://localhost:9090;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name memory.yangpeiyuan.com;
location / {
proxy_pass http://localhost:9091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name yangpeiyuan.com www.yangpeiyuan.com;
location / {
root /var/www/yangpeiyuan.com;
index index.html index.htm;
try_files $uri $uri/ =404;
}
}
//启用 Nginx 配置。创建一个符号链接到 sites-enabled 目录
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
//检查配置文件语法
nginx -t
//重启nginx
sudo systemctl reload nginx
在您的域名注册商的 DNS 设置中,添加 A 记录,将您的域名指向您服务器的 IP 地址。
使用 GitHub Action 自动编译和上传到服务器。
name: Build and Deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22.4
- name: Build
run: go build -v ./...
- name: Build binary
run: GOOS=linux GOARCH=amd64 go build -o delicious
- name: Prepare static files
run: |
mkdir -p deploy/config
cp -r static/ deploy/static
cp -r templates/ deploy/templates
cp config/config.toml deploy/config
cp delicious deploy/
- name: Deploy to VPS
env:
PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
HOST: ${{ secrets.SERVER_HOST }}
USER: ${{ secrets.SERVER_USER }}
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
scp -i private_key -o StrictHostKeyChecking=no -r deploy/* ${USER}@${HOST}:/var/www/deploy/
ssh -i private_key -o StrictHostKeyChecking=no ${USER}@${HOST} '
sudo rm -rf /var/www/delicious.yangpeiyuan.com/*
sudo mv /var/www/deploy/delicious /var/www/delicious.yangpeiyuan.com/
sudo mv /var/www/deploy/* /var/www/delicious.yangpeiyuan.com/
sudo chown -R www-data:www-data /var/www/delicious.yangpeiyuan.com/static
sudo chmod -R 775 /var/www/delicious.yangpeiyuan.com/static
sudo systemctl restart delicious.service
'