@[toc]
apache 常用的功能,虚拟主机

一:虚拟Web主机

  • 在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机

    1.1 httpd支持的虚拟主机类型(三种)

  • 基于域名的类型
  • 基于IP地址的虚拟主机
  • 基于端口的虚拟主机
    例如:
    www.kgc.om
    www.accp.com
    IP相同,端口相同

IP不同,端口相同

IP相同,端口不通

理论+实操:apache 的虚拟web主机配置_第1张图片

二:构建虚拟主机基于域名的实验

2.1.1 安装软件包

[root@localhost ~]# yum install bind httpd -y
Package 32:bind-9.11.4-9.P2.el7.x86_64 already installed and latest version
Package httpd-2.4.6-90.el7.centos.x86_64 already installed and latest version
Nothing to do

2.1.2 关闭防火墙增强服务

[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# 

2.1.3 配置dns

配置dns全局配置文件/etc/named.conf

[root@localhost ~]# vim /etc/named.conf 
options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };

配置dns区域配置文件/etc/named.rfc1912.zones

[root@localhost ~]# vim /etc/named.rfc1912.zones 
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};

zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};      

修改dns的区域数据文件

[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www     IN      A       192.168.247.150
~                                                   
[root@localhost named]# cp -p kgc.com.zone accp.com.zone

修改完毕,启动dns服务

[root@localhost named]# systemctl start named

为客户机配置好dns,去进行测试服务是否生效

理论+实操:apache 的虚拟web主机配置_第2张图片

nslookup 解析成功

理论+实操:apache 的虚拟web主机配置_第3张图片

2.1.4 创建虚拟主机配置文件,位置在/etc/httpd/conf/extra/,为了简明之意,文件名设为vhost.conf

[root@localhost httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run
[root@localhost httpd]# ls -l
total 0
drwxr-xr-x. 2 root root  37 Dec 12 14:45 conf
drwxr-xr-x. 2 root root  82 Dec 12 14:45 conf.d
drwxr-xr-x. 2 root root 146 Dec 12 14:45 conf.modules.d
lrwxrwxrwx. 1 root root  19 Dec 12 14:45 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root  29 Dec 12 14:45 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 Dec 12 14:45 run -> /run/httpd
[root@localhost httpd]# 
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf  magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
[root@localhost extra]# 

/etc/httpd/conf/extra/vhost.conf文件中

  • 指的是所有的ip地址同过80端口都可以访问
    DocumentRoot “是web站点目录”
    ServerName “站点服务域名”
    Errorlog “指定错误日志路径”
    Customlog “指定访问日志路径” 后面跟common扩展工具
    指定详细配置的目录名,可以发现是web站点目录的父目录
    允许所有用户主机的所有访问权限 //后面由此还会扩展更多的配置属性
    
    [root@localhost extra]# vim vhost.conf
    1 
    2   DocumentRoot "/var/www/html/kgc"
    3   ServerName www.kgc.com
    4   Errorlog "logs/www.kgc.com.error_log"
    5   Customlog "logs/www.kgc.comaccess_log" common
    6   
    7    Require all granted
    8   
    9 
    10 
    11 
    12   DocumentRoot "/var/www/html/accp"
    13   ServerName www.accp.com
    14   Errorlog "logs/www.accp.com.error_log"
    15   Customlog "logs/www.accp.comaccess_log" common
    16   
    17    Require all granted
    18   
    19 
### 2.1.5 创建两个web站点的首页,首页文件index.html在/var/www/html下面的站点目录中
```bash
[root@localhost extra]# cd /var/
[root@localhost var]# ls
account  cache  db     games   kerberos  local  log   named  opt       run    target  www
adm      crash  empty  gopher  lib       lock   mail  nis    preserve  spool  tmp     yp
[root@localhost var]# cd www
[root@localhost www]# ls
cgi-bin  html
[root@localhost www]# cd html
[root@localhost html]# ls
[root@localhost html]# mkdir kgc accp
[root@localhost html]# ls
accp  kgc
[root@localhost html]# echo "this is accp web" > accp/index.html
[root@localhost html]# echo "this is kgc web" > kgc/index.html
[root@localhost html]# tree accp kgc
accp
└── index.html
kgc
└── index.html

0 directories, 2 files
[root@localhost html]# 

2.1.7 重点:需要把extra的路径加入到主配置文件中,启动时才可以识别

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
354 Include conf/extra/vhost.conf

2.1.8 开启服务,查看服务端口

[root@localhost html]# systemctl start httpd
[root@localhost html]# netstat -natp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      79262/httpd         

2.1.9 在客户机验证

在这里插入图片描述
理论+实操:apache 的虚拟web主机配置_第4张图片

三:构建虚拟主机————基于端口的实验

以上个实验为基础,进行端口不通的虚拟主机的配置

3.1.1 修改虚拟web主机的配置文件,增加一项8080端口的参数,为了区别站点,需要把站点文件名修改作以区分,不会覆盖

[root@localhost html]# vim /etc/httpd/conf/extra/vhost.conf 
//复制修改增加
 11 
 12   DocumentRoot "/var/www/html/kgc02"
 13   ServerName www.kgc02.com
 14   Errorlog "logs/www.kgc02.com.error_log"
 15   Customlog "logs/www.kgc02.comaccess_log" common
 16   
 17    Require all granted
 18   
 19 
[root@localhost html]# ls
accp  kgc
[root@localhost html]# cp -p kgc kgc02
cp: omitting directory ‘kgc’
[root@localhost html]# mkdir kgc02
[root@localhost html]# echo "this is web kgc02" >kgc02/index.html
[root@localhost html]# 

3.1.2 增加了端口,也就增加了监听地址,监听地址的修改在主配置文件/etc/httpd/conf/httpd/conf中

[root@localhost html]# vim /etc/httpd/conf/httpd.conf
 41 Listen 192.168.247.150:80
 42 Listen 192.168.247.150:8080
 43 #Listen 80

3.1.3 重启服务,验证接口是否开启

[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -napt | grep httpd
tcp        0      0 192.168.247.150:8080    0.0.0.0:*               LISTEN      91814/httpd         
tcp        0      0 192.168.247.150:80      0.0.0.0:*               LISTEN      91814/httpd   

3.1.4 查看验证,IP地址一样,端口不一样

理论+实操:apache 的虚拟web主机配置_第5张图片

四:构建虚拟主机————基于IP不同的实验

4.1.1 添加网卡,创建几个不同的IP地址的虚拟web主机就需要增加几个网卡

[root@localhost html]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.247.150  netmask 255.255.255.0  broadcast 192.168.247.255

ens36: flags=4163  mtu 1500
        inet 192.168.247.158  netmask 255.255.255.0  broadcast 192.168.247.255

验证网卡是否联网

理论+实操:apache 的虚拟web主机配置_第6张图片

4.1.2 修改虚拟web主机的配置文件

备注:当服务器中使用不同IP的虚拟web主机时,/etc/httpd/conf/extra/vhost.conf文件中的IP地址需要具象到一个固定的IP地址,不可以再用*通配符号,以免混淆
[root@localhost html]# vim /etc/httpd/conf/extra/vhost.conf 

  DocumentRoot "/var/www/html/accpaccp"
  ServerName www.accpaccp.com
  Errorlog "logs/www.accpaccp.com.error_log"
  Customlog "logs/www.accpaccp.comaccess_log" common
  
   Require all granted
  

4.1.3 创建新建的虚拟web站点目录

[root@localhost html]# ls
accp  accp02  kgc  kgc02
[root@localhost html]# mkdir accpaccp
[root@localhost html]# echo "this is 192.168.247.158" > accpaccp/index.html

4.1.4 修改主配置文件,增加监听地址

[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.247.158:80

4.1.5 重启httpd服务

[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -natp | grep httpd
tcp        0      0 192.168.247.150:8080    0.0.0.0:*               LISTEN      123662/httpd        
tcp        0      0 192.168.247.158:80      0.0.0.0:*               LISTEN      123662/httpd        
tcp        0      0 192.168.247.150:80      0.0.0.0:*               LISTEN      123662/httpd   

4.1.6 客户机验证

#####因为没有配置dns解析,所以需要输入IP地址去进入网站,接下来增加这个ip地址的域名解析
理论+实操:apache 的虚拟web主机配置_第7张图片
理论+实操:apache 的虚拟web主机配置_第8张图片
在这里插入图片描述

4.1.7 修改dns配置文件

/etc/named.rfc1912.zones文件
[root@localhost html]# vim /etc/named.rfc1912.zones
zone "accpaccp.com" IN {
        type master;
        file "accpaccp.com.zone";
        allow-update { none; };
};
/var/named/目录,创建对应的区域数据文件
[root@localhost html]# cd /var/named
[root@localhost named]# ls
accp.com.zone  data  dynamic  kgc.com.zone  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p accp.com.zone accpaccp.com.zone
[root@localhost named]# vim accpaccp.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www     IN      A       192.168.247.158

4.1.8 重启域名解析服务

[root@localhost named]# systemctl restart named

理论+实操:apache 的虚拟web主机配置_第9张图片

五:创建站点内的超链接

5.1 修改首页

[root@localhost named]# vim /var/www/html/accpaccp/index.html 


 hello world


   

hello world

5.2 客户机测试,当然,还需要在重启HTTPD服务

[root@localhost named]# systemctl restart httpd

理论+实操:apache 的虚拟web主机配置_第10张图片
理论+实操:apache 的虚拟web主机配置_第11张图片