对于绝大多数用户来说,基于名称的虚拟主机 是最常用、最简单、也是最推荐的方法,因为它允许你使用一个 IP 地址托管多个域名。

场景设定
假设我们要在同一台服务器上搭建两个网站:
- 网站A:
www.example.com(网站根目录:/var/www/example) - 网站B:
www.another-example.com(网站根目录:/var/www/another-example)
基于名称的虚拟主机(最常用)
这种方法通过不同的域名来区分不同的网站,所有域名都指向同一个 IP 地址。
第 1 步:安装 Apache
如果你的服务器上还没有安装 Apache,请先安装它。
在 Debian/Ubuntu 系统上:

sudo apt update sudo apt install apache2
在 CentOS/RHEL 系统上:
sudo yum install httpd # 启动并设置开机自启 sudo systemctl start httpd sudo systemctl enable httpd
第 2 步:创建网站目录
为每个网站创建一个独立的目录来存放网站文件。
# 创建网站A的目录 sudo mkdir -p /var/www/example # 创建网站B的目录 sudo mkdir -p /var/www/another-example
第 3 步:添加测试页面
为了方便测试,我们在每个网站目录下创建一个 index.html 文件,并写入不同的内容,以便我们能够区分。
为 www.example.com 创建页面:

echo "<h1>Welcome to Example.com</h1><p>This is the website for example.com</p>" | sudo tee /var/www/example/index.html
为 www.another-example.com 创建页面:
echo "<h1>Welcome to Another-Example.com</h1><p>This is the website for another-example.com</p>" | sudo tee /var/www/another-example/index.html
重要: 设置目录的所有权,确保 Apache 服务器有权限写入这些目录。
# 在 Debian/Ubuntu 上 sudo chown -R www-data:www-data /var/www/ # 在 CentOS/RHEL 上 sudo chown -R apache:apache /var/www/
第 4 步:创建虚拟主机配置文件
Apache 使用配置文件来定义每个虚拟主机的行为,最佳实践是为每个网站创建一个独立的配置文件。
-
启用
vhosts模块(如果尚未启用):- Debian/Ubuntu:
sudo a2enmod vhost_alias
- CentOS/RHEL: 通常默认已启用。
- Debian/Ubuntu:
-
创建配置文件:
- 在 Debian/Ubuntu 上,配置文件通常存放在
/etc/apache2/sites-available/目录。 - 在 CentOS/RHEL 上,通常存放在
/etc/httpd/conf.d/目录。
为网站A创建配置文件 (
example.conf):# 在 Debian/Ubuntu 上 sudo nano /etc/apache2/sites-available/example.conf # 在 CentOS/RHEL 上 sudo nano /etc/httpd/conf.d/example.conf
粘贴到文件中,并根据你的实际情况修改:
<VirtualHost *:80> # ServerAdmin: 网站管理员的邮箱 ServerAdmin webmaster@example.com # ServerName: 网站的主域名 # ServerAlias: 其他可以访问此网站的域名(可选) ServerName www.example.com ServerAlias example.com # DocumentRoot: 网站文件的根目录 DocumentRoot /var/www/example # 错误日志和访问日志的路径 ErrorLog ${APACHE_LOG_DIR}/error_example.log CustomLog ${APACHE_LOG_DIR}/access_example.log combined <Directory /var/www/example> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>为网站B创建配置文件 (
another-example.conf):# 在 Debian/Ubuntu 上 sudo nano /etc/apache2/sites-available/another-example.conf # 在 CentOS/RHEL 上 sudo nano /etc/httpd/conf.d/another-example.conf
粘贴到文件中:
<VirtualHost *:80> ServerAdmin webmaster@another-example.com ServerName www.another-example.com ServerAlias another-example.com DocumentRoot /var/www/another-example ErrorLog ${APACHE_LOG_DIR}/error_another-example.log CustomLog ${APACHE_LOG_DIR}/access_another-example.log combined <Directory /var/www/another-example> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>注意:
<VirtualHost *:80>中的 表示监听服务器的所有 IP 地址,如果你只想为特定 IP 设置,可以写成<VirtualHost 192.168.1.100:80>。AllowOverride All允许在该目录下使用.htaccess文件。Require all granted允许所有用户访问。
- 在 Debian/Ubuntu 上,配置文件通常存放在
第 5 步:启用新配置
在 Debian/Ubuntu 上:
你需要使用 a2ensite 命令来启用你刚刚创建的站点配置,它会创建一个指向 sites-enabled 目录的符号链接。
sudo a2ensite example.conf sudo a2ensite another-example.conf
最好禁用默认的 000-default.conf 网站,避免干扰:
sudo a2dissite 000-default.conf
在 CentOS/RHEL 上:
只要配置文件放在 /etc/httpd/conf.d/ 目录下,并且文件名以 .conf Apache 就会自动加载它,无需额外命令。
第 6 步:检查配置并重启 Apache
在重启服务之前,强烈建议先检查配置文件是否有语法错误。
# 在 Debian/Ubuntu 和 CentOS/RHEL 上命令相同 sudo apache2ctl configtest
如果输出 Syntax OK,说明配置正确,如果有错误,它会告诉你具体是哪一行有问题。
重启 Apache 以应用所有更改:
# 在 Debian/Ubuntu 上 sudo systemctl restart apache2 # 在 CentOS/RHEL 上 sudo systemctl restart httpd
第 7 步:测试
- 本地测试:在服务器的浏览器中访问
http://localhost或http://127.0.0.1,看是否指向了其中一个网站。 - 域名访问测试:
- 方法A(推荐): 修改你本地电脑的
hosts文件(在C:\Windows\System32\drivers\etc\hostson Windows, or/etc/hostson Linux/Mac),添加以下两行:0.0.1 www.example.com 127.0.0.1 www.another-example.com然后分别用浏览器访问
http://www.example.com和http://www.another-example.com,你应该能看到不同的页面内容。 - 方法B: 如果你已经将这两个域名解析到了你的服务器公网 IP,那么直接在浏览器中访问即可。
- 方法A(推荐): 修改你本地电脑的
至此,你已经成功使用基于名称的虚拟主机搭建了多个网站!
基于IP地址的虚拟主机
这种方法为每个网站分配一个独立的 IP 地址,这在需要使用不同 SSL 证书(HTTP/2 时代)或某些特殊网络环境下可能需要,但现在已不常见。
前提条件
你的服务器必须拥有至少两个 IP 地址。
- 网站A:
168.1.100 - 网站B:
168.1.101
步骤
-
配置IP地址:确保你的服务器已经配置好了这两个 IP 地址。
-
创建网站目录:与方法一的第 2、3 步相同。
-
创建虚拟主机配置文件:与方法一的第 4 步类似,但
<VirtualHost>指令需要指定具体的 IP 地址。为网站A创建配置文件 (
example-ip.conf):<VirtualHost 192.168.1.100:80> ServerAdmin webmaster@example.com ServerName www.example.com DocumentRoot /var/www/example ErrorLog ${APACHE_LOG_DIR}/error_example.log CustomLog ${APACHE_LOG_DIR}/access_example.log combined <Directory /var/www/example> Options Indexes FollowSymLinks AllowOverride All
