<服务>Apache虚拟主机

时间:Nov. 18, 2016 分类:

目录:

什么是虚拟主机

虚拟主机:

部署多个站点,每个站点希望使用不同域名和不同目录,或者不同端口,不同IP。 所有一个http服务器要配置多个站点的话,就要使用虚拟主机技术。

虚拟主机分类:

  1. 基于域名(常用)
  2. 基于端口
  3. 基于IP

基于域名

配置三个站点:

www.why.cn      /var/html/www
blog.why.cn     /var/html/blog
demo.why.cn     /var/html/demo

域名的根目录创建

[root@why-2 var]# mkdir -p html/{www,blog,demo}
[root@why-2 var]# touch html/{www,blog,demo}/index.html
[root@why-2 var]# tree html
html
├── blog
│   └── index.html
├── demo
│   └── index.html
└── www
    └── index.html

3 directories, 3 files

[root@why-2 var]# for name in www blog demo ;do echo "http://$name.why.com" > /var/html/$name/index.html;done
[root@why-2 var]# for name in www blog demo ;do cat /var/html/$name/index.html;done
http://www.why.com
http://blog.why.com
http://demo.why.com

基于域名的虚拟主机配置

[root@why-2 var]# vi /usr/local/apache/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>                  #基于任何ip的80端口
    ServerAdmin 93216193@qq.com     #报警邮箱
    DocumentRoot "/var/html/www"    #域名根目录
    ServerName www.why.cn           #域名
    ServerAlias why.com             #域名别名
    ErrorLog "logs/www.why.com-error_log"       #错误日志的文件目录
    CustomLog "logs/www.why.com-access_log" common  #日志的文件目录 轮询方式
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/blog"
    ServerName blog.why.cn
    ErrorLog "logs/blog.why.com-error_log"
    CustomLog "logs/blog.why.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/demo"
    ServerName demo.why.cn
    ErrorLog "logs/demo.why.com-error_log"
    CustomLog "logs/demo.why.com-access_log" common
</VirtualHost>

添加根目录的权限

[root@why-2 var]# vi /usr/local/apache/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf        #引用httpd-vhosts.conf 配置文件
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf

添加
<Directory "/var/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

去掉Options中的Indexes,不然会

检查语法平滑重启

[root@why-2 htdocs]# ../bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.0.202 for ServerName
Syntax OK
[root@why-2 htdocs]# ../bin/apachectl graceful
httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.0.202 for ServerName

主机配置hosts解析

C:\Windows\System32\drivers\etc\hosts
192.168.0.202   www.why.cn blog.why.cn demo.why.cn

检查一下能否ping通

打开www.why.cn和blog.why.cn网页

基于端口

主配置文件apache开启的端口

[root@why-2 var]# vi /usr/local/apache/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 81
Listen 82

扩展配置文件vhost中配置

[root@why-2 ~]# vi /usr/local/apache/conf/extra/httpd-vhosts.conf 

NameVirtualHost *:80
NameVirtualHost *:81
NameVirtualHost *:82

<VirtualHost *:80>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/www"
    ServerName www.why.cn
    ServerAlias why.com
    ErrorLog "logs/www.why.com-error_log"
    CustomLog "logs/www.why.com-access_log" common
</VirtualHost>


<VirtualHost *:81>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/blog"
    ServerName blog.why.cn
    ErrorLog "logs/blog.why.com-error_log"
    CustomLog "logs/blog.why.com-access_log" common
</VirtualHost>

<VirtualHost *:82>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/demo"
    ServerName demo.why.cn
    ErrorLog "logs/demo.why.com-error_log"
    CustomLog "logs/demo.why.com-access_log" common
</VirtualHost>

输入www.why.cn:80会自动跳转到www.why.cn

输入blog.why.cn

blog.why.cn会被解析为IP地址192.168.0.202的,只有输入81端口才能打开blog.why.cn的网址。

基于ip

创建临时ip地址

[root@why-2 ~]# ifconfig eth0:0 192.168.0.203/24 up
[root@why-2 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:84:18:E4  
          inet addr:192.168.0.202  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe84:18e4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32646 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13656 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10438738 (9.9 MiB)  TX bytes:2053920 (1.9 MiB)
          Interrupt:19 Base address:0x2000 

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:84:18:E4  
          inet addr:192.168.0.203  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:896 (896.0 b)  TX bytes:896 (896.0 b)

扩展配置文件vhost中配置

[root@why-2 ~]# vi /usr/local/apache/conf/extra/httpd-vhosts.conf 
<VirtualHost 192.168.0.202:80>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/www"
    ServerName 192.168.0.202
    ServerAlias why.com
    ErrorLog "logs/www.why.com-error_log"
    CustomLog "logs/www.why.com-access_log" common
</VirtualHost>


<VirtualHost 192.168.0.203:80>
    ServerAdmin 93216193@qq.com
    DocumentRoot "/var/html/blog"
    ServerName 192.168.0.203
    ErrorLog "logs/blog.why.com-error_log"
    CustomLog "logs/blog.why.com-access_log" common
</VirtualHost>

通过新添加的203IP访问

本来计划用云主机的,但是云主机使用域名会有这个情况。