CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

Virtual Web hosting refers to the run on the same server multiple Web sites, each site is not independent of the actual take up the entire server, it is called a "virtual" Web host. Can make full use of hardware resources by virtual server Web hosting service, thus greatly reducing site construction and operating costs.

Can build a virtual host server using httpd service very easily, only you need to run a httpd service colleagues will be able to support a large number of Web sites.

httpd web hosting service supports the following three types:

Based domain: using a different domain for each virtual host, but the corresponding IP address is the same;

Based IP address: using a different domain for each virtual host, each corresponding IP address is not the same;

Port-based: This approach does not use the domain name, IP address, to distinguish between different sites content, but use different TCP port, so users need to specify the port number to access while browsing different virtual sites.

These types of virtual Web hosts, domain name-based virtual hosting is the most widely used; based on the IP address and port based virtual hosts are generally only available in-house.

Implementation steps based virtual hosting domain host:

1. To provide for the virtual host domain name resolution

You first need to set up DNS service to provide domain name resolution, DNS service to build detailed steps can refer to the post CentOS 7 set up DNS service , here only the key configuration information:

 [root@localhost /]# vim /etc/named.conf
//修改DNS服务主配置文件
                               …………    //省略部分内容
zone "a.com" IN {
        type master;
        file "a.com.zone";
};
zone "b.com" IN {
        type master;
        file "b.com.zone";
};
[root@localhost /]# vim /var/named/a.com.zone
//编写a.com区域配置文件

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@       IN      NS      www.a.com.
www     IN      A       192.168.1.1
[root@localhost /]# vim /var/named/b.com.zone
//编写b.com区域配置文件

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@       IN      NS      www.b.com.
www     IN      A       192.168.1.1
[root@localhost /]# systemctl start named
//启动DNS服务
[root@localhost /]# nslookup
> www.a.com
Server:     127.0.0.1
Address:    127.0.0.1#53

Name:   www.a.com
Address: 192.168.1.1
> www.b.com
Server:     127.0.0.1
Address:    127.0.0.1#53

Name:   www.b.com
Address: 192.168.1.1
//测试解析

2. To prepare the virtual host web documents

[root@localhost /]# mkdir -p /var/www/html/acom
[root@localhost /]# mkdir -p /var/www/html/bcom
[root@localhost /]# echo "<h1>www.a.com</h1>" > /var/www/html/acom/index.html
[root@localhost /]# echo "<h1>www.b.com</h1>" > /var/www/html/bcom/index.html
//文件存放位置可以自定义

3. Add the virtual host configuration
time when a large number of virtual Web hosting, it is recommended to use a separate virtual host configuration file, then loads them in the main configuration file httpd service by Include. This amendment primary configuration file httpd service will be minimized, easier to maintain the configuration of the content.

root@localhost /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
//创建独立的配置文件
<VirtualHost *:80>                                          //*表示所有地址
    ServerAdmin [email protected]              //管理员邮箱地址       
    DocumentRoot "/var/www/html/acom"       //设置a虚拟站点区域 
    ServerName www.a.com                           //域名
    ServerAlias www.dummy-host.example.com       //别名
    ErrorLog "logs/www.a.com.error_log"                  //错误日志
    CustomLog "logs/www.a.com.access_log" common    //访问日志
    <Directory "/var/www/html">
      Require  all granted              //允许所有主机访问
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/bcom"
    ServerName www.b.com
    ServerAlias www.dummy1-host.example.com
    ErrorLog "logs/www.b.com.error_log"
    CustomLog "logs/www.b.com.access_log" common
    <Directory "/var/www/html">
      Require  all granted
    </Directory>
</VirtualHost>
[root@localhost /]# vim /usr/local/httpd/conf/httpd.conf 
//编写httpd服务的主配置文件
                  …………              //省略部分内容
Include conf/extra/httpd-vhosts.conf
//加载独立的配置文件
[root@localhost /]# systemctl start httpd
//启动httpd服务

4. Client Access test
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

Virtual host IP address implementation steps based on
lab environment, so a simulation IP address on the server

