VPS 部署全记录:从裸机到 HTTPS
目录
第一次用 VPS 部署网站,踩了不少坑,把关键步骤整理出来。
基础环境
# Ubuntu 22.04 为例
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx python3 python3-venv git
Nginx 反向代理
让 Nginx 监听 80 端口,把动态请求转发给本地服务(比如 Gunicorn 起的 5000 端口):
server {
listen 80;
server_name your-domain.com;
# 静态文件直接服务
location / {
root /var/www/blog/public;
try_files $uri $uri/ =404;
}
# 动态接口转发
location /api/ {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
免费 HTTPS
用 Let’s Encrypt 五分钟搞定:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
证书到期前 certbot 会自动续期,一劳永逸。
几个坑
- 改完配置一定要
nginx -t检查语法再 reload - 防火墙记得放行 80/443(
ufw allow 'Nginx Full') - 反向代理时务必转发
X-Real-IP和X-Forwarded-For,否则后端拿不到访客真实 IP