跳转至

20- Web 服务配置(Apache2 / Nginx) 🌐

前提条件 ✅

  • 已完成第三阶段存储与日志配置(/data 可用于 Web 站点目录)
  • 以 ops 用户登录(具有 sudo 权限)
  • 系统运行在 Debian 12 Bookworm,防火墙已放行 80/tcp 和 443/tcp
  • 虚拟机环境:VMware Workstation Pro(建议从宿主机浏览器访问测试)
  • 全局约定:主机名 LinuxDC,IP 192.168.1.100,SSH 端口 2222

详细步骤 🛠️

  1. 选择 Web 服务器
  2. Apache2:模块化强、.htaccess 支持好、配置直观(适合传统 PHP/WordPress 站点)
  3. Nginx:高并发、低内存、反向代理强(推荐生产高负载、静态资源、容器环境)

本章并行演示两者,生产环境推荐 Nginx + Apache 作为后端(混合架构)。

  1. 安装 Apache2
    sudo apt update
    sudo apt install -y apache2 apache2-utils
    

启动并启用:

sudo systemctl enable --now apache2
sudo systemctl status apache2

默认站点测试: - 浏览器访问 http://192.168.1.100
- 应看到 “Apache2 Debian Default Page”

防火墙放行:

sudo ufw allow 'Apache'
sudo ufw reload

  1. Apache2 虚拟主机配置(多站点)
    创建站点目录:
    sudo mkdir -p /var/www/site1 /var/www/site2
    echo "<h1>Site 1 - LinuxDC</h1>" | sudo tee /var/www/site1/index.html
    echo "<h1>Site 2 - Test</h1>" | sudo tee /var/www/site2/index.html
    sudo chown -R www-data:www-data /var/www
    

创建虚拟主机配置文件:

sudo nano /etc/apache2/sites-available/site1.conf

内容:

<VirtualHost *:80>
    ServerName site1.local
    ServerAlias www.site1.local
    DocumentRoot /var/www/site1

    <Directory /var/www/site1>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site1_error.log
    CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>

类似创建 site2.conf(修改 ServerName/DocumentRoot)

启用站点:

sudo a2ensite site1.conf
sudo a2ensite site2.conf
sudo a2dissite 000-default.conf   # 禁用默认站点
sudo apache2ctl configtest        # 测试配置语法
sudo systemctl reload apache2

  1. 安装 Nginx
    sudo apt install -y nginx
    sudo systemctl enable --now nginx
    sudo systemctl status nginx
    

默认站点测试:http://192.168.1.100 → “Welcome to nginx!”

防火墙放行:

sudo ufw allow 'Nginx Full'
sudo ufw reload

  1. Nginx 虚拟主机配置
    创建站点目录:
    sudo mkdir -p /var/www/site1 /var/www/site2
    echo "<h1>Nginx Site 1</h1>" | sudo tee /var/www/site1/index.html
    

创建配置文件:

sudo nano /etc/nginx/sites-available/site1

内容:

server {
    listen 80;
    server_name site1.local www.site1.local;

    root /var/www/site1;
    index index.html index.htm;

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

    access_log /var/log/nginx/site1.access.log;
    error_log /var/log/nginx/site1.error.log;
}

启用:

sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
sudo nginx -t                  # 测试配置
sudo systemctl reload nginx

  1. HTTPS 配置(Let's Encrypt 免费证书)
    安装 certbot:
    sudo apt install -y certbot python3-certbot-apache   # 或 python3-certbot-nginx
    

获取证书(Apache 示例):

sudo certbot --apache -d site1.local -d www.site1.local

Nginx 同理:

sudo certbot --nginx -d site1.local

自动续期已默认加入 cron。

  1. 性能调优建议
  2. Apache:启用 mod_deflate、mod_expires、MPM event/worker
  3. Nginx:worker_processes auto、worker_connections 1024、gzip on

实践任务 🎯

  1. 安装 Apache2,创建两个虚拟主机 site1/site2,浏览器访问确认显示不同页面
  2. 安装 Nginx,创建 site1 虚拟主机,确认独立运行
  3. 用 certbot 为一个站点申请 Let's Encrypt 证书,访问 https://192.168.1.100 确认安全锁图标
  4. 查看 /var/log/apache2 或 /var/log/nginx 日志,找出最近访问记录

自测问题 ❓

  1. Apache2 的 a2ensite 和 a2dissite 命令的作用是什么?
  2. Nginx 配置中 try_files $uri $uri/ =404 的含义是什么?
  3. 为什么生产环境优先使用 HTTPS 而不是 HTTP?
  4. certbot 自动续期默认通过什么方式实现?

总结 📌

Apache2 和 Nginx 是 Debian 上两大主流 Web 服务器。
Apache2 配置友好、模块丰富;Nginx 高性能、适合反向代理与静态服务。
本章建立的虚拟主机 + HTTPS 基础,将在后续数据库、邮件、容器章节中作为前端服务承载业务。