温馨提示×

Ubuntu Apache2多站点管理方法

小樊
38
2025-12-07 03:30:00
栏目: 智能运维

Ubuntu Apache2多站点管理方法

一 核心概念与准备

  • 使用 Apache 虚拟主机(VirtualHost) 在同一台服务器上托管多个域名或端口的站点,每个站点拥有独立的 ServerName/ServerAliasDocumentRoot
  • 准备:安装 Apache、创建站点目录、设置目录属主为 www-data:www-data,并按需启用模块(如 mod_rewrite、mod_ssl)。

二 标准配置步骤

  • 安装与启用模块
    • 安装:sudo apt update && sudo apt install apache2
    • 启用常用模块:sudo a2enmod rewrite、如需 HTTPS 则 sudo a2enmod ssl
  • 创建站点目录与示例首页
    • 目录:sudo mkdir -p /var/www/site1 /var/www/site2
    • 属主:sudo chown -R www-data:www-data /var/www/site1 /var/www/site2
    • 首页:分别在 /var/www/site1/index.html/var/www/site2/index.html 写入区分内容
  • 新建站点配置(/etc/apache2/sites-available/*.conf)
    • 示例 site1.conf:
      <VirtualHost *:80>
          ServerAdmin webmaster@site1.com
          ServerName site1.com
          ServerAlias www.site1.com
          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>
      
    • 将 site1 替换为 site2,修改 ServerName/ServerAlias/DocumentRoot/日志文件名 即可
  • 启用站点与禁用默认站点
    • 启用:sudo a2ensite site1.confsudo a2ensite site2.conf
    • 禁用默认:sudo a2dissite 000-default.conf
  • 使配置生效
    • 检查语法:sudo apache2ctl configtest
    • 热加载:sudo systemctl reload apache2(或重启:sudo systemctl restart apache2
  • DNS 解析
    • site1.com、site2.com(及 www 子域)的 A/AAAA 记录指向服务器公网 IP

三 常用运维操作

  • 启用/禁用站点
    • 启用:sudo a2ensite example.com.conf
    • 禁用:sudo a2dissite example.com.conf
  • 列出与生效
    • 查看可用/已启用站点:ls /etc/apache2/sites-available /etc/apache2/sites-enabled
    • 热加载:sudo systemctl reload apache2
  • 本地测试(无公网 DNS 时)
    • 在客户端或服务器测试环境编辑 /etc/hosts服务器IP site1.com www.site1.com
  • 目录权限与安全
    • 目录属主:sudo chown -R www-data:www-data /var/www/yourdomain
    • 权限建议:sudo find /var/www/yourdomain -type d -exec chmod 755 {} ;、文件 644
  • 日志查看
    • 站点错误日志:tail -f ${APACHE_LOG_DIR}/site1_error.log
    • 访问日志:tail -f ${APACHE_LOG_DIR}/site1_access.log
  • 模块管理
    • 启用/禁用:sudo a2enmod module_namesudo a2dismod module_name

四 启用 HTTPS 与自动化证书

  • 安装 Certbot 并获取证书
    • 安装:sudo apt install certbot python3-certbot-apache
    • 申请并自动配置(可同时覆盖 HTTP→HTTPS 与 www 跳转):
      • 单域:sudo certbot --apache -d site1.com
      • 多域:sudo certbot --apache -d site1.com -d www.site1.com -d site2.com
  • 自动续期
    • 建议测试:sudo certbot renew --dry-run
    • 系统定时:sudo systemctl enable --now certbot.timer(默认每天检查,临近到期自动续期)

五 排错与最佳实践

  • 配置语法与生效
    • 语法检查:sudo apache2ctl configtest
    • 变更后优先 reload 而非 restart,减少中断
  • 端口与监听
    • 确保 /etc/apache2/ports.conf 包含 Listen 80 与(如需)Listen 443,且虚拟主机端口与之匹配
  • 默认站点冲突
    • 多个站点时建议禁用 000-default.conf,避免请求被默认站点捕获
  • 访问控制
    • 生产环境慎用 Indexes,按需设置 AllowOverride(如启用 .htaccess 则设为 All
  • 日志优先
    • 访问异常先看对应站点的 ErrorLog/CustomLog,定位 DocumentRoot、权限、.htaccess 规则 等问题
  • 文件与目录结构
    • 推荐将站点放在 /var/www/<域名>/public_html,便于权限与备份管理
  • 防火墙与云安全组
    • 放行 TCP 80/443(如使用 ufwsudo ufw allow 80,443/tcp;云上需在安全组放行对应端口)

0