Apache: http虚拟主机

apache: 服务器,Host, 物理主机

如果一个很小的站点就占用一个物理主机的话,很浪费,

所以可以把物理主机虚拟成多个虚拟主机,服务多个不同

的站点

Apache:

中心主机

虚拟主机

     IP, 80

(1)  基于IP

每一个虚拟主机使用一个IP地址

(2)  基于端口

IP地址只有一个,但端口不一样

但客户不知使用的端口,一般很少用

(3)  基于域名的

IP和端口都一样,但主机名不一样

www.magedu.com

www.b.net

www.a.org

这3个IP都可以解释到同一个IP地址,但

主机名不一样,是根据http请求里的host判断

访问哪一个虚拟主机。

Apache 2.2 需要启用NameVirtualHost

Apache 2.4 不需要

 

例子:

ServerName: www.a.org

ServerName:www2.a.org

DocumentRoot

#封装定义用户访问某一个本地站点的文件系统目录下的文件

#的时候具备什么属性

<Directory “/www/a.org”>

    Option

  AllowOverride

</Directory>

Alias

Errorlog

CustomLog

以上对于每个主机都是独立的

 

#定义访问URL下的文件可以有什么的方法和权限的

<Location “/images”>

          

</Location>

例子:

<Location “/server-status”>

           SetHandlerserver-status #定义访问这个URL的action

     Order Deny,Allow#allow是默认

     Allow from all  #允许

</Location>

http:// 172.16.100.1/server-status


几行点线(256个线程)中每个点代表一个可启用的线程,W表示一个线程

处在允许的状态。

 

ScriptAlias   ---允许执行CGI脚本的目录在哪里

客户端动态: 有风险

服务器端动态: 不可用控制其它,但可以控制自己

定义虚拟主机:

启用虚拟主机,必须注释中心主机的DocumentRoot即可

<VirtualHost HOST>

</VirtualHost>

基于IP,HOST的写法

HOST

IP1:80

  IP2:80

基于端口:

HOST

    IP:80

  IP:8080

基于主机名的:

  IP:80

  ServerName 不同

 

首先,注释DocumentRoot

Vim ../conf.d/virtual.conf

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerName www.a.org

   DocumentRoot “/www/a.org”

</VirtualHost>

 

使用httpd –t检测,目录不存在

 

Mkdir –pv /www/{magedu.com,a.org}

Cd ./magedu.com

Vim index.html

<title> magedu.com </title>

Cd ./ a.org

Vim index.html

<title>a.org</title>

再检测语法, httpd –t

 

增加一个IP地址:

ip addr add 172.168.100.2/16 dev eth0

ip addr show

ping 172.168.100.2

 

基于端口:

Listen 8080  ---监听8080端口

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerName www.a.org

   DocumentRoot “/www/a.org”

</VirtualHost>

<VirtualHost 172.16.100.1:8080>

   ServerName www.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

混合使用了基于IP和基于端口的

Mkdir –pv /www/{ b.org}

Cd ./ b.net

Vim index.html

<title>b.net</title>

 

基于主机名的:

在virtual.conf中加入:

#基于域名的方式需要加这一行NameVirtualHost

NameVirtualHost 172.16.100.2:80

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.a.org

   DocumentRoot “/www/a.org”

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.d.gov

   DocumentRoot “/www/d.gov”

</VirtualHost>

 

<VirtualHost 172.16.100.1:8080>

   ServerNamewww.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

 

Mkdir –pv /www/{ d.gov}

Cd ./ b.net

Vim index.html

<title>d.gov</title>

 

最简单的方式是编制本机的host文件;

172.16.100.2 www.a.org

172.16.100.2 www.d.gov

 

混合使用3中方式了。

 

让不同的站点使用不同的日志;

NameVirtualHost 172.16.100.2:80

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

   CustomLog  /var/log/httpd/magedu.com/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.a.org

   DocumentRoot “/www/a.org”

CustomLog  /var/log/httpd/a.org/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.d.gov

   DocumentRoot “/www/d.gov”

 CustomLog /var/log/httpd/d.gov/access_log combined

</VirtualHost>

 

<VirtualHost 172.16.100.1:8080>

   ServerNamewww.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

查看默认日志:

Tail /var/log/httpd/error.log

还要创建日志目录,不然会报错。

 

不允许d.gov拒绝172.16.100.177访问的配置:

NameVirtualHost 172.16.100.2:80

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

   CustomLog  /var/log/httpd/magedu.com/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.a.org

   DocumentRoot “/www/a.org”

CustomLog  /var/log/httpd/a.org/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.d.gov

   DocumentRoot “/www/d.gov”

 CustomLog /var/log/httpd/d.gov/access_log combined

 <Directory “/www/d.gov”>

              Options none

    AllowOverride none

    Order deny,allow

    Deny from 172.16.100.177

</Directory>

</VirtualHost>

 

<VirtualHost 172.16.100.1:8080>

   ServerNamewww.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

 

这时访问www.d.gov,会默认跳到webcome页面,可以把

Webcome.conf删除。

 

需要www.a.org需要密码访问的配置方式:

NameVirtualHost 172.16.100.2:80

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

   CustomLog  /var/log/httpd/magedu.com/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.a.org

   DocumentRoot “/www/a.org”

CustomLog  /var/log/httpd/a.org/access_log combined

<Directory “/www/d.gov”>

              Options none

    AllowOverride none

    AuthType basic

    AuthName “Restrict area”

    AuthUserFile “/etc/httpd/.htpasswd”

    Required valid-user

</Directory>

 

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.d.gov

   DocumentRoot “/www/d.gov”

 CustomLog /var/log/httpd/d.gov/access_log combined

 <Directory “/www/d.gov”>

              Options none

    AllowOverride none

    Order deny,allow

    Deny from 172.16.100.177

</Directory>

</VirtualHost>

 

<VirtualHost 172.16.100.1:8080>

   ServerNamewww.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

 

Htpasswd –c –m /etc/httpd/.htpasswd jerry  #第一次要加-c

Htpasswd  –m/etc/httpd/.htpasswd tom

 

安装apache操作手册:

#yum –y install httpd-manual

安装完毕后打开手册:

在任何一个站点都可以打开,只需要在后面加manual:

www.a.org/manual

 

定义默认虚拟主机:

#放在第一行,如果用户访问不存在的主机,就会跳到默认虚拟主机

<VirtualHost  _default_:80>

   DocumentRoot “/www/default”

</VirtualHost>

 

<VirtualHost 172.16.100.1:80>

   ServerNamehelle.magedu.com

   DocumentRoot “/www/magedu.com”

   CustomLog  /var/log/httpd/magedu.com/access_log combined

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.a.org

   DocumentRoot “/www/a.org”

CustomLog  /var/log/httpd/a.org/access_log combined

<Directory “/www/d.gov”>

              Options none

    AllowOverride none

    AuthType basic

    AuthName “Restrict area”

    AuthUserFile “/etc/httpd/.htpasswd”

    Required valid-user

</Directory>

 

</VirtualHost>

<VirtualHost 172.16.100.2:80>

   ServerNamewww.d.gov

   DocumentRoot “/www/d.gov”

 CustomLog /var/log/httpd/d.gov/access_log combined

 <Directory “/www/d.gov”>

              Options none

    AllowOverride none

    Order deny,allow

    Deny from 172.16.100.177

</Directory>

</VirtualHost>

 

<VirtualHost 172.16.100.1:8080>

   ServerNamewww.b.net

   DocumentRoot “/www/b.net”

</VirtualHost>

 

 

Mkdir –pv /www/{ default }

Cd ./ default

Vim index.html

<title> default </title>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值