凌峰创科服务平台

Apache如何配置多网站?

核心概念:什么是虚拟主机?

虚拟主机允许你在一台物理服务器上运行多个网站,并且每个网站都有自己独立的域名、配置和内容目录,Apache 通过两种主要方式实现这一点:

Apache如何配置多网站?-图1
(图片来源网络,侵删)
  1. 基于域名的虚拟主机:这是最常用的方式,服务器根据客户端请求的 Host 头部字段来决定将请求指向哪个网站,访问 www.example.comwww.another.com,即使它们指向同一个服务器的同一个 IP 地址,Apache 也能将它们区分开。
  2. 基于 IP 的虚拟主机:服务器为每个网站分配一个独立的 IP 地址,这种方式现在较少使用,除非一个网站必须使用自己的 IP(比如需要配置 SSL 证书但 IP 不够用的情况)。
  3. 基于端口的虚拟主机:为每个网站分配不同的端口号。example.com:8080another.com:8081,这种方式也较少用于公开网站,更多用于内部服务或测试。

本指南将重点讲解最常用的“基于域名的虚拟主机”。


准备工作

在开始配置之前,请确保你已经完成了以下准备工作:

  1. 已安装 Apache:在 Linux 系统上,通常使用 apt (Debian/Ubuntu) 或 yum (CentOS/RHEL) 安装。

    # Debian/Ubuntu
    sudo apt update
    sudo apt install apache2
    # CentOS/RHEL
    sudo yum install httpd
  2. 拥有域名yourdomain.comanotherdomain.com

    Apache如何配置多网站?-图2
    (图片来源网络,侵删)
  3. DNS 解析:将这两个域名解析到你服务器的公网 IP 地址,在你的域名提供商(如阿里云、腾讯云、GoDaddy 等)的控制面板中,添加 A 记录,指向你的服务器 IP。

  4. 服务器权限:你需要有 sudoroot 权限来修改 Apache 配置文件。


配置步骤

我们将配置两个网站作为示例:

  • 网站 A: yourdomain.com
  • 网站 B: anotherdomain.com

步骤 1:创建网站根目录

为每个网站创建一个独立的目录来存放网站文件。

Apache如何配置多网站?-图3
(图片来源网络,侵删)
# 创建网站 A 的目录
sudo mkdir -p /var/www/yourdomain.com/html
# 创建网站 B 的目录
sudo mkdir -p /var/www/anotherdomain.com/html
# 为目录设置所有者和权限
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/anotherdomain.com/html
sudo chmod -R 755 /var/www

注意$USER 是当前用户变量,确保你有权限写入这些目录,如果你使用 root 用户创建,可以省略 chown

步骤 2:创建测试页面

在每个网站的根目录下创建一个 index.html 文件,以便区分它们。

# 网站 A 的测试页面
echo "<h1>Welcome to yourdomain.com</h1><p>This is the website for yourdomain.com</p>" | sudo tee /var/www/yourdomain.com/html/index.html
# 网站 B 的测试页面
echo "<h1>Welcome to anotherdomain.com</h1><p>This is the website for anotherdomain.com</p>" | sudo tee /var/www/anotherdomain.com/html/index.html

步骤 3:创建虚拟主机配置文件

Apache 的虚拟主机配置文件通常存放在 /etc/apache2/sites-available/ 目录下(Debian/Ubuntu)或 /etc/httpd/conf.d/ 目录下(CentOS/RHEL)。

为每个网站创建一个独立的配置文件(推荐)

这是最清晰、最易于管理的方法。

  1. 创建网站 A 的配置文件

    sudo nano /etc/apache2/sites-available/yourdomain.com.conf

    粘贴进去,并修改相应信息:

    <VirtualHost *:80>
        # ServerAdmin 用于接收服务器错误报告的邮箱
        ServerAdmin admin@yourdomain.com
        # 网站的域名和可能的别名
        ServerName yourdomain.com
        ServerAlias www.yourdomain.com
        # 网站的根目录
        DocumentRoot /var/www/yourdomain.com/html
        # 对该目录的访问权限设置
        <Directory /var/www/yourdomain.com/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        # 错误日志和访问日志
        ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log
        CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined
    </VirtualHost>
  2. 创建网站 B 的配置文件

    sudo nano /etc/apache2/sites-available/anotherdomain.com.conf
    ```类似,只需修改域名和路径:
    ```apache
    <VirtualHost *:80>
        ServerAdmin admin@anotherdomain.com
        ServerName anotherdomain.com
        ServerAlias www.anotherdomain.com
        DocumentRoot /var/www/anotherdomain.com/html
        <Directory /var/www/anotherdomain.com/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/anotherdomain.com_error.log
        CustomLog ${APACHE_LOG_DIR}/anotherdomain.com_access.log combined
    </VirtualHost>

