如何在Linux服务器上部署静态企业官网?

在 Linux 服务器上部署静态企业官网,通常采用 Nginx + HTML/CSS/JS 的方案。以下是完整、安全的部署流程(以 Ubuntu 22.04 / CentOS 8+ 为例):


✅ 一、准备阶段

  1. 获取网站文件

    • 本地开发完成后的 index.htmlcss/js/images/ 等目录打包为 website.zip
    • 确保路径无中文/空格,权限正确(推荐 755 目录,644 文件)。
  2. 服务器要求

    • 已安装 Linux 系统(Ubuntu/CentOS/Rocky 等)
    • 开放 80(HTTP)和 443(HTTPS)端口(防火墙需放行)
    • 拥有 root 或 sudo 权限

✅ 二、安装 Web 服务器(推荐 Nginx)

▶ Ubuntu/Debian

sudo apt update
sudo apt install nginx -y
sudo systemctl enable --now nginx

▶ CentOS/RHEL

sudo dnf install epel-release -y
sudo dnf install nginx -y
sudo systemctl enable --now nginx

💡 验证安装:访问 http://<服务器IP>,应看到默认 Nginx 欢迎页。


✅ 三、上传并配置网站文件

1. 创建网站根目录

sudo mkdir -p /var/www/mycompany.com/html
sudo chown -R $USER:$USER /var/www/mycompany.com

2. 上传文件(示例:通过 SCP)

scp -r website.zip user@your-server:/tmp/
ssh user@your-server 'cd /tmp && unzip -o website.zip -d /var/www/mycompany.com/html'

🔐 建议后续将目录所有者改为 www-data(Ubuntu)或 nginx(CentOS):

# Ubuntu
sudo chown -R www-data:www-data /var/www/mycompany.com/html
# CentOS
sudo chown -R nginx:nginx /var/www/mycompany.com/html

3. 配置 Nginx 站点

创建虚拟主机配置:

sudo nano /etc/nginx/sites-available/mycompany.com

填入以下内容(按需修改):

server {
    listen 80;
    server_name mycompany.com www.mycompany.com;
    root /var/www/mycompany.com/html;
    index index.html index.htm;

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # 静态资源缓存(可选)
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # 强制 HTTPS(见下文)
    # return 301 https://$server_name$request_uri;

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}

启用站点:

# Ubuntu
sudo ln -s /etc/nginx/sites-available/mycompany.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default  # 禁用默认站

# CentOS(若使用 include 方式)
echo "include /etc/nginx/conf.d/*.conf;" >> /etc/nginx/nginx.conf
# 或将配置放入 /etc/nginx/conf.d/mycompany.com.conf

测试配置并重载:

sudo nginx -t
sudo systemctl reload nginx

✅ 四、配置 SSL(Let’s Encrypt 免费证书,强烈推荐)

1. 安装 Certbot

# Ubuntu
sudo apt install certbot python3-certbot-nginx -y
# CentOS
sudo dnf install certbot python3-certbot-nginx -y

2. 申请证书

sudo certbot --nginx -d mycompany.com -d www.mycompany.com

→ 按提示输入邮箱,同意条款,自动配置 HTTP→HTTPS 重定向及续期。

✅ 完成后:

  • 自动更新 /etc/nginx/sites-available/... 添加 listen 443 ssl
  • 设置自动续期(Certbot 已加入 cron/systemd timer)

📌 验证续期:sudo certbot renew --dry-run


✅ 五、防火墙与安全加固

1. 防火墙规则(UFW / firewalld)

# UFW (Ubuntu)
sudo ufw allow 'Nginx Full'
sudo ufw enable

# Firewalld (CentOS)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

2. 其他建议

  • 隐藏 Nginx 版本:server_tokens off;
  • 禁止目录浏览:autoindex off;
  • 定期更新系统 & Nginx:sudo apt upgrade / sudo dnf update

✅ 六、验证上线

  1. 浏览器访问:https://mycompany.com
  2. 检查:
    • 页面正常加载
    • 地址栏有锁图标(HTTPS)
    • 移动端适配良好
    • 控制台无 404/错误

🔁 后续维护建议

任务 命令/工具
更新内容 scp 上传新文件 → rsync 增量同步更优
日志查看 tail -f /var/log/nginx/access.log
性能监控 htop, nginx -V, 或集成 Prometheus/Grafana
备份 tar czf backup-$(date +%F).tar.gz /var/www/mycompany.com

需要我提供:

  • Docker 化部署方案?
  • CI/CD 自动化发布(GitHub Actions + SSH)?
  • 多语言/多域名支持配置?
  • CDN 提速集成指南?

欢迎告诉我你的具体需求,我可进一步定制方案! 🚀

未经允许不得转载:CLOUD技术博 » 如何在Linux服务器上部署静态企业官网?