[root@localhost /]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
//复制网卡配置文件
[root@localhost network-scripts]# vim ifcfg-ens33:0
//修改刚才复制的网卡配置文件
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0                          //必须修改
UUID=fddc7556-c28d-4149-afb8-13356cdeb279
DEVICE=ens33:0                       //必须修改
ONBOOT=yes
IPADDR=192.168.1.11               //必须修改
[root@localhost network-scripts]# ifdown ens33:0;ifup ens33:0
//重新加载ens33:0网卡的配置文件
[root@localhost network-scripts]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:98:42:5d  txqueuelen 1000  (Ethernet)
[root@localhost /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
//修改虚拟主机配置文件
<VirtualHost 192.168.1.1:80>                              //把*改为IP地址
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/acom"
    ServerName www.a.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.a.com.error_log"
    CustomLog "logs/www.a.com.access_log" common
    <Directory "/var/www/html">
      Require  all granted
    </Directory>
</VirtualHost>

<VirtualHost 192.168.1.11:80>                    //把*改为IP地址
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/bcom"
    ServerName www.b.com
    ServerAlias www.dummy1-host.example.com
    ErrorLog "logs/www.b.com.error_log"
    CustomLog "logs/www.b.com.access_log" common
    <Directory "/var/www/html">
      Require  all granted
    </Directory>
</VirtualHost>
[root@localhost /]# systemctl restart httpd
//重新加载httpd服务

Client Access test
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

Port-based virtual host implementation steps
to modify the virtual host configuration file

root@localhost /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/acom"
    ServerName www.a.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.a.com.error_log"
    CustomLog "logs/www.a.com.access_log" common
    <Directory "/var/www/html">
      Require  all granted
    </Directory>
</VirtualHost>

<VirtualHost *:8080>                              //修改端口信息
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/bcom"
    ServerName www.b.com
    ServerAlias www.dummy1-host.example.com
    ErrorLog "logs/www.b.com.error_log"
    CustomLog "logs/www.b.com.access_log" common
    <Directory "/var/www/html">
      Require  all granted
    </Directory>
</VirtualHost>
Listen 8080             //监听8080端口,写在主配置文件或虚拟主机配置文件都可以

[root@localhost /]# systemctl restart httpd
//重新加载httpd服务

It is strongly recommended, modify the port information of the time, do not modify the ports from 1 to 1024, because the port is already using the system service!

Client Access test
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

Virtual Directory implementation steps

[root@localhost httpd-2.4.25]# mkdir /yum
[root@localhost httpd-2.4.25]# echo "<h1>www.yum.com</h1>" > /yum/index.html
//创建虚拟目录测试文件
[root@localhost httpd-2.4.25]# vim /usr/local/httpd/conf/httpd.conf 
//编写httpd服务主配置文件
                        …………     //省略部分内容
Alias /test "/yum"        //定义虚拟目录“/test”,物理路径为“/yum”
<Directory "/yum">        //定义目录访问权限
Options Indexes MultiViews FollowSymLinks      //固定格式,这项可以忽略
AllowOverride None             //固定格式。这项可以忽略
Order allow,deny    //匹配顺序为先允许,后拒绝,这项可以忽略
Allow from all        //设置允许所有人访问,这项可以忽略
Require all granted    //对这个目录给予授权
</Directory>
[root@localhost httpd-2.4.25]# systemctl restart httpd
//重新启动httpd服务

Client Test Access
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

If you want to turn on authentication for the virtual directory
to the above configuration modified as follows:

[root@localhost httpd]# vim /usr/local/httpd/conf/httpd.conf 
//修改httpd服务主配置文件
                           …………               //省略部分内容
Alias /test "/yum"
<Directory "/yum">
Options Indexes MultiViews FollowSymLinks                //固定格式可以省略
AllowOverride None                                                      //固定格式可以省略                                                 
AuthName "test"                               //定义受保护的领域名称,该内容将在浏览器弹出的认证对话框中显示
AuthType Basic                                //设置认证的类型,Basic为基本的认证
AuthUserFile /usr/local/httpd/user    //设置用于保存用户账号、密码的认证文件路径
Require valid-user                            //要求只有认证文件中的合法用户才能访问。valid-user表示所有合法用户
</Directory>
[root@localhost htdocs]# cd /usr/local/httpd/
[root@localhost httpd]# bin/htpasswd -c /usr/local/httpd/conf/123.txt xiaozhang
New password: 
Re-type new password: 
Adding password for user xiaozhang
[root@localhost httpd]# systemctl restart httpd
//重新启动httpd服务

: Web site for the user set limit, you can refer Bowen restrictions Detailed CentOS 7 in httpd service user
client access test
CentOS 7 in httpd Web services to build virtual hosts and virtual directory settings Detailed

If the virtual directory for the virtual host, add a virtual host configuration file.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160137.htm