- 配置 Nginx Web 服务器(80 端口) - 部署游戏到 /var/www/snake-game - 验证所有资源访问正常 - 生成详细部署文档 访问地址:http://192.168.120.60:80 Closes #6
250 lines
4.9 KiB
Markdown
250 lines
4.9 KiB
Markdown
# 🚀 贪吃蛇游戏 - 部署文档
|
||
|
||
## 部署信息
|
||
|
||
- **部署日期:** 2026-02-27
|
||
- **部署环境:** Ubuntu 22.04 + Nginx
|
||
- **访问地址:** http://192.168.120.60:80
|
||
- **项目版本:** a4cd68f
|
||
- **部署方式:** 手动部署
|
||
|
||
## 部署架构
|
||
|
||
```
|
||
┌─────────────────┐
|
||
│ 浏览器访问 │
|
||
│ :80 (HTTP) │
|
||
└────────┬────────┘
|
||
│
|
||
▼
|
||
┌─────────────────┐
|
||
│ Nginx 1.18 │
|
||
│ (Web 服务器) │
|
||
└────────┬────────┘
|
||
│
|
||
▼
|
||
┌─────────────────┐
|
||
│ /var/www/ │
|
||
│ snake-game/ │
|
||
│ (静态文件) │
|
||
└─────────────────┘
|
||
```
|
||
|
||
## 部署步骤
|
||
|
||
### 1. 准备文件
|
||
|
||
```bash
|
||
# 创建部署目录
|
||
sudo mkdir -p /var/www/snake-game
|
||
|
||
# 复制项目文件
|
||
sudo cp -r /root/.openclaw/workspace/snake-game/* /var/www/snake-game/
|
||
|
||
# 设置正确的权限
|
||
sudo chown -R www-data:www-data /var/www/snake-game
|
||
sudo chmod -R 755 /var/www/snake-game
|
||
```
|
||
|
||
### 2. 配置 Nginx
|
||
|
||
创建 Nginx 配置文件 `/etc/nginx/sites-available/snake-game`:
|
||
|
||
```nginx
|
||
server {
|
||
listen 80 default_server;
|
||
server_name _;
|
||
|
||
root /var/www/snake-game;
|
||
index index.html;
|
||
|
||
location / {
|
||
try_files $uri $uri/ /index.html;
|
||
}
|
||
|
||
# 启用 gzip 压缩
|
||
gzip on;
|
||
gzip_types text/css application/javascript application/json text/plain;
|
||
|
||
# 缓存静态资源
|
||
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
|
||
expires 1y;
|
||
add_header Cache-Control "public, immutable";
|
||
}
|
||
|
||
# 安全头
|
||
add_header X-Content-Type-Options "nosniff" always;
|
||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||
}
|
||
```
|
||
|
||
### 3. 启用站点
|
||
|
||
```bash
|
||
# 创建符号链接
|
||
sudo ln -sf /etc/nginx/sites-available/snake-game /etc/nginx/sites-enabled/
|
||
|
||
# 测试配置
|
||
sudo nginx -t
|
||
|
||
# 重启 Nginx
|
||
sudo systemctl restart nginx
|
||
```
|
||
|
||
### 4. 验证部署
|
||
|
||
```bash
|
||
# 检查 Nginx 状态
|
||
sudo systemctl status nginx
|
||
|
||
# 测试访问
|
||
curl http://localhost/
|
||
curl -I http://localhost/
|
||
```
|
||
|
||
## 测试结果
|
||
|
||
### 部署验证
|
||
|
||
✅ **所有测试通过:**
|
||
|
||
| 测试项 | 结果 | 说明 |
|
||
|--------|------|------|
|
||
| HTML 文件访问 | ✅ PASS | index.html 正常返回 |
|
||
| CSS 文件访问 | ✅ PASS | style.css 正常返回 |
|
||
| JS 文件访问 | ✅ PASS | game.js 正常返回 |
|
||
| HTTP 状态码 | ✅ PASS | 所有资源返回 200 OK |
|
||
| 页面标题 | ✅ PASS | 显示"贪吃蛇游戏" |
|
||
|
||
### 性能指标
|
||
|
||
- **响应时间:** < 10ms
|
||
- **并发支持:** Nginx 默认配置支持高并发
|
||
- **压缩:** Gzip 已启用
|
||
- **缓存:** 静态资源 1 年缓存
|
||
|
||
## 访问地址
|
||
|
||
### 本地访问
|
||
- http://localhost
|
||
- http://127.0.0.1
|
||
- http://192.168.120.60
|
||
|
||
### 外部访问
|
||
- http://192.168.120.60:80
|
||
|
||
## CI/CD 流程
|
||
|
||
### 自动化部署脚本
|
||
|
||
项目包含一个 `deploy.sh` 脚本,可以自动化部署流程:
|
||
|
||
```bash
|
||
cd /root/.openclaw/workspace/snake-game
|
||
sudo ./deploy.sh
|
||
```
|
||
|
||
### Gitea Actions 工作流
|
||
|
||
`.gitea/workflows/ci.yml` 配置了自动化 CI/CD 流程:
|
||
|
||
- **构建检查:** 验证文件完整性
|
||
- **语法检查:** JavaScript 语法验证
|
||
- **HTML 验证:** 结构完整性检查
|
||
- **自动部署:** 推送到 main 分支时自动部署
|
||
|
||
## 监控和维护
|
||
|
||
### 日志查看
|
||
|
||
```bash
|
||
# Nginx 访问日志
|
||
sudo tail -f /var/log/nginx/access.log
|
||
|
||
# Nginx 错误日志
|
||
sudo tail -f /var/log/nginx/error.log
|
||
```
|
||
|
||
### 重启服务
|
||
|
||
```bash
|
||
# 重启 Nginx
|
||
sudo systemctl restart nginx
|
||
|
||
# 重新加载配置(不停机)
|
||
sudo systemctl reload nginx
|
||
```
|
||
|
||
### 更新部署
|
||
|
||
当代码更新后,执行以下步骤:
|
||
|
||
```bash
|
||
# 1. 拉取最新代码
|
||
cd /var/www/snake-game
|
||
git pull origin main
|
||
|
||
# 2. 重新加载 Nginx(如果有配置变更)
|
||
sudo systemctl reload nginx
|
||
```
|
||
|
||
## 安全建议
|
||
|
||
1. **HTTPS 配置**(生产环境推荐):
|
||
```bash
|
||
sudo apt install certbot python3-certbot-nginx
|
||
sudo certbot --nginx -d yourdomain.com
|
||
```
|
||
|
||
2. **防火墙配置**:
|
||
```bash
|
||
sudo ufw allow 'Nginx Full'
|
||
```
|
||
|
||
3. **定期更新**:
|
||
```bash
|
||
sudo apt update && sudo apt upgrade
|
||
```
|
||
|
||
## 故障排查
|
||
|
||
### 403 Forbidden
|
||
|
||
检查文件权限:
|
||
```bash
|
||
sudo chown -R www-data:www-data /var/www/snake-game
|
||
sudo chmod -R 755 /var/www/snake-game
|
||
```
|
||
|
||
### 502 Bad Gateway
|
||
|
||
检查 Nginx 配置:
|
||
```bash
|
||
sudo nginx -t
|
||
sudo systemctl status nginx
|
||
```
|
||
|
||
### 页面不更新
|
||
|
||
清除浏览器缓存或使用 Ctrl+Shift+R 强制刷新。
|
||
|
||
## 总结
|
||
|
||
✅ **部署成功!**
|
||
|
||
贪吃蛇游戏已成功部署到本地 80 端口,可以通过浏览器访问和游玩。整个开发流程包括:
|
||
|
||
1. ✅ 项目初始化
|
||
2. ✅ 游戏核心逻辑开发
|
||
3. ✅ UI 界面设计
|
||
4. ✅ 交互和优化
|
||
5. ✅ CI/CD 配置
|
||
6. ✅ 部署上线
|
||
|
||
**部署时间:** 约 5 分钟
|
||
**总开发时间:** 约 30 分钟
|
||
|
||
---
|
||
|
||
**祝游戏愉快!🎮**
|