走啊走
加油

如何在Linux服务器上部署企业官方网站?

服务器价格表

在 Linux 服务器上部署企业官方网站是一个系统工程,涉及服务器准备、环境搭建、代码部署、安全配置和运维监控等多个环节。以下是标准且安全的部署流程(以 Nginx + PHP/Node.js/静态网站为例):


一、前期准备

  1. 选择操作系统

    • 推荐:Ubuntu 22.04 LTS / CentOS Stream 9 / Rocky Linux 9(长期支持版)
    • 执行系统更新:
      sudo apt update && sudo apt upgrade -y          # Ubuntu/Debian
      sudo dnf update -y                              # RHEL/CentOS/Rocky
  2. 购买并配置域名与 DNS

    • 将域名 A 记录指向服务器公网 IP(如 example.com203.0.113.10
    • 建议启用 HTTPS(后续步骤会配置 Let’s Encrypt 证书)
  3. 创建专用用户与权限控制

    sudo adduser webadmin
    sudo usermod -aG sudo webadmin
    # 禁用 root 远程登录(SSH 中设置 PermitRootLogin no)

二、安装基础服务栈(以 LAMP/LNMP 为例)

方案 A:静态网站(HTML/CSS/JS)+ Nginx

# 安装 Nginx
sudo apt install nginx -y        # Ubuntu
sudo dnf install nginx -y        # CentOS/Rocky

# 启动并设置开机自启
sudo systemctl enable --now nginx

# 配置虚拟主机(示例:/etc/nginx/sites-available/example.com)
sudo nano /etc/nginx/sites-available/example.com

配置内容关键片段:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/html/example.com/public;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # 强制 HTTPS(配合 SSL 后生效)
    # return 301 https://$host$request_uri;
}

启用站点:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

方案 B:动态网站(PHP + MySQL)→ LNMP

# Ubuntu 示例
sudo apt install nginx php-fpm mysql-server php-mysql -y

# 编辑 Nginx 配置,添加 PHP 处理
location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

💡 若使用 Node.js/Python/Django 等,需安装对应运行时(如 nodejs, python3, pip),并通过 PM2/Gunicorn 管理进程。


三、部署网站代码

  • 方式 1:Git 拉取(推荐)

    cd /var/www/html
    sudo mkdir -p example.com && cd example.com
    sudo git clone https://github.com/your-org/site.git .
    sudo chown -R www-data:www-data .  # 根据实际用户调整
  • 方式 2:上传压缩包

    scp site.zip webadmin@your-server:/tmp/
    ssh webadmin@your-server 'cd /var/www/html && unzip /tmp/site.zip'
  • 构建前端项目(如需)

    cd /var/www/html/example.com
    npm ci && npm run build   # 生成 dist/ 或 build/
    sudo mv dist/* public/    # 移动至 Nginx root 目录

四、配置 HTTPS(免费证书)

使用 Certbot 自动申请 Let’s Encrypt 证书:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

✅ 自动完成:

  • 配置 HTTP→HTTPS 重定向
  • 设置自动续期(cron 任务已内置)
  • 验证防火墙规则(确保 443 端口开放)

检查续期:

sudo certbot renew --dry-run

五、安全加固

措施 操作
防火墙 ufw allow 'Nginx Full' + ufw enable
SSH 安全 禁用密码登录,改用密钥认证;修改默认端口(可选)
文件权限 find /var/www -type f -exec chmod 644 {} ; ; find /var/www -type d -exec chmod 755 {} ;
防暴力破解 安装 fail2bansudo apt install fail2ban -y
定期备份 编写脚本备份数据库 + 网站文件到远程存储(如 AWS S3)

六、监控与运维

  • 日志分析
    tail -f /var/log/nginx/access.log
    grep "error" /var/log/nginx/error.log
  • 健康检查脚本(可集成到 Prometheus/Nagios):
    curl -I http://localhost | grep "200 OK"
  • 自动部署(CI/CD)
    结合 GitHub Actions / GitLab CI 实现:

    # .github/workflows/deploy.yml
    on: push
    jobs:
    deploy:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v4
        - name: Deploy via SSH
          uses: appleboy/ssh-action@master
          with:
            host: ${{ secrets.HOST }}
            username: webadmin
            key: ${{ secrets.SSH_KEY }}
            script: |
              cd /var/www/html/example.com
              git pull origin main
              npm ci && npm run build
              sudo systemctl restart nginx

七、常见问题排查

问题 解决方案
403 Forbidden 检查 root 路径权限、SELinux(setenforce 0 测试)、Nginx try_files 配置
502 Bad Gateway PHP-FPM/Nginx 未联动?检查 fastcgi_pass 地址;重启 php-fpm
证书无效 确认域名 DNS 解析正确;检查 certbot 续期时间
访问慢 开启 Gzip 压缩、启用 CDN(如 Cloudflare)、优化图片资源

需要我根据您的具体技术栈(如 WordPress、Django、React SSR、Go 微服务等)提供定制化部署方案吗?或者需要自动化脚本模板(Ansible/Terraform)?