在 Linux 服务器上部署企业官方网站是一个标准化但需兼顾安全、性能与可维护性的工程任务。以下是生产环境推荐的完整部署流程(以主流技术栈为例,兼顾稳健性与现代实践):
✅ 一、前期准备与规划
| 项目 | 建议 |
|---|---|
| 服务器选择 | 云服务器(如阿里云ECS、腾讯云CVM、AWS EC2)或物理机;推荐 Ubuntu 22.04 LTS / CentOS Stream 9(避免已停更的 CentOS 7/8) |
| 域名与SSL | 已备案域名(国内必需)、DNS 解析至服务器公网 IP;强制启用 HTTPS(Let’s Encrypt 免费证书) |
| 网站类型 | 静态官网(HTML/CSS/JS)→ 推荐 Nginx;动态官网(PHP/Node.js/Python)→ 按后端选型 |
💡 企业官网绝大多数为静态站点(Vue/React 打包后、Hugo/Jekyll 生成),优先考虑静态部署——更安全、更快、免运维后端。
✅ 二、推荐架构(静态官网示例)
用户浏览器 → CDN(可选)→ Nginx(HTTPS + 静态服务 + 缓存)→ 文件系统(/var/www/your-company.com)
✅ 优势:零后端依赖、高并发、防DDoS、易于备份与回滚
✅ 三、详细部署步骤(Ubuntu 22.04 + Nginx + Let’s Encrypt)
1️⃣ 更新系统 & 安装基础工具
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx curl wget git gnupg2 software-properties-common
sudo systemctl enable nginx
2️⃣ 配置域名与防火墙
# 开放必要端口
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full' # 同时开放 80 & 443
sudo ufw --force enable
# 确保域名已解析到本机IP(ping your-company.com 应返回服务器IP)
3️⃣ 部署网站文件
# 创建网站根目录(按域名隔离,便于多站管理)
sudo mkdir -p /var/www/your-company.com/html
sudo chown -R $USER:$USER /var/www/your-company.com/html
sudo chmod -R 755 /var/www/your-company.com
# 上传文件(任选其一):
# ▪ 方式1:本地打包后SCP上传(推荐)
scp -r ./dist/* user@your-server-ip:/var/www/your-company.com/html/
# ▪ 方式2:Git 部署(适合CI/CD)
git clone https://github.com/your-org/website.git /tmp/website
cp -r /tmp/website/dist/* /var/www/your-company.com/html/
rm -rf /tmp/website
4️⃣ 配置 Nginx 虚拟主机
创建配置文件:sudo nano /etc/nginx/sites-available/your-company.com
server {
listen 80;
server_name your-company.com www.your-company.com;
root /var/www/your-company.com/html;
index index.html;
location / {
try_files $uri $uri/ /index.html; # 支持 Vue Router history 模式
}
# 安全加固(关键!)
location ~ /.(htaccess|htpasswd|env|ini|log|sh|bak|swp)$ {
deny all;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg|woff2|ttf)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
启用站点:
sudo ln -sf /etc/nginx/sites-available/your-company.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
5️⃣ 配置 HTTPS(Let’s Encrypt + Certbot)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-company.com -d www.your-company.com
# ✅ 自动修改 Nginx 配置、申请证书、配置自动续期
🔐 Certbot 会自动添加 443 配置,并重定向 HTTP → HTTPS(301)
6️⃣ (可选)启用 CDN 提速(如 Cloudflare)
- 在 Cloudflare 添加域名,设置 DNS 为
Proxied(橙色云) - 务必关闭 Cloudflare 的“自动 HTTPS 重写”,避免与 Nginx 冲突
- 在 Nginx 中添加真实 IP 头支持(防止日志显示 Cloudflare IP):
set_real_ip_from 103.21.244.0/22; set_real_ip_from 103.22.200.0/22; # ...(填入 Cloudflare 所有 IP 段,见 https://www.cloudflare.com/ips/) real_ip_header CF-Connecting-IP;
7️⃣ 设置自动证书续期(Certbot 已默认配置 cron)
sudo systemctl list-timers | grep certbot # 验证定时任务存在
sudo certbot renew --dry-run # 测试续期是否正常
✅ 四、企业级增强建议(必做项)
| 类别 | 措施 | 命令/说明 |
|---|---|---|
| 安全加固 | 禁用服务器 Banner、限制 SSH 登录、配置 Fail2ban | sudo apt install fail2ban;禁用密码登录,改用 SSH 密钥 |
| 日志监控 | Nginx 访问日志 + 错误日志轮转 | 默认已启用 logrotate,检查 /etc/logrotate.d/nginx |
| 备份策略 | 每日自动备份网站文件 + SSL 证书 | tar -czf /backup/website-$(date +%F).tar.gz /var/www/your-company.com /etc/letsencrypt/live/your-company.com |
| 健康检查 | 添加简单健康接口(如 /healthz 返回 200) |
在 Nginx location /healthz { return 200 'OK'; } |
| CI/CD 集成 | GitHub Actions 自动构建 + 部署 | 使用 ssh-keyscan + rsync 或 scp 实现推送 |
✅ 五、常见问题排查
| 现象 | 快速诊断 |
|---|---|
| ❌ 打不开网站 | curl -I http://your-company.com → 检查状态码;sudo nginx -t;sudo journalctl -u nginx -n 50 --no-pager |
| ❌ HTTPS 不生效 | openssl s_client -connect your-company.com:443 -servername your-company.com 2>/dev/null | grep "Verify return code" |
| ❌ 页面 403 Forbidden | ls -l /var/www/your-company.com/html → 确认 www-data 用户有读取权限;检查 SELinux(若启用) |
| ❌ 域名跳转异常 | dig your-company.com 检查 DNS;curl -v http://your-company.com 查看重定向链 |
✅ 六、进阶选项(按需选用)
- 🌐 多语言支持:Nginx 根据
Accept-Language头重写路径(如/zh/,/en/) - 📱 PWA 支持:确保
manifest.json&service-worker.js正确部署,添加Cache-Control: no-cache到 SW 文件 - 📊 访问统计:集成 Plausible(轻量开源)或 Google Analytics(注意 GDPR 合规)
- 🛡️ WAF 防护:Cloudflare WAF 或 ModSecurity(Nginx 模块)
✅ 最后验证清单:
- [ ]
https://your-company.com可正常访问(无证书警告) - [ ]
http://your-company.com自动 301 跳转至 HTTPS - [ ] 所有静态资源(CSS/JS/图片)加载成功(F12 → Network)
- [ ] 移动端适配良好(Chrome DevTools → Toggle Device Toolbar)
- [ ] Lighthouse 评分 ≥ 90(性能、SEO、可访问性)
- [ ] SSL Labs 测试 A+(https://www.ssllabs.com/ssltest/)
需要我为你提供以下任意一项?我可以立即生成:
- 📜 完整的 Nginx 生产配置模板(含安全头、CSP、HSTS)
- 🐳 Docker Compose 部署方案(容器化静态站 + 自动 HTTPS)
- 🔄 GitHub Actions 自动部署脚本
- 🧩 WordPress / Next.js / Hugo 企业站专项部署指南
欢迎告诉我你的具体技术栈(如:Vue 打包、WordPress、还是自研 PHP 后台),我可定制化输出 👇
CLOUD技术博