使用 Nginx 部署模板站点并配置 HTTPS
如果你有云服务器(如阿里云 ECS / 腾讯云 CVM),推荐使用 Nginx 部署。
一、安装 Nginx
Ubuntu / Debian
sudo apt update
sudo apt install nginx
CentOS / RHEL
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
二、上传模板文件
# 创建站点目录
sudo mkdir -p /www/wwwroot/你的域名
# 上传文件(用 scp 或 FTP)
scp -r 本地模板目录/* root@服务器IP:/www/wwwroot/你的域名/
三、配置 Nginx
创建配置文件 /etc/nginx/conf.d/你的域名.conf:
server {
listen 80;
server_name 你的域名 www.你的域名;
root /www/wwwroot/你的域名;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
# gzip 压缩
gzip on;
gzip_types text/css application/javascript text/javascript;
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
重载配置:
sudo nginx -t # 检查语法
sudo systemctl reload nginx # 重载
四、配置 HTTPS(Let's Encrypt)
安装 Certbot
sudo apt install certbot python3-certbot-nginx
申请证书
sudo certbot --nginx -d 你的域名 -d www.你的域名
按提示操作,Certbot 会自动修改 Nginx 配置并开启 HTTPS。
自动续期
sudo crontab -e
# 添加定时任务
0 3 * * * certbot renew --quiet
五、常见问题
端口被占用?
sudo lsof -i :80 # 查看占用进程
sudo kill -9 PID # 结束进程
Nginx 配置不生效?
- 确认 nginx -t 语法检查通过
- 确认执行了 systemctl reload nginx
- 检查是否有其他 .conf 文件冲突