Apache 配置虚拟主机三种方式:(推荐第二种) 一、基于 IP
- 假设服务器有个 IP 地址为 192.168.1.10 ,使用 ifconfig 在同一个网络接口 eth0 上绑定 3 个 IP :
[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13
- 修改 hosts 文件,添加三个域名与之一一对应:
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com
- 建立虚拟主机存放网页的根目录,如在 /www 目录下建立 test1 、 test2 、 test3 文件夹,其中分别存放 1.html 、 2.html 、 3.html
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
- 在 httpd.conf 中将附加配置文件 httpd-vhosts.conf 包含进来,接着在 httpd-vhosts.conf 中写入如下配置:
<VirtualHost 192.168.1.11:80>
ServerName www.test1.com
DocumentRoot /www/test1/
</virtualhost><VirtualHost 192.168.1.12:80>
ServerName www.test1.com
DocumentRoot /www/test2/
</virtualhost><VirtualHost 192.168.1.13:80>
ServerName www.test1.com
DocumentRoot /www/test3/
</virtualhost>- 测试下每个虚拟主机,分别访问 www.test1.com 、 www.test2.com 、 www.test3.com
二、基于主机名
- 设置域名映射同一个 IP ,修改 hosts :
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com
- 跟上面一样,建立虚拟主机存放网页的根目录
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
- 在 httpd.conf 中将附加配置文件 httpd-vhosts.conf 包含进来,接着在 httpd-vhosts.conf 中写入如下配置:
NameVirtualHost *:80 <VirtualHost *:80>
ServerName *
DocumentRoot /www/
</virtualhost> <virtualhost *:80="">ServerName www.test1.com
DocumentRoot /www/test1/
</virtualhost><VirtualHost *:80=""> ServerName www.test2.com
DocumentRoot /www/test2/
</virtualhost> <virtualhost *:80=""> ServerName www.test3.comDocumentRoot /www/test3/
</virtualhost>- 测试下每个虚拟主机,分别访问 www.test1.com 、 www.test2.com 、 www.test3.com
三、基于端口
-
修改配置文件 将原来的 Listen 80 改为 Listen 80 Listen 8080
-
更改虚拟主机设置:
<VirtualHost 192.168.1.10:80>
DocumentRoot /var/www/test1/
ServerName www.test1.com
</virtualhost>
<VirtualHost 192.168.1.10:8080>
DocumentRoot /var/www/test2
ServerName www.test2.com
</virtualhost>
Ngix 配置方法:( ngix 配置简单 并发数高)
server { listen 80;
server_name www.test1.com;
root /data/www/html/www.test1.com;
index index.html index.htm index.php;
} server { listen 80;
server_name www.test2.com;
root /data/www/html/www.test2.com;
index index.html index.htm index.php;
}