Centos7 Apache配置虚拟主机的三种方式

目录 原创

一、虚机主机的三种方式

1.基于IP
2.基于IP+端口 
3.基于域名 
官网文档:http://httpd.apache.org/docs/2.4/ 

二、安装Apache

1.系统环境

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]# ip ad
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
valid_lft forever preferred_lft forever
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736
valid_lft 1388sec preferred_lft 1388sec
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link
valid_lft forever preferred_lft forever

2.yum安装

[root@localhost ~]# yum install -y httpd
httpd x86_64 2.4.6-80.el7.centos base 2.7 M
apr x86_64 1.4.8-3.el7_4.1 base 103 k
apr-util x86_64 1.5.2-6.el7 base 92 k
httpd-tools x86_64 2.4.6-80.el7.centos base 89 k
mailcap noarch 2.1.41-2.el7 base 31 k
httpd.x86_64 0:2.4.6-80.el7.centos
apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7

可以查看安装了内容

[root@localhost ~]# rpm -ql httpd | less

3.配置Selinux文件,SELINUX=disabled

[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
#或者临时关闭
[root@localhost ~]# setenforce 0

4.关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

5.启动httpd服务,访问测试

[root@localhost ~]# systemctl start httpd

三、修改主配置文件

1.查看apache主配置文件,确保存在以下配置,因为等下需要在conf.d/创建虚机主机配置

[root@localhost ~]# vim  /etc/httpd/conf/httpd.conf 
IncludeOptional conf.d/*.conf

2.另外,把 Require all denied默认拒绝访问设置为允许访问: Require all granted,方便测试

<Directory /> 
 AllowOverride none
 #Require all denied
 Require all granted
</Directory> 

四、新增虚拟主机配置文件

1.添加基于多个IP的虚拟主机

1)创建配置文件

[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
 <VirtualHost 192.168.253.128:80>
      ServerName a.com
      DocumentRoot "/www/a.com/"    #网页路径
 </VirtualHost>
 <VirtualHost 192.168.253.129:80>
      ServerName b.com     
      DocumentRoot "/www/b.com/"    #网页路径
  </VirtualHost>

2)网卡绑定多个IP(我的网卡名是eno16777736,不是eth0)

[root@localhost conf.d]# ip addr add 192.168.253.129 dev eno16777736 [root@localhost conf.d]# ip add 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
inet 127.0.0.1/8 scope host lo 
valid_lft forever preferred_lft forever 
valid_lft forever preferred_lft forever 
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff 
inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736 
valid_lft 1542sec preferred_lft 1542sec 
inet 192.168.253.129/32 scope global eno16777736 
valid_lft forever preferred_lft forever 
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link 
valid_lft forever preferred_lft forever 

3)创建虚机主机a.com和b.com的主页面

[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com} 
mkdir: 已创建目录 "/www/a.com" 
mkdir: 已创建目录 "/www/b.com" 
[root@localhost conf.d]# vim /www/a.com/index.html <h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html <h1>Hello,b.com</h1>  

4)检查配置文件是否正常

[root@localhost conf.d]# httpd -t 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message 

5)重启httpd服务

[root@localhost conf.d]# systemctl restart httpd 

6)打开浏览器,查看结果

2.配置基于IP+端口的虚拟主机

1)创建配置文件

[root@localhost ~]# cd /etc/httpd/conf.d/ 
[root@localhost conf.d]# vim virtual.conf 
<VirtualHost 192.168.253.128:80>
        ServerName a.com
        DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost 192.168.253.128:8080>
        ServerName b.com
        DocumentRoot "/www/b.com/"
</VirtualHost>

2)修改httpd主配置文件,在Listen 80下面添加一行监控8080端口

[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
Listen 8080 

3)创建虚机主机a.com和b.com的主页面(如果前面已经创建就不用重复)

[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
Hello,a.com
[root@localhost conf.d]# vim /www/b.com/index.html
Hello,b.com

4)检查配置文件

[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

5)重启httpd服务

[root@localhost conf.d]# systemctl restart httpd

6)打开浏览器,查看结果

3.基于域名的虚拟主机

1)创建配置文件

[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost *:80>
        ServerName a.com
        DocumentRoot "/www/a.com/"
</VirtualHost>

<VirtualHost *:80>
        ServerName b.com
        DocumentRoot "/www/b.com/"
</VirtualHost>

2)修改物理主机hosts文件(C:\Windows\System32\drivers\etc),因为这里是因为物理机去访问Apache服务器

添加:
192.168.253.128 a.com
192.168.253.128 b.com

3)物理主机ping域名测试

4)创建虚机主机a.com和b.com的主页面(如果前面已经创建就不用重复)

[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
Hello,a.com
[root@localhost conf.d]# vim /www/b.com/index.html
Hello,b.com

5)检查配置文件

[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

6)重启httpd服务

[root@localhost conf.d]# systemctl restart httpd

7)打开浏览器,查看结果

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注