Apache Virtual Host function (based on IP, domain name, port number)

Apache virtual host is configured on an Apache server multiple virtual hosts to achieve a server providing multi-site service, in fact, access to different directories on the same server.

There are three main methods:

1. different IP addresses

2. different domain names

3. different port number

 

 First, based on IP address

Step 1: Use nmtui command to add multiple IP addresses for the network card configuration after the need to "disable", "active", reboot the network card.

192.168.2.252

192.168.2.253

192.168.2.254

[/ 24 is the number of bits of the mask   
    default subnet mask Class A IP address of 255.0.0.0

(Since 255 corresponding to binary 1 of 8, it is also abbreviated as "/ 8" represents the network account number 8);
    Class B is 255.255.0.0 (/ 16);
    class C is 255.255.255.0 (/ 24)
    / 30 is 255.255.255.252
    / 32 is 255.255.255.255

We here are using a Class C]

 

 

 

Step 2: Use the ping command to see the changed configuration card

 

Step 3: Create three sites data directory under / home / wwwroot directory, respectively

 

 Step 4: The IP address-based virtual hosts are described in the configuration file.

 

 In the empty position is used to describe the present graphic editor based on the virtual host IP address

(When the browser search http: // + IP address The following will jump directly to the IP address corresponding to a position, time can edit "rows + yy" a> "p" in the command mode with copy and paste the text.) Key! ! : Save and exit reboot (systemctl restart httpd) httpd service and view (systemctl status httpd) httpd service status.

 

 

 

 Step 5: writing to each page and increases the content of each directory corresponding

 

 

Step 6: in order to access each IP

 

 

 

 

 

 

 

 

 

 

Question 1: ping nowhere yum can not download service httpd

网关(Gateway)又称网间连接器、协议转换器。默认网关在网络层上以实现网络互连,是最复杂的网络互连设备,仅用于两个高层协议不同的网络互连。网关的结构也和路由器类似,不同的是互连层。网关既可以用于广域网互连,也可以用于局域网互连

网关实质上是一个网络通向其他网络的IP地址。比如有网络A和网络B,网络A的IP地址范围为“192.168.1.1~192. 168.1.254”,子网掩码为255.255.255.0;网络B的IP地址范围为“192.168.2.1~192.168.2.254”,子网掩码为255.255.255.0。在没有路由器的情况下,两个网络之间是不能进行TCP/IP通信的,即使是两个网络连接在同一台交换机(或集线器)上,TCP/IP协议也会根据子网掩码(255.255.255.0)判定两个网络中的主机处在不同的网络里。而要实现这两个网络之间的通信,则必须通过网关。如果网络A中的主机发现数据包的目的主机不在本地网络中,就把数据包转发给它自己的网关,再由网关转发给网络B的网关,网络B的网关再转发给网络B的某个主机(如附图所示)。网络A向网络B转发数据包的过程。

解决方法:

(1)查看网络适配器是否为桥接模式,如果是的话重启一下yum和本机的网卡再ping。

(2)如果还ping不通,首先判断是否能ping通网关(网关:0.0.0.0,cnetos7可以用route命令查看,default哪一行就是网关。)

 

 如果可以ping通,查看是不是防火墙的问题,再重启网卡ping一次,能ping通网关一般都没问题。

 

问题2:按上课步骤编辑配置文件用ip就是打不开

 

 

二、基于域名

当服务器无法为每个网站都分配到独立IP地址时,可以试试让Apache服务程序自动识别来源主机名或域名然后跳转到指定的网站。

第1步:配置网卡IP地址与hosts文件。

 

 hosts文件作用是定义IP地址与主机名的映射关系,即强制将某个主机名地址解析到指定的IP地址

 

[root@linuxprobe ~]# vim /etc/hosts

 

 

//每行只能写一条,格式为IP地址+空格+主机名(域名)。

 

192.168.10.10 www.linuxprobe.com

 

192.168.10.10 bbs.linuxprobe.com

 

192.168.10.10 tech.linuxprobe.com

 

 

第2步:分别创建网站数据目录,并写入不同的首页文件。

 

 

 第3步:在配置文件中描述基于主机名称的虚拟主机。
编辑主配置文件(/etc/httpd/conf/httpd.conf),在主配置文件的末尾按下面格式定义虚拟主机信息:

 

 如果出现下图所示问题,请仔细检查是否敲错了文本。

 

 配置完后保存退出重新启动httpd服务。(看他绿绿的就对了)

 

 

 第4步:分别访问网站验证结果。

 

 

 

三、基于端口

第1步:配置服务器IP地址

 

 第2步:分别创建网站数据目录,在配置文件中描述基于端口号的虚拟主机。

分别创建端口为6111,6222的网站数据目录:

[root@linuxprobe ~]# mkdir -p /home/wwwroot/6111

[root@linuxprobe ~]# mkdir -p /home/wwwroot/6222

分别在网站数据目录中写入不同内容的主页文件:

[root@linuxprobe ~]# echo "port:6111" > /home/wwwroot/6111/index.html

[root@linuxprobe ~]# echo "port:6222" > /home/wwwroot/6222/index.html

 

 

第3步:在配置文件中描述基于端口号的虚拟主机。

编辑主配置文件(/etc/httpd/conf/httpd.conf),找到约在42行的Listen 80,并在下面追加:

Listen 6111
Listen 6222

 

 

然后在主配置文件的末尾按下面格式定义虚拟主机信息:

<VirtualHost 192.168.10.10:6111>
DocumentRoot “/home/wwwroot/6111”
ServerName www.linuxprobe.com
<Directory “/home/wwwroot/6111”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.10.10:6222>
DocumentRoot “/home/wwwroot/6222”
ServerName bbs.linuxprobe.com
<Directory “/home/wwwroot/6222”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

 

 

 

修改配置参数到主配置文件(/etc/httpd/conf/httpd.conf)的末尾然后重启apache网站服务程序

也可将需要的服务加入到开机启动项中:“systemctl enable httpd

第4步:分别访问网站验证结果。

 

Guess you like

Origin www.cnblogs.com/Zh1z3ven/p/11774182.html