From be8e48225b713c56b95bb549cf0534fe2647ac10 Mon Sep 17 00:00:00 2001 From: xiaji Date: Sun, 4 Jan 2026 16:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E7=BD=B2=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 238 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 216 insertions(+), 22 deletions(-) diff --git a/readme.md b/readme.md index 4b32803..584a1a5 100644 --- a/readme.md +++ b/readme.md @@ -43,67 +43,261 @@ ## 安装部署 -### 1. 环境准备 +### 1. 环境准备(Ubuntu 22.04/24.04) ```bash -# 克隆项目 -cd c:\Users\xiaji\Documents\个人文件夹\夏骥\状态页面 +# 更新系统包 +sudo apt update && sudo apt upgrade -y + +# 安装Python3和pip +sudo apt install python3 python3-pip python3-venv python3-dev -y + +# 安装Git +sudo apt install git -y + +# 克隆项目(示例路径,根据实际情况调整) +cd /opt +sudo git clone https://your-repo-url/statuspage.git +cd statuspage + +# 创建虚拟环境 +sudo python3 -m venv venv + +# 激活虚拟环境 +source venv/bin/activate # 安装依赖 +pip install --upgrade pip pip install -r requirements.txt ``` ### 2. 数据库迁移 ```bash -python manage.py migrate +# 激活虚拟环境(如果未激活) +source venv/bin/activate + +# 执行数据库迁移(使用自定义settings文件) +python manage.py migrate --settings=statuspage.生产系统settings + +# 创建超级用户(用于后台管理) +python manage.py createsuperuser --settings=statuspage.生产系统settings ``` -### 3. 启动开发服务器 +### 3. 收集静态文件 ```bash -python manage.py runserver +# 激活虚拟环境(如果未激活) +source venv/bin/activate + +# 收集静态文件(使用自定义settings文件) +python manage.py collectstatic --noinput --settings=statuspage.生产系统settings ``` -访问地址:http://localhost:8000 - -### 4. 生产环境部署 - -#### 使用Gunicorn和Nginx +### 4. 开发服务器(仅用于测试) ```bash +# 激活虚拟环境(如果未激活) +source venv/bin/activate + +# 启动开发服务器(仅本地访问,使用自定义settings文件) +python manage.py runserver --settings=statuspage.生产系统settings + +# 或允许外部访问(开发环境) +python manage.py runserver 0.0.0.0:8000 --settings=statuspage.生产系统settings +``` + +访问地址:http://your-server-ip:8000 + +### 5. 生产环境部署(Ubuntu) + +#### 5.1 安装Gunicorn和Nginx + +```bash +# 激活虚拟环境 +source venv/bin/activate + # 安装Gunicorn pip install gunicorn -# 启动Gunicorn -python -m gunicorn --bind 0.0.0.0:8000 statuspage.wsgi:application +# 退出虚拟环境 +deactivate + +# 安装Nginx +sudo apt install nginx -y ``` -Nginx配置示例: +#### 5.2 创建Gunicorn系统服务 + +```bash +# 创建服务文件 +sudo nano /etc/systemd/system/statuspage.service +``` + +在文件中添加以下内容: + +```ini +[Unit] +Description=Gunicorn instance to serve statuspage +After=network.target + +[Service] +User=www-data +Group=www-data +WorkingDirectory=/opt/statuspage +Environment="PATH=/opt/statuspage/venv/bin" +Environment="DJANGO_SETTINGS_MODULE=statuspage.生产系统settings" +ExecStart=/opt/statuspage/venv/bin/gunicorn --workers 3 --bind unix:/opt/statuspage/statuspage.sock statuspage.wsgi:application + +[Install] +WantedBy=multi-user.target +``` + +启动并启用服务: + +```bash +# 重新加载systemd +sudo systemctl daemon-reload + +# 启动服务 +sudo systemctl start statuspage + +# 开机自启 +sudo systemctl enable statuspage + +# 查看服务状态 +sudo systemctl status statuspage +``` + +#### 5.3 配置Nginx + +```bash +# 创建Nginx配置文件 +sudo nano /etc/nginx/sites-available/statuspage +``` + +添加以下配置: ```nginx server { listen 80; - server_name your-domain.com; + server_name your-domain.com www.your-domain.com; + charset utf-8; 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; + include proxy_params; + proxy_pass http://unix:/opt/statuspage/statuspage.sock; } location /static/ { - alias /path/to/your/project/static/; + alias /opt/statuspage/static/; + expires 30d; } location /media/ { - alias /path/to/your/project/media/; + alias /opt/statuspage/media/; + expires 30d; + } + + # 错误页面配置 + error_page 404 /404.html; + error_page 500 502 503 504 /500.html; + location = /500.html { + root /opt/statuspage/static; } } ``` +启用站点并重启Nginx: + +```bash +# 启用站点 +sudo ln -s /etc/nginx/sites-available/statuspage /etc/nginx/sites-enabled/ + +# 测试Nginx配置 +sudo nginx -t + +# 重启Nginx +sudo systemctl restart nginx +``` + +#### 5.4 配置防火墙 + +```bash +# 允许SSH、HTTP和HTTPS流量 +sudo ufw allow ssh +sudo ufw allow 'Nginx Full' + +# 启用防火墙 +sudo ufw enable + +# 查看防火墙状态 +sudo ufw status +``` + +#### 5.5 配置HTTPS(推荐) + +使用Certbot获取免费SSL证书: + +```bash +# 安装Certbot +sudo apt install certbot python3-certbot-nginx -y + +# 获取并配置SSL证书 +sudo certbot --nginx -d your-domain.com -d www.your-domain.com + +# 配置自动续约 +sudo certbot renew --dry-run +``` + +## 6. 部署验证 + +访问以下地址验证部署: +- Web界面:http://your-domain.com 或 https://your-domain.com +- Django后台:http://your-domain.com/admin +- API文档:http://your-domain.com/api/docs/ + +## 7. 生产环境配置注意事项 + +在生产环境部署前,务必修改以下配置项: + +### 7.1 修改配置文件 + +编辑 `statuspage/生产系统settings.py` 文件,修改以下关键配置: + +```python +# 1. 关闭DEBUG模式 +DEBUG = False + +# 2. 设置允许的主机名或IP地址 +ALLOWED_HOSTS = ['your-domain.com', 'www.your-domain.com', 'your-server-ip'] + +# 3. 生成新的SECRET_KEY +# 可以使用Django提供的密钥生成工具: +# python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())" +SECRET_KEY = 'your-new-secret-key-here' + +# 4. 可选:配置更安全的数据库(如PostgreSQL或MySQL) +# DATABASES = { +# 'default': { +# 'ENGINE': 'django.db.backends.postgresql_psycopg2', +# 'NAME': 'statuspage', +# 'USER': 'dbuser', +# 'PASSWORD': 'dbpassword', +# 'HOST': 'localhost', +# 'PORT': '5432', +# } +# } +``` + +### 7.2 重启服务 + +修改配置后,需要重启Gunicorn服务: + +```bash +sudo systemctl restart statuspage +``` + ## 项目结构 ```