first commit
This commit is contained in:
@@ -0,0 +1,471 @@
|
||||
# TGOIM 公司官网部署文档
|
||||
|
||||
**域名**:www.tgoim.com
|
||||
**服务器**:阿里云 ECS Ubuntu 24.04
|
||||
**Web 服务器**:Nginx(已安装,已部署过其他 HTTPS 站点)
|
||||
**HTTPS 证书**:Let's Encrypt(certbot)
|
||||
|
||||
---
|
||||
|
||||
## 一、部署前准备
|
||||
|
||||
### 1.1 本地准备工作
|
||||
|
||||
```bash
|
||||
# 在本地项目根目录 (d:\Git\company_website) 执行
|
||||
|
||||
# 1. 更新 requirements.txt,确保包含生产环境需要的依赖
|
||||
# (已有 Django + Pillow,生产环境还需要 gunicorn)
|
||||
|
||||
# 2. 创建生产环境配置文件
|
||||
```
|
||||
|
||||
### 1.2 服务器信息确认
|
||||
|
||||
在服务器上执行以下命令确认环境:
|
||||
|
||||
```bash
|
||||
# 确认操作系统
|
||||
lsb_release -a
|
||||
|
||||
# 确认 Nginx 已安装且运行
|
||||
nginx -v
|
||||
sudo systemctl status nginx
|
||||
|
||||
# 确认 Python 3 版本
|
||||
python3 --version
|
||||
|
||||
# 确认 certbot 是否已安装(如果之前用过)
|
||||
certbot --version
|
||||
|
||||
# 确认是否已安装 python3-venv 和 python3-pip
|
||||
python3 -m venv --help
|
||||
pip3 --version
|
||||
```
|
||||
|
||||
### 1.3 域名解析确认
|
||||
|
||||
在阿里云控制台 → 云解析 DNS 中确认:
|
||||
- `www.tgoim.com` 的 A 记录已解析到 ECS 公网 IP
|
||||
- `tgoim.com`(裸域名)也建议解析到同一 IP
|
||||
|
||||
---
|
||||
|
||||
## 二、服务器环境安装
|
||||
|
||||
### 2.1 安装必要依赖
|
||||
|
||||
```bash
|
||||
# 更新系统
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
|
||||
# 安装 Python 虚拟环境和 pip(如果未安装)
|
||||
sudo apt install -y python3 python3-pip python3-venv python3-dev
|
||||
|
||||
# 安装 Supervisor(进程守护)
|
||||
sudo apt install -y supervisor
|
||||
|
||||
# 安装 certbot(如果未安装)
|
||||
sudo apt install -y certbot python3-certbot-nginx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、代码上传
|
||||
|
||||
### 3.1 方式一:使用 Git 拉取(推荐)
|
||||
|
||||
```bash
|
||||
# 在服务器上创建项目目录
|
||||
cd /var/www
|
||||
sudo mkdir tgoim
|
||||
sudo chown -R $USER:$USER /var/www/tgoim
|
||||
|
||||
# 克隆代码(替换为你的 Git 仓库地址)
|
||||
cd /var/www/tgoim
|
||||
git clone <你的仓库地址> .
|
||||
|
||||
# 如果代码不在 Git 上,使用方式二
|
||||
```
|
||||
|
||||
### 3.2 方式二:使用 SCP 上传(本地 Windows)
|
||||
|
||||
在本地 PowerShell 中执行:
|
||||
|
||||
```powershell
|
||||
# 先打包(排除虚拟环境和数据库)
|
||||
cd d:\Git\company_website
|
||||
tar -czf tgoim_deploy.tar.gz --exclude='.venv' --exclude='db.sqlite3' --exclude='__pycache__' --exclude='*.pyc' --exclude='staticfiles' --exclude='media' .
|
||||
|
||||
# 上传到服务器(替换 user 和 server_ip)
|
||||
scp tgoim_deploy.tar.gz user@server_ip:/tmp/
|
||||
```
|
||||
|
||||
然后在服务器上解压:
|
||||
|
||||
```bash
|
||||
cd /var/www
|
||||
sudo mkdir tgoim
|
||||
sudo chown -R $USER:$USER /var/www/tgoim
|
||||
cd /var/www/tgoim
|
||||
tar -xzf /tmp/tgoim_deploy.tar.gz
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 四、项目配置
|
||||
|
||||
### 4.1 创建虚拟环境并安装依赖
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
|
||||
# 创建虚拟环境
|
||||
python3 -m venv venv
|
||||
|
||||
# 激活并安装依赖
|
||||
source venv/bin/activate
|
||||
pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
pip install gunicorn
|
||||
```
|
||||
|
||||
### 4.2 配置生产环境变量
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
|
||||
# 创建 .env 文件
|
||||
cat > .env << 'EOF'
|
||||
DJANGO_SECRET_KEY=$(python3 -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())")
|
||||
DJANGO_DEBUG=False
|
||||
DJANGO_ALLOWED_HOSTS=www.tgoim.com,tgoim.com,127.0.0.1,localhost
|
||||
EOF
|
||||
|
||||
# 验证
|
||||
cat .env
|
||||
```
|
||||
|
||||
### 4.3 数据库迁移和静态文件收集
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
source venv/bin/activate
|
||||
|
||||
# 数据库迁移(使用 SQLite,直接创建)
|
||||
python manage.py migrate
|
||||
|
||||
# 收集静态文件
|
||||
python manage.py collectstatic --noinput
|
||||
|
||||
# 创建超级管理员
|
||||
python manage.py createsuperuser
|
||||
```
|
||||
|
||||
### 4.4 设置文件权限
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
|
||||
# 媒体目录需要可写(用于上传文件)
|
||||
mkdir -p media
|
||||
sudo chown -R www-data:www-data media
|
||||
|
||||
# 数据库文件需要可写
|
||||
sudo chown www-data:www-data db.sqlite3
|
||||
|
||||
# 其他目录保持只读
|
||||
sudo chown -R $USER:$USER .
|
||||
sudo chmod -R 755 .
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 五、Gunicorn 配置
|
||||
|
||||
### 5.1 创建 Gunicorn 配置文件
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
cat > gunicorn_config.py << 'EOF'
|
||||
import multiprocessing
|
||||
|
||||
bind = "127.0.0.1:8000"
|
||||
workers = multiprocessing.cpu_count() * 2 + 1
|
||||
worker_class = "sync"
|
||||
worker_connections = 1000
|
||||
max_requests = 5000
|
||||
timeout = 30
|
||||
keepalive = 5
|
||||
|
||||
preload_app = True
|
||||
daemon = False
|
||||
|
||||
accesslog = "/var/log/gunicorn/tgoim_access.log"
|
||||
errorlog = "/var/log/gunicorn/tgoim_error.log"
|
||||
loglevel = "info"
|
||||
EOF
|
||||
```
|
||||
|
||||
### 5.2 创建日志目录
|
||||
|
||||
```bash
|
||||
sudo mkdir -p /var/log/gunicorn
|
||||
sudo chown -R www-data:www-data /var/log/gunicorn
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 六、Supervisor 配置
|
||||
|
||||
### 6.1 创建 Supervisor 配置
|
||||
|
||||
```bash
|
||||
sudo cat > /etc/supervisor/conf.d/tgoim.conf << 'EOF'
|
||||
[program:tgoim]
|
||||
command=/var/www/tgoim/venv/bin/gunicorn company_website.wsgi:application -c /var/www/tgoim/gunicorn_config.py
|
||||
directory=/var/www/tgoim
|
||||
user=www-data
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=10
|
||||
stopwaitsecs=30
|
||||
stdout_logfile=/var/log/supervisor/tgoim_stdout.log
|
||||
stderr_logfile=/var/log/supervisor/tgoim_stderr.log
|
||||
environment=DJANGO_SETTINGS_MODULE="company_website.settings"
|
||||
EOF
|
||||
```
|
||||
|
||||
### 6.2 启动并验证
|
||||
|
||||
```bash
|
||||
# 重新加载 Supervisor 配置
|
||||
sudo supervisorctl reread
|
||||
sudo supervisorctl update
|
||||
|
||||
# 启动服务
|
||||
sudo supervisorctl start tgoim
|
||||
|
||||
# 查看状态
|
||||
sudo supervisorctl status tgoim
|
||||
|
||||
# 如果显示 RUNNING 则正常
|
||||
```
|
||||
|
||||
如果启动失败,查看日志排查:
|
||||
|
||||
```bash
|
||||
sudo tail -30 /var/log/supervisor/tgoim_stderr.log
|
||||
sudo tail -30 /var/log/gunicorn/tgoim_error.log
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 七、Nginx 配置
|
||||
|
||||
### 7.1 创建 Nginx 站点配置
|
||||
|
||||
由于服务器上已有其他 HTTPS 站点,先创建一个 HTTP 的配置,申请证书后再升级为 HTTPS。
|
||||
|
||||
```bash
|
||||
sudo cat > /etc/nginx/sites-available/tgoim.com << 'EOF'
|
||||
# HTTP 配置(用于申请证书,后续 certbot 会自动升级为 HTTPS)
|
||||
server {
|
||||
listen 80;
|
||||
server_name www.tgoim.com tgoim.com;
|
||||
|
||||
root /var/www/tgoim;
|
||||
|
||||
# 日志
|
||||
access_log /var/log/nginx/tgoim_access.log;
|
||||
error_log /var/log/nginx/tgoim_error.log;
|
||||
|
||||
# Gzip 压缩
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
|
||||
|
||||
# 静态文件直接由 Nginx 处理
|
||||
location /static/ {
|
||||
alias /var/www/tgoim/staticfiles/;
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
|
||||
location /media/ {
|
||||
alias /var/www/tgoim/media/;
|
||||
expires 7d;
|
||||
add_header Cache-Control "public";
|
||||
}
|
||||
|
||||
# Django 动态请求转发到 Gunicorn
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8000;
|
||||
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;
|
||||
proxy_connect_timeout 300;
|
||||
proxy_read_timeout 300;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
### 7.2 启用站点
|
||||
|
||||
```bash
|
||||
# 建立软链接启用站点
|
||||
sudo ln -sf /etc/nginx/sites-available/tgoim.com /etc/nginx/sites-enabled/
|
||||
|
||||
# 检查 Nginx 配置
|
||||
sudo nginx -t
|
||||
|
||||
# 如果显示 syntax is ok 则重启 Nginx
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 八、申请 Let's Encrypt HTTPS 证书
|
||||
|
||||
### 8.1 使用 certbot 自动申请和配置
|
||||
|
||||
由于服务器已部署过其他 HTTPS 站点,certbot 应该已有安装。直接执行:
|
||||
|
||||
```bash
|
||||
# 申请证书并自动配置 Nginx(certbot 会自动修改配置)
|
||||
sudo certbot --nginx -d www.tgoim.com -d tgoim.com
|
||||
```
|
||||
|
||||
执行过程中会提示:
|
||||
1. 输入邮箱(用于接收证书过期提醒)
|
||||
2. 同意服务条款(输入 Y)
|
||||
3. 是否接收 EFF 邮件(选 Y 或 N 均可)
|
||||
4. 是否强制 HTTPS 重定向(选 2: Redirect)
|
||||
|
||||
### 8.2 验证证书
|
||||
|
||||
```bash
|
||||
# 检查证书是否申请成功
|
||||
sudo ls -la /etc/letsencrypt/live/tgoim.com/
|
||||
|
||||
# 查看 Nginx 配置,certbot 应该已自动修改
|
||||
sudo cat /etc/nginx/sites-available/tgoim.com
|
||||
```
|
||||
|
||||
正常情况下 certbot 会自动在配置中添加:
|
||||
- `listen 443 ssl http2;`
|
||||
- SSL 证书路径
|
||||
- HTTP 到 HTTPS 的 301 重定向
|
||||
|
||||
### 8.3 证书自动续期
|
||||
|
||||
Let's Encrypt 证书有效期 90 天,certbot 会自动设置定时器续期。验证:
|
||||
|
||||
```bash
|
||||
# 检查续期定时器
|
||||
sudo systemctl list-timers | grep certbot
|
||||
|
||||
# 测试续期(不会真的续期,只检查流程)
|
||||
sudo certbot renew --dry-run
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 九、验证部署
|
||||
|
||||
### 9.1 本地测试(跳过 DNS)
|
||||
|
||||
在服务器本地测试:
|
||||
|
||||
```bash
|
||||
curl -I http://127.0.0.1:8000/
|
||||
curl -I http://127.0.0.1/ --header "Host: www.tgoim.com"
|
||||
```
|
||||
|
||||
### 9.2 外网测试
|
||||
|
||||
浏览器访问:
|
||||
- http://www.tgoim.com → 应该自动跳转到 https://www.tgoim.com
|
||||
- https://www.tgoim.com → 应该正常显示网站
|
||||
- https://www.tgoim.com/admin/ → 后台登录页
|
||||
|
||||
### 9.3 检查清单
|
||||
|
||||
- [ ] 首页正常显示
|
||||
- [ ] 各栏目页面可访问
|
||||
- [ ] 多语言切换正常(中文/英文)
|
||||
- [ ] 后台管理可登录
|
||||
- [ ] 图片和静态资源加载正常
|
||||
- [ ] HTTP 自动跳转到 HTTPS
|
||||
- [ ] 浏览器地址栏显示锁图标(安全连接)
|
||||
|
||||
---
|
||||
|
||||
## 十、后续维护
|
||||
|
||||
### 10.1 更新代码
|
||||
|
||||
```bash
|
||||
cd /var/www/tgoim
|
||||
|
||||
# 拉取最新代码
|
||||
git pull origin main
|
||||
|
||||
# 激活虚拟环境
|
||||
source venv/bin/activate
|
||||
|
||||
# 安装新依赖(如有)
|
||||
pip install -r requirements.txt
|
||||
|
||||
# 数据库迁移
|
||||
python manage.py migrate
|
||||
|
||||
# 收集静态文件
|
||||
python manage.py collectstatic --noinput
|
||||
|
||||
# 重启服务
|
||||
sudo supervisorctl restart tgoim
|
||||
```
|
||||
|
||||
### 10.2 常用命令
|
||||
|
||||
```bash
|
||||
# 查看服务状态
|
||||
sudo supervisorctl status tgoim
|
||||
|
||||
# 重启服务
|
||||
sudo supervisorctl restart tgoim
|
||||
|
||||
# 停止服务
|
||||
sudo supervisorctl stop tgoim
|
||||
|
||||
# 查看 Gunicorn 日志
|
||||
sudo tail -f /var/log/gunicorn/tgoim_error.log
|
||||
|
||||
# 查看 Nginx 日志
|
||||
sudo tail -f /var/log/nginx/tgoim_access.log
|
||||
|
||||
# 测试 Nginx 配置
|
||||
sudo nginx -t
|
||||
|
||||
# 重载 Nginx
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
### 10.3 备份
|
||||
|
||||
```bash
|
||||
# 备份数据库和媒体文件
|
||||
cd /var/www/tgoim
|
||||
tar -czf /tmp/tgoim_backup_$(date +%Y%m%d).tar.gz db.sqlite3 media/
|
||||
|
||||
# 下载到本地(从本地执行)
|
||||
scp user@server_ip:/tmp/tgoim_backup_xxx.tar.gz .
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
*部署文档结束*
|
||||
Reference in New Issue
Block a user