Detailed building web-based Apache virtual hosts

Virtual web hosting refers to the same server running on multiple web sites, each site does not actually occupy the entire independent server, it is called a "virtual" web hosting. You can make full use of hardware resources by virtual server web hosting, website building and thus greatly reduce operating costs.
Httpd can use to build virtual host server very easily, only need to run a httpd service will be able to simultaneously support from a large number of web sites.
httpd virtual host support includes the following three types:
the same IP, same port, different domain: domain name based
on the IP address: a different IP, the same port
on the port: the same IP, different ports

in practice, the most commonly used or a different domain name visit the Web site.
First, based virtual hosting:
First, we need to provide domain name resolution for the Web Hosting:

[root@www /]# vim /etc/named.conf 
……       //
zone "test1.com" in {
        type master;
        file "test1.com.zone";
};

zone "test2.com" in {
        type master;
        file "test2.com.zone";
};
[root@www /]# vim /var/named/test1.com.zone 
……       //
        in      ns      www.test1.com.
www     in      a       192.168.1.10
[root@www /]# vim /var/named/test2.com.zone
……      //
        in      ns      www.test2.com.
www     in      a       192.168.1.10

Page document prepared for the virtual host:

[root@www /]# mkdir -p /var/www/html/test1com
[root@www /]# mkdir -p /var/www/html/test2com
[root@www /]# echo "<h1>www.test1.com</h1>" > /var/www/html/test1com/index.html
[root@www /]# echo "<h1>www.test2.com</h1>" > /var/www/html/test2com/index.html

Add a virtual host configuration:

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 

配置 test1 的虚拟站点区域:
 ……        // 省略部分内容
<VirtualHost *:80>             # 配置监听地址和端口  “*” 代表任意地址
    ServerAdmin [email protected]     # 设置管理员的邮箱   可删除
    DocumentRoot "/var/www/html/test1com"    # 指定网站根目录
    ServerName www.test1.com                 # 配置域名
    ServerAlias www.dummy-host.example.com     # 配置别名
    ErrorLog "logs/www.test1.com-error_log"   # 记录错误日志
    CustomLog "logs/www.test1.com-access_log" common    # 记录访问日志
        <Directory "/var/www/html">     # 设置目录访问权限
        Require all granted    # 允许所有
        </Directory>
</VirtualHost>
配置 test2 的虚拟站点区域:
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
[root@www /]# vim /usr/local/httpd/conf/httpd.conf   # 进入主配置文件
……
Include conf/extra/httpd-vhosts.conf         # 找到本行后将开头 # 去掉
[root@www /]# systemctl restart httpd      重启服务使配置生效

Client access web authentication:
Detailed building web-based Apache virtual hosts
Detailed building web-based Apache virtual hosts
II-based virtual host IP addresses:
There is no any connection between each way, do not write based virtual hosts IP addresses and domain names based on a confused

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
……
<VirtualHost 192.168.1.10:80>             # 配置监听地址为 192.168.1.10
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/test1com"
    ServerName www.test1.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test1.com-error_log"
    CustomLog "logs/www.test1.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
<VirtualHost 192.168.1.20:80>             # 配置监听地址为 192.168.1.20
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>

Detailed building web-based Apache virtual hosts
Detailed building web-based Apache virtual hosts
Third, Port-based virtual hosts:

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
……
<VirtualHost 192.168.1.10:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/test1com"
    ServerName www.test1.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test1.com-error_log"
    CustomLog "logs/www.test1.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
<VirtualHost 192.168.1.10:8080>               # 修改端口号
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
listen 80                      # 监听80端口(默认80也可不用写这一行)
listen 8080                  # 监听8080端口

Note: After the completion of each configuration will need to restart the service
Detailed building web-based Apache virtual hosts
Detailed building web-based Apache virtual hosts

Guess you like

Origin blog.51cto.com/14227204/2424738