在一个配置文件中定义多个虚拟主机

你也可以将所有虚拟主机放在一个文件中,/etc/apache2/sites-available/my-sites.conf,这适用于网站数量不多的情况。

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/html
    <Directory /var/www/yourdomain.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined
</VirtualHost>
<VirtualHost *:80>
    ServerName anotherdomain.com
    ServerAlias www.anotherdomain.com
    DocumentRoot /var/www/anotherdomain.com/html
    <Directory /var/www/anotherdomain.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/anotherdomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/anotherdomain.com_access.log combined
</VirtualHost>

步骤 4:启用虚拟主机配置

创建好配置文件后,你需要告诉 Apache 启用它们。

在 Debian/Ubuntu 系统上,使用 a2ensite 命令:

# 启用网站 A
sudo a2ensite yourdomain.com.conf
# 启用网站 B
sudo a2ensite anotherdomain.com.conf

如果你使用的是单个配置文件 my-sites.conf,则只需运行一次:

sudo a2ensite my-sites.conf

在 CentOS/RHEL 系统上,只需确保配置文件名以 .conf 结尾并保存在 /etc/httpd/conf.d/ 目录下,Apache 重启时会自动加载它们。

步骤 5:禁用默认配置(可选)

为了安全,可以禁用 Apache 的默认欢迎页面。

# Debian/Ubuntu
sudo a2dissite 000-default.conf
# CentOS/RHEL
# 通常需要手动注释或删除 /etc/httpd/conf.d/welcome.conf 文件

步骤 6:检查配置并重启 Apache

在重启服务之前,务必检查配置文件是否有语法错误。

# 检查 Apache 配置语法
sudo apache2ctl configtest  # Debian/Ubuntu
# 或
sudo apachectl configtest    # CentOS/RHEL
# 如果看到 "Syntax OK",说明配置正确

如果一切正常,重启 Apache 服务使新配置生效:

# Debian/Ubuntu (使用 systemd)
sudo systemctl restart apache2
# CentOS/RHEL (使用 systemd)
sudo systemctl restart httpd

测试

打开你的浏览器,分别访问:

  • http://yourdomain.com
  • http://www.yourdomain.com
  • http://anotherdomain.com
  • http://www.anotherdomain.com

你应该能看到每个网站对应的 index.html 页面内容,如果看到 "Welcome to yourdomain.com" 字样,说明配置成功!


最佳实践和注意事项

  1. 配置文件命名:保持配置文件名与域名一致(如 yourdomain.com.conf),便于管理。
  2. 日志分离:为每个网站设置独立的错误日志和访问日志,对于排查问题至关重要。
  3. 目录权限AllowOverride All 允许在该目录下的 .htaccess 文件生效,如果你不使用 .htaccess,可以设置为 None 以提高性能。Require all granted 允许所有用户访问。
  4. 使用 SSL/HTTPS:现代网站必须使用 HTTPS,你可以使用 Let's Encrypt 免费为每个域名获取 SSL 证书,并配置 Apache 的 mod_ssl 模块。
  5. 配置模板:创建一个虚拟主机配置模板文件,添加新网站时只需复制并修改关键信息,可以大大提高效率。
  6. 管理工具:对于大量网站,可以考虑使用如 Virtualmin 这样的管理面板,它们可以图形化地完成这些配置工作。

常见问题排查

如果访问网站时遇到问题,可以按照以下步骤排查:

  1. 检查 DNS:使用 dig yourdomain.comping yourdomain.com 确认域名是否正确解析到你的服务器 IP。

  2. 检查 Apache 状态:确保 Apache 服务正在运行。

    sudo systemctl status apache2  # 或 httpd
  3. 检查配置语法:再次运行 apache2ctl configtest,确保没有语法错误。

  4. 检查文件权限:确认 Apache 运行用户(通常是 www-dataapache)有权限读取网站的 index.html 文件。

    # 查看Apache运行用户
    ps aux | grep apache2
    # 测试权限
    sudo -u www-data cat /var/www/yourdomain.com/html/index.html
  5. 检查日志文件:这是最有效的排错方法!查看你为每个网站配置的错误日志。

    # Debian/Ubuntu
    tail -f /var/log/apache2/yourdomain.com_error.log
    # CentOS/RHEL
    tail -f /var/log/httpd/yourdomain.com_error.log

    日志中会明确告诉你为什么会出错(文件未找到、权限被拒绝等)。

希望这份详细的指南能帮助你成功配置 Apache 多网站!

分享:
扫描分享到社交APP
上一篇
下一篇