Files
3d_contorl/mqtt_web/DEPLOY.md

207 lines
3.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 3D 打印控制面板公网部署方案
## 一、服务器准备
### 1.1 云服务器推荐
| 厂商 | 推荐配置 | 价格约 |
|------|---------|--------|
| 腾讯云轻量应用服务器 | 2核2G 50G SSD | 60/月 |
| 阿里云轻量应用服务器 | 2核2G 40G SSD | 60/月 |
| DigitalOcean | 2核2G 50G SSD | $10/月 |
地区选择:**国内**(用户访问快,需备案)或 **香港/海外**(免备案)
### 1.2 域名(可选)
- 国内服务器需备案后才能使用域名
- 香港/海外服务器可直接使用域名
- 推荐在腾讯云/阿里云购买域名(.com 约 60/年)
---
## 二、服务器环境配置
### 2.1 安装 Python 3
```bash
# Ubuntu/Debian
sudo apt update
sudo apt install -y python3 python3-pip python3-venv
# 检查版本
python3 --version
```
### 2.2 安装 Nginx
```bash
# Ubuntu/Debian
sudo apt install -y nginx
```
### 2.3 配置防火墙
```bash
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw enable
```
---
## 三、部署应用
### 3.1 上传代码
```bash
# 在服务器上
cd /var/www
sudo mkdir mqtt-web
sudo chown $USER mqtt-web
cd mqtt-web
# 上传 mqtt_web 目录内容(可通过 git 或 scp
```
### 3.2 创建虚拟环境
```bash
cd /var/www/mqtt-web
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
### 3.3 测试运行
```bash
# 测试能否正常启动
python3 app.py
# 访问 http://服务器IP:3000 测试
```
---
## 四、Nginx 配置(推荐)
### 4.1 创建配置文件
```bash
sudo nano /etc/nginx/sites-available/mqtt-web
```
### 4.2 写入配置HTTP
```nginx
server {
listen 80;
server_name your-domain.com; # 替换为你的域名
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket 支持(备用)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
### 4.3 启用配置
```bash
sudo ln -s /etc/nginx/sites-available/mqtt-web /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```
---
## 五、SSL 证书HTTPS
### 5.1 安装 Certbot
```bash
sudo apt install -y certbot python3-certbot-nginx
```
### 5.2 获取免费证书
```bash
sudo certbot --nginx -d your-domain.com
```
按提示输入邮箱,完成后自动配置 HTTPS
---
## 六、Systemd 服务(开机自启)
### 6.1 创建服务文件
```bash
sudo nano /etc/systemd/system/mqtt-web.service
```
### 6.2 写入内容
```ini
[Unit]
Description=MQTT Web Control Panel
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/mqtt-web
Environment="PATH=/var/www/mqtt-web/venv/bin"
ExecStart=/var/www/mqtt-web/venv/bin/gunicorn -w 2 -b 127.0.0.1:3000 app:app
Restart=always
[Install]
WantedBy=multi-user.target
```
### 6.3 启用服务
```bash
sudo systemctl daemon-reload
sudo systemctl enable mqtt-web
sudo systemctl start mqtt-web
```
---
## 七、访问方式
- **有域名**https://your-domain.com
- **无域名**http://服务器IP
---
## 八、常见问题
### Q1: 端口被占用
```bash
sudo lsof -i :3000
sudo kill -9 <PID>
```
### Q2: 查看日志
```bash
sudo journalctl -u mqtt-web -f
```
### Q3: 更新代码
```bash
sudo systemctl stop mqtt-web
# 更新代码...
sudo systemctl start mqtt-web
```
---
## 九、架构图
```
用户浏览器
↓ HTTPS
Nginx (80/443)
↓ Proxy
Gunicorn (3000端口)
Python Flask + MQTT
MQTT Broker (mqtt.beihong.wang)
STM32
```