哈哈
This commit is contained in:
99
README.md
99
README.md
@@ -18,7 +18,7 @@ pip install -r requirements.txt
|
||||
# 首次运行:初始化数据库并导入示例数据
|
||||
python init_db.py
|
||||
|
||||
# 启动服务
|
||||
# 开发时直接跑
|
||||
python app.py
|
||||
# 或
|
||||
python main.py
|
||||
@@ -27,6 +27,59 @@ python main.py
|
||||
- 前台:<http://127.0.0.1:5001>
|
||||
- 后台:<http://127.0.0.1:5001/admin>(默认密码见下方环境变量)
|
||||
|
||||
### 生产环境:Gunicorn 后台常驻(Ubuntu systemd)
|
||||
|
||||
让 gunicorn 一直在后台运行,断线自启、开机自启:
|
||||
|
||||
**1. 安装 gunicorn**
|
||||
```bash
|
||||
cd /opt/vps_price # 或你的项目目录
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
pip install gunicorn
|
||||
```
|
||||
|
||||
**2. 创建 systemd 服务**
|
||||
```bash
|
||||
sudo nano /etc/systemd/system/vps_price.service
|
||||
```
|
||||
|
||||
粘贴下面内容,**把 `/opt/vps_price` 和 `/opt/vps_price/venv` 改成你实际的项目路径**(若用 root 跑可把 `User=www-data` 改为 `User=root`):
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
Description=VPS Price Gunicorn
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=notify
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/vps_price
|
||||
Environment="PATH=/opt/vps_price/venv/bin"
|
||||
ExecStart=/opt/vps_price/venv/bin/gunicorn -w 4 -b 127.0.0.1:5001 app:app
|
||||
ExecReload=/bin/kill -s HUP $MAINPID
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
**3. 启用并启动**
|
||||
```bash
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable vps_price
|
||||
sudo systemctl start vps_price
|
||||
sudo systemctl status vps_price
|
||||
```
|
||||
|
||||
之后 gunicorn 会一直后台运行,重启服务器也会自动起来。常用命令:
|
||||
- 查看状态:`sudo systemctl status vps_price`
|
||||
- 重启:`sudo systemctl restart vps_price`
|
||||
- 看日志:`journalctl -u vps_price -f`
|
||||
|
||||
## 环境变量(可选)
|
||||
|
||||
| 变量 | 说明 | 默认 |
|
||||
@@ -45,9 +98,26 @@ python main.py
|
||||
|
||||
当前默认站点域名:`https://vps.ddrwode.cn`(可通过环境变量 `SITE_URL` 覆盖)。
|
||||
|
||||
### 用域名访问(Nginx 反向代理示例)
|
||||
### Ubuntu:Nginx + HTTPS(Let's Encrypt)
|
||||
|
||||
在服务器上用 Nginx 把域名 `vps.ddrwode.cn` 指到本服务(例如本机 5001 端口):
|
||||
在 **Ubuntu 服务器**上按顺序执行即可为 `vps.ddrwode.cn` 启用 HTTPS。
|
||||
|
||||
**1. 安装 Nginx 和 Certbot**
|
||||
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install nginx certbot python3-certbot-nginx -y
|
||||
sudo systemctl enable nginx
|
||||
sudo systemctl start nginx
|
||||
```
|
||||
|
||||
**2. 先写 Nginx 配置(只开 80,方便 Certbot 验证域名)**
|
||||
|
||||
```bash
|
||||
sudo nano /etc/nginx/sites-available/vps.ddrwode.cn
|
||||
```
|
||||
|
||||
写入(确保本机 Flask/gunicorn 已监听 5001 端口):
|
||||
|
||||
```nginx
|
||||
server {
|
||||
@@ -63,7 +133,28 @@ server {
|
||||
}
|
||||
```
|
||||
|
||||
若启用 HTTPS(推荐),可用 certbot 申请证书并配置 `listen 443 ssl`,同时保证 `SITE_URL` 为 `https://vps.ddrwode.cn`。
|
||||
保存后启用并检查、重载:
|
||||
|
||||
```bash
|
||||
sudo ln -sf /etc/nginx/sites-available/vps.ddrwode.cn /etc/nginx/sites-enabled/
|
||||
sudo nginx -t
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
**3. 申请 HTTPS 证书(自动改 Nginx 为 443)**
|
||||
|
||||
```bash
|
||||
sudo certbot --nginx -d vps.ddrwode.cn
|
||||
```
|
||||
|
||||
按提示输入邮箱、同意条款;选「重定向 HTTP 到 HTTPS」即可。Certbot 会自动加上 443 和证书路径。
|
||||
|
||||
**4. 确认访问**
|
||||
|
||||
- 打开:`https://vps.ddrwode.cn`
|
||||
- 证书会自动续期(`certbot renew`),可加定时任务:`sudo certbot renew --dry-run`
|
||||
|
||||
项目根目录下的 `deploy/nginx-vps.ddrwode.cn.conf` 为上述配置的参考副本,便于你复制到服务器。
|
||||
|
||||
## 接入 Google 广告
|
||||
|
||||
|
||||
15
deploy/nginx-vps.ddrwode.cn.conf
Normal file
15
deploy/nginx-vps.ddrwode.cn.conf
Normal file
@@ -0,0 +1,15 @@
|
||||
# 复制到服务器: /etc/nginx/sites-available/vps.ddrwode.cn
|
||||
# 然后: sudo ln -sf /etc/nginx/sites-available/vps.ddrwode.cn /etc/nginx/sites-enabled/
|
||||
# 先只保留下面这个 server(80),执行 certbot --nginx -d vps.ddrwode.cn 后会自动添加 443 和证书
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name vps.ddrwode.cn;
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:5001;
|
||||
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;
|
||||
}
|
||||
}
|
||||
25
deploy/vps_price.service
Normal file
25
deploy/vps_price.service
Normal file
@@ -0,0 +1,25 @@
|
||||
# Gunicorn 后台常驻:复制到 /etc/systemd/system/vps_price.service
|
||||
# 注意:把 /opt/vps_price 和 /opt/vps_price/venv 改成你服务器上的实际路径
|
||||
#
|
||||
# sudo systemctl daemon-reload
|
||||
# sudo systemctl enable vps_price
|
||||
# sudo systemctl start vps_price
|
||||
# sudo systemctl status vps_price
|
||||
|
||||
[Unit]
|
||||
Description=VPS Price Gunicorn
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=notify
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/vps_price
|
||||
Environment="PATH=/opt/vps_price/venv/bin"
|
||||
ExecStart=/opt/vps_price/venv/bin/gunicorn -w 4 -b 127.0.0.1:5001 app:app
|
||||
ExecReload=/bin/kill -s HUP $MAINPID
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user