LAMP环境下虚拟主机配置(基于IP)

本教程实验环境:

1
2
3
[root@localhost ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)
[root@localhost ~]#

一、首先在dns server上面配置域名解析

(1).在dns server上面创建三个域名
1
[root@localhost ~]# vim /etc/named.rfc1912.zones

1-1.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
zone "zzqa.com" IN {

type master;
file "zzqa.com";
allow-update { none; };
};
zone "zzqb.com" IN {
type master;
file "zzqb.com";
allow-update { none; };
};
zone "zzqc.com" IN {
type master;
file "zzqc.com";
allow-update { none; };
};
(2) 在网站根目录对应创建三个网站的目录,并在每个目录下创建一个人html测试页面。
1
2
3
4
5
6
7
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir zzqa
[root@localhost html]# mkdir zzqb
[root@localhost html]# mkdir zzqc
[root@localhost html]# ls
1.html 1.php DVWA zzqa zzqb zzqc
[root@localhost html]#
1
2
3
4
[root@localhost html]# echo "I am zzqa" > zzqa/index.html
[root@localhost html]# echo "I am zzqb" > zzqb/index.html
[root@localhost html]# echo "I am zzqc" > zzqc/index.html
[root@localhost html]#
(3)配置域名解析
  • 首先在/var/named/目录下面拷贝3份模板分别作为解析三个站点的配置文件。
    1
    2
    3
    4
    [root@localhost ~]# cd /var/named/
    [root@localhost named]# cp named.localhost zzqa.com
    [root@localhost named]# cp named.localhost zzqb.com
    [root@localhost named]# cp named.localhost zzqc.com
  • 依次编辑zzqa.com zzqb.com zzqc.com三个解析文件,编辑完成,保存并退出。
    1
    [root@localhost named]# vim zzqa.com
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $TTL 1D
    @ IN SOA @ rname.invalid. (
    0 ; serial
    1D ; refresh
    1H ; retry
    1W ; expire
    3H ) ; minimum
    NS @
    @ IN A 192.168.3.112
    www IN A 192.168.3.113
    game IN A 10.1.1.1
    1
    [root@localhost named]# vim zzqb.com
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $TTL 1D
    @ IN SOA @ rname.invalid. (
    0 ; serial
    1D ; refresh
    1H ; retry
    1W ; expire
    3H ) ; minimum
    NS @
    @ IN A 192.168.3.112
    www IN A 192.168.3.114
    game IN A 10.1.1.2
    1
    [root@localhost named]# vim zzqc.com
1
2
3
4
5
6
7
8
9
10
11
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
@ IN A 192.168.3.112
www IN A 192.168.3.115
game IN A 10.1.1.3
(4).到此,我们可以重启named服务,测试一下我们配置的域名解析是否成功。
1
[root@localhost named]# service named restart

1-2.png

重启服务没毛病,说明我们的dns解析配置没出错!

下面测试:
开始测试一下失败了,原因是三个目录没有给可读权限:

  • 因此我们先刚给个可读权限

    1
    2
    3
    [root@localhost named]# ll | grep zzq
    [root@localhost named]# chmod +r zzq*
    [root@localhost named]# ll | grep zzq

    1-3.png

  • 加完权限一定要重启服务

    1
    [root@localhost named]# service  named restart

    1-4.png

  • 然后再来测试就OK了,如下图所示

1-5.png

二、然后配置apache三个虚拟主机

1.在eth0上面添加3个IP地址
1
2
3
[root@localhost ~]# ifconfig eth0:1 192.168.3.113 netmask 255.255.255.0
[root@localhost ~]# ifconfig eth0:2 192.168.3.114 netmask 255.255.255.0
[root@localhost ~]# ifconfig eth0:3 192.168.3.115 netmask 255.255.255.0

配置完之后查看一下:

2-1.png

2.修改apache服务配置文件
1
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  • 在此文件尾部,将下图部分复制三份,中间最好有空行,避免错误也美观。

2-2.png

  • 然后修改为如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<VirtualHost 192.168.3.113:80>
ServerAdmin www.zzqa.com
DocumentRoot /var/www/html/zzqa/
ServerName 192.168.3.113
ErrorLog logs/dummy-host.zzqa.com-error_log
CustomLog logs/dummy-host.zzqa.com-access_log common
</VirtualHost>


<VirtualHost 192.168.3.114:80>
ServerAdmin www.zzqb.com
DocumentRoot /var/www/html/zzqb/
ServerName 192.168.3.114
ErrorLog logs/dummy-host.zzqb.com-error_log
CustomLog logs/dummy-host.zzqb.com-access_log common
</VirtualHost>

<VirtualHost 192.168.3.115:80>
ServerAdmin www.zzqc.com
DocumentRoot /var/www/html/zzqc/
ServerName 192.168.3.115
ErrorLog logs/dummy-host.zzqc.com-error_log
CustomLog logs/dummy-host.zzqc.com-access_log common
</VirtualHost>

2-3.png

要注意图2-3里面的网站路径要和下图2-4创建的三个文件夹路径一致*

2-4.png

  • 然后保存退出
3.重启httpd服务
1
[root@localhost html]# service httpd restart

3-1.png

4.测试,在本机上面将DNS地址改为我们搭建DNS Server的地址

3-2.png

zzqa.png

通过web访问三个站点,如下图所示:

zzqb.png

zzqc.png

没毛病,全部正常!

nice!