基于多主机的Web服务
【Centos7.4版本】
!!!测试环境我们首关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config [root@localhost ~]# setenforce 0
一、基于多主机的Web服务
1、首先先看一下本地网卡的信息
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cat ifcfg-ens32 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=10.0.0.129 //我本机主网卡P地址 NETMASK=255.255.255.0 GATEWAY=10.0.0.2 DNS1=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens32 UUID=e4b11756-1775-4b6a-adbf-95f3f24f941e DEVICE=ens32 ONBOOT=yes
2、安装HTTP服务,并启动服务
[root@localhost ~]# yum install -y httpd [root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
3、首先看一下配置模板
[root@localhost ~]# vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .......... //上面的我省略了 <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:@@Port@@> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log" CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common </VirtualHost> //这几行是配置虚拟主机的模板
4、创建目录,写入测试首页
[root@localhost ~]# mkdir /var/www/aaa [root@localhost ~]# mkdir /var/www/bbb [root@localhost ~]# echo '<h1>This servername is aaa.test.com</h1>' > /var/www/aaa/index.html [root@localhost ~]# echo '<h1>This servername is bbb.test.com</h1>' > /var/www/bbb/index.html
5、在本地的/etc/hosts文件下写入两个hostname
[root@localhost ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.0.129 aaa.test.com 10.0.0.129 bbb.test.com
//最后两条需要手动添加
6、编辑HTTP的配置文件,添加以下内容
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf .......... //上面的我就省略 <VirtualHost 10.0.0.129:80> documentroot "/var/www/aaa" servername "aaa.test.com" ErrorLog "/var/log/httpd/aaa.test.com-error_log" CustomLog "/var/log/httpd/aaa.test.com-access_log" common </VirtualHost> <VirtualHost 10.0.0.129:80> documentroot "/var/www/bbb" servername "bbb.test.com" ErrorLog "/var/log/httpd/bbb.test.com-error_log" CustomLog "/var/log/httpd/bbb.test.com-access_log" common </VirtualHost> //在文件的最后添加上面的内容
7、重启HTTP服务
[root@localhost ~]# systemctl restart httpd
8、在本地C盘的C:\Windows\System32\drivers\etc\hosts这个文件添加两行内容
1 # Copyright (c) 1993-2009 Microsoft Corp. 2 # 3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 4 # 5 # This file contains the mappings of IP addresses to host names. Each 6 # entry should be kept on an individual line. The IP address should 7 # be placed in the first column followed by the corresponding host name. 8 # The IP address and the host name should be separated by at least one 9 # space. 10 # 11 # Additionally, comments (such as these) may be inserted on individual 12 # lines or following the machine name denoted by a '#' symbol. 13 # 14 # For example: 15 # 16 # 102.54.94.97 rhino.acme.com # source server 17 # 38.25.63.10 x.acme.com # x client host 18 19 # localhost name resolution is handled within DNS itself. 20 # 127.0.0.1 localhost 21 # ::1 localhost 22 23 10.0.0.129 aaa.test.com 24 10.0.0.129 bbb.test.com 25 //添加最后两行就行
9、在浏览器的地址栏分别输入两个域名