Apache配置虚拟主机

在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录。Apache虚拟主机配置有3中方法:基于IP配置、基于端口配置,基于主机名配置。本文中Apache版本为2.4.28,主机版本是CentOS 6.9,由于是编译安装,其安装路径是/usr/local/apache/,配置文件在/etc/httpd/目录下,DocumentRoot和Directory路径为/www/htdocs/,主机ip地址为172.16.8.11/24,selinux和iptables已关闭

配置虚拟主机前的准备:
1.启用vhosts
编辑主配置文件,找到 #Include /etc/httpd/extra/httpd-vhosts.conf这一行,把前面的#号去掉,然后重读配置文件

[root@localhost ~]# vim /etc/httpd/httpd.conf
Include /etc/httpd/extra/httpd-vhosts.conf
[root@localhost ~]# service httpd reload

2,创建虚拟主机存放的根目录,3个测试页面

[root@localhost ~]# mkdir -p /vhosts/{web1,web2,web3}/htdcos
[root@localhost ~]# vim /vhosts/web1/htdcos/index.html
Page at web1
[root@localhost ~]# vim /vhosts/web2/htdcos/index.html
Page at web2
[root@localhost ~]# vim /vhosts/web3/htdcos/index.html
Page at web3

一,基于ip配置虚拟主机
主机ip地址为172.16.8.11/24,在eth0网卡上额外添加2个ip地址

[root@localhost ~]# ip addr add 172.16.8.12/24 dev eth0 
[root@localhost ~]# ip addr add 172.16.8.13/24 dev eth0 

编辑虚拟主机配置文件,添加如下内容

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web1/htdocs"
    <Directory "/vhosts/web1/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.12:80>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web2/htdocs"
    <Directory "/vhosts/web2/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.13:80>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web3/htdocs"
    <Directory "/vhosts/web3/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重载httpd配置文件

[root@localhost ~]# service httpd reload

在浏览器上依次输入172.16.8.11,172.16.8.12,172.16.8.13
得到的结果依次为Page at web1,Page at web2,Page at web3

二,基于端口配置虚拟主机
编辑主配置文件,在Listen 80下添加2行

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

修改虚拟主机配置文件

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web1/htdocs"
    <Directory "/vhosts/web1/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.11:8008>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web2/htdocs"
    <Directory "/vhosts/web2/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.11:8080>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web3/htdocs"
    <Directory "/vhosts/web3/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重启httpd服务

[root@localhost ~]# service httpd restart

在浏览器上依次输入172.16.8.11,172.16.8.11:8008,172.16.8.11:8080
得到的结果依次为Page at web1,Page at web2,Page at web3

三,基于主机名配置虚拟主机
修改虚拟主机的配置文件

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>
    ServerName web1.example.com
    DocumentRoot "/vhosts/web1/htdocs"
    <Directory "/vhosts/web1/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.11:80>
    ServerName web2.example.com
    DocumentRoot "/vhosts/web2/htdocs"
    <Directory "/vhosts/web2/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 172.16.8.11:80>
    ServerName web3.example.com
    DocumentRoot "/vhosts/web3/htdocs"
    <Directory "/vhosts/web3/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重载httpd配置

[root@localhost ~]# service httpd reload

修改浏览器所在的系统hosts文件(Windows路径是C:\Windows\System32\drivers\etc\hosts,Linux路径是/etc/hosts),添加如下3行

172.16.8.11 web1.example.com
172.16.8.11 web2.example.com
172.16.8.11 web3.example.com

在浏览器上依次输入web1.example.com,web2.example.com,web3.example.com
得到的结果依次为Page at web1,Page at web2,Page at web3

以上三种配置虚拟主机的方式可以混合使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值