创建部署的配置说明
This commit is contained in:
325
DEPLOYMENT.md
Normal file
325
DEPLOYMENT.md
Normal file
@@ -0,0 +1,325 @@
|
||||
# Ubuntu部署方案:Nginx + Gunicorn
|
||||
|
||||
本文档详细说明如何在Ubuntu服务器上部署任务中心管理系统,使用Nginx作为反向代理,Gunicorn作为WSGI服务器,并将服务注册为系统服务。
|
||||
|
||||
## 环境要求
|
||||
|
||||
- Ubuntu 20.04+
|
||||
- Python 3.8+
|
||||
- Nginx 1.18+
|
||||
- Gunicorn 20.0+
|
||||
|
||||
## 1. 服务器准备
|
||||
|
||||
### 1.1 系统更新
|
||||
```bash
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
```
|
||||
|
||||
### 1.2 安装必要依赖
|
||||
```bash
|
||||
sudo apt install -y python3-pip python3-venv python3-dev \
|
||||
build-essential libpq-dev nginx \
|
||||
git curl wget
|
||||
```
|
||||
|
||||
## 2. 项目部署
|
||||
|
||||
### 2.1 创建项目目录
|
||||
```bash
|
||||
sudo mkdir -p /var/www/task_center
|
||||
cd /var/www/task_center
|
||||
```
|
||||
|
||||
### 2.2 克隆项目
|
||||
```bash
|
||||
git clone <repository-url> .
|
||||
```
|
||||
|
||||
### 2.3 创建虚拟环境
|
||||
```bash
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
```
|
||||
|
||||
### 2.4 安装项目依赖
|
||||
```bash
|
||||
pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
如果没有requirements.txt文件:
|
||||
```bash
|
||||
pip install django djangorestframework gunicorn
|
||||
```
|
||||
|
||||
### 2.5 配置环境变量
|
||||
创建.env文件:
|
||||
```bash
|
||||
cat > .env << EOF
|
||||
SECRET_KEY=your-secret-key
|
||||
DEBUG=False
|
||||
ALLOWED_HOSTS=your-domain.com,www.your-domain.com
|
||||
DATABASE_URL=sqlite:///db.sqlite3
|
||||
EOF
|
||||
```
|
||||
|
||||
### 2.6 配置Django设置
|
||||
修改`task_center/settings.py`:
|
||||
```python
|
||||
# 从.env文件加载配置
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
SECRET_KEY = os.getenv('SECRET_KEY')
|
||||
DEBUG = os.getenv('DEBUG') == 'True'
|
||||
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',')
|
||||
```
|
||||
|
||||
安装python-dotenv:
|
||||
```bash
|
||||
pip install python-dotenv
|
||||
```
|
||||
|
||||
### 2.7 初始化数据库
|
||||
```bash
|
||||
python manage.py makemigrations
|
||||
python manage.py migrate
|
||||
```
|
||||
|
||||
### 2.8 创建超级用户
|
||||
```bash
|
||||
python manage.py createsuperuser
|
||||
```
|
||||
|
||||
### 2.9 收集静态文件
|
||||
```bash
|
||||
python manage.py collectstatic
|
||||
```
|
||||
|
||||
## 3. Gunicorn配置
|
||||
|
||||
### 3.1 测试Gunicorn
|
||||
```bash
|
||||
cd /var/www/task_center
|
||||
venv/bin/gunicorn --bind 0.0.0.0:8000 task_center.wsgi:application
|
||||
```
|
||||
|
||||
访问 http://your-server-ip:8000 测试是否正常运行,然后按Ctrl+C停止。
|
||||
|
||||
### 3.2 创建Gunicorn服务文件
|
||||
```bash
|
||||
sudo nano /etc/systemd/system/task_center.service
|
||||
```
|
||||
|
||||
配置内容:
|
||||
```ini
|
||||
[Unit]
|
||||
Description=Gunicorn instance to serve task_center
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/var/www/task_center
|
||||
Environment="PATH=/var/www/task_center/venv/bin"
|
||||
ExecStart=/var/www/task_center/venv/bin/gunicorn --workers 3 --bind unix:/var/www/task_center/task_center.sock task_center.wsgi:application
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
### 3.3 启动和启用Gunicorn服务
|
||||
```bash
|
||||
sudo systemctl start task_center
|
||||
sudo systemctl enable task_center
|
||||
```
|
||||
|
||||
### 3.4 检查Gunicorn服务状态
|
||||
```bash
|
||||
sudo systemctl status task_center
|
||||
```
|
||||
|
||||
## 4. Nginx配置
|
||||
|
||||
### 4.1 创建Nginx配置文件
|
||||
```bash
|
||||
sudo nano /etc/nginx/sites-available/task_center
|
||||
```
|
||||
|
||||
配置内容:
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com www.your-domain.com;
|
||||
|
||||
location = /favicon.ico { access_log off; log_not_found off; }
|
||||
location /static/ {
|
||||
root /var/www/task_center;
|
||||
}
|
||||
|
||||
location /media/ {
|
||||
root /var/www/task_center;
|
||||
}
|
||||
|
||||
location / {
|
||||
include proxy_params;
|
||||
proxy_pass http://unix:/var/www/task_center/task_center.sock;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4.2 启用Nginx配置
|
||||
```bash
|
||||
sudo ln -s /etc/nginx/sites-available/task_center /etc/nginx/sites-enabled/
|
||||
```
|
||||
|
||||
### 4.3 测试Nginx配置
|
||||
```bash
|
||||
sudo nginx -t
|
||||
```
|
||||
|
||||
### 4.4 重启Nginx服务
|
||||
```bash
|
||||
sudo systemctl restart nginx
|
||||
```
|
||||
|
||||
### 4.5 配置防火墙
|
||||
```bash
|
||||
sudo ufw allow 'Nginx Full'
|
||||
sudo ufw allow ssh
|
||||
sudo ufw enable
|
||||
```
|
||||
|
||||
## 5. SSL证书配置(可选)
|
||||
|
||||
### 5.1 安装Certbot
|
||||
```bash
|
||||
sudo apt install -y certbot python3-certbot-nginx
|
||||
```
|
||||
|
||||
### 5.2 获取SSL证书
|
||||
```bash
|
||||
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
|
||||
```
|
||||
|
||||
### 5.3 自动更新证书
|
||||
```bash
|
||||
sudo systemctl status certbot.timer
|
||||
```
|
||||
|
||||
## 6. 配置媒体文件权限
|
||||
```bash
|
||||
sudo chown -R www-data:www-data /var/www/task_center/media
|
||||
sudo chmod -R 755 /var/www/task_center/media
|
||||
```
|
||||
|
||||
## 7. 配置定时任务
|
||||
|
||||
### 7.1 添加任务超时检查命令
|
||||
```bash
|
||||
sudo crontab -e
|
||||
```
|
||||
|
||||
添加以下内容(每小时执行一次):
|
||||
```
|
||||
0 * * * * cd /var/www/task_center && /var/www/task_center/venv/bin/python manage.py check_task_timeouts
|
||||
```
|
||||
|
||||
## 8. 监控和维护
|
||||
|
||||
### 8.1 查看Gunicorn日志
|
||||
```bash
|
||||
sudo journalctl -u task_center
|
||||
```
|
||||
|
||||
### 8.2 查看Nginx日志
|
||||
```bash
|
||||
sudo tail -f /var/log/nginx/access.log
|
||||
sudo tail -f /var/log/nginx/error.log
|
||||
```
|
||||
|
||||
### 8.3 重启服务
|
||||
```bash
|
||||
sudo systemctl restart task_center
|
||||
sudo systemctl restart nginx
|
||||
```
|
||||
|
||||
### 8.4 部署更新
|
||||
```bash
|
||||
cd /var/www/task_center
|
||||
git pull
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
python manage.py migrate
|
||||
python manage.py collectstatic --noinput
|
||||
deactivate
|
||||
sudo systemctl restart task_center
|
||||
sudo systemctl restart nginx
|
||||
```
|
||||
|
||||
## 9. 常见问题排查
|
||||
|
||||
### 9.1 502 Bad Gateway
|
||||
- 检查Gunicorn服务状态:`sudo systemctl status task_center`
|
||||
- 检查socket文件权限:`ls -l /var/www/task_center/task_center.sock`
|
||||
- 确保www-data用户有访问权限
|
||||
|
||||
### 9.2 静态文件404
|
||||
- 检查STATIC_ROOT配置
|
||||
- 确保执行了collectstatic命令
|
||||
- 检查Nginx静态文件路径配置
|
||||
|
||||
### 9.3 媒体文件上传失败
|
||||
- 检查媒体文件目录权限
|
||||
- 确保www-data用户有写入权限
|
||||
|
||||
## 10. 系统架构
|
||||
|
||||
```
|
||||
用户请求 → Nginx (80/443) → Gunicorn (Unix Socket) → Django应用
|
||||
↓
|
||||
静态文件 → Nginx直接服务
|
||||
↓
|
||||
媒体文件 → Nginx直接服务
|
||||
```
|
||||
|
||||
## 11. 性能优化建议
|
||||
|
||||
1. **Gunicorn工作进程**:根据CPU核心数调整workers数量,建议为 `CPU核心数 * 2 + 1`
|
||||
2. **数据库优化**:生产环境建议使用PostgreSQL
|
||||
3. **缓存配置**:使用Redis或Memcached缓存
|
||||
4. **文件存储**:使用云存储(如S3)存储媒体文件
|
||||
5. **监控系统**:添加Prometheus + Grafana监控
|
||||
6. **日志管理**:配置ELK Stack或其他日志管理系统
|
||||
|
||||
## 12. 安全建议
|
||||
|
||||
1. **定期更新系统和依赖**
|
||||
2. **使用强密码**
|
||||
3. **配置防火墙**
|
||||
4. **启用HTTPS**
|
||||
5. **限制SSH访问**
|
||||
6. **定期备份数据库**
|
||||
7. **使用最小权限原则**
|
||||
|
||||
## 访问地址
|
||||
|
||||
- **系统首页**:http://your-domain.com
|
||||
- **后台管理**:http://your-domain.com/admin/
|
||||
- **API文档**:http://your-domain.com/api/
|
||||
|
||||
## 总结
|
||||
|
||||
本部署方案已完成以下内容:
|
||||
1. 系统环境配置
|
||||
2. 项目部署和依赖安装
|
||||
3. Gunicorn服务配置和注册
|
||||
4. Nginx反向代理配置
|
||||
5. SSL证书配置(可选)
|
||||
6. 定时任务配置
|
||||
7. 监控和维护指南
|
||||
|
||||
任务中心管理系统已成功部署在Ubuntu服务器上,可以通过域名访问和使用。
|
||||
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Reference in New Issue
Block a user