在 Debian 上部署 Terebi 服务(含 systemd + Nginx + SSL)

1. 基础环境准备

更新系统:

sudo apt update && sudo apt upgrade -y

安装依赖:

sudo apt install -y nginx python3 python3-venv python3-pip certbot python3-certbot-nginx



2. 部署应用代码

假设代码放在:

/home/ubuntu/www/terebi

进入目录:

cd ~/www/terebi

如果用 conda 环境(比如 terebi),先激活:

conda activate terebi

运行测试:

python app.py 8001
# 或
python -m http.server 8001

确认能在服务器访问:

curl http://127.0.0.1:8001



3. 创建 systemd 服务

app 服务

/etc/systemd/system/terebi.service

[Unit]
Description=Terebi Web App
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/www/terebi
ExecStart=/home/ubuntu/miniconda3/envs/terebi/bin/python app.py 8001
Restart=always

[Install]
WantedBy=multi-user.target

scheduler 服务

/etc/systemd/system/terebi_scheduler.service

[Unit]
Description=Terebi Scheduler
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/www/terebi/tools
ExecStart=/home/ubuntu/miniconda3/envs/terebi/bin/python scheduler.py
Restart=always

[Install]
WantedBy=multi-user.target

加载并启用:

sudo systemctl daemon-reload
sudo systemctl enable --now terebi terebi_scheduler

检查:

sudo systemctl status terebi
sudo systemctl status terebi_scheduler



4. 配置 Nginx 反向代理

/etc/nginx/conf.d/terebi.iamcheyan.com.conf

server {
    listen 80;
    server_name terebi.iamcheyan.com;

    location / {
        proxy_pass http://127.0.0.1:8001;
        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;
    }
}

测试并重启:

sudo nginx -t
sudo systemctl reload nginx



5. 配置 HTTPS (SSL)

使用 Certbot 自动申请证书:

sudo certbot --nginx -d terebi.iamcheyan.com

Certbot 会自动:
	•	申请并安装证书到 /etc/letsencrypt/live/terebi.iamcheyan.com/
	•	在 Nginx 中添加 443 端口配置
	•	配置自动续期

测试访问:

https://terebi.iamcheyan.com/

强制跳转 HTTP → HTTPS:
Certbot 会提示是否启用,选择 Yes。


6. 自动续期

证书默认 90 天过期,Certbot 已经配置自动续期。你可以手动测试:

sudo certbot renew --dry-run



7. 常见遗漏点
	•	防火墙放行端口

sudo ufw allow 80
sudo ufw allow 443
sudo ufw reload


	•	日志查看

journalctl -u terebi -f
journalctl -u terebi_scheduler -f


	•	进程残留问题
如果换过 Nginx(宝塔/系统),确保只保留一个。
检查端口占用:

sudo ss -tulnp | grep -E ':80|:443'


	•	重启后自动运行
只要 systemctl enable 过,app.py 和 scheduler.py 会随系统启动。


🎉 部署完成

现在你可以通过:

https://terebi.iamcheyan.com/

安全访问你的服务了。