1、概念
虚拟主机:需要部署多个站点,每个站点需要使用不同的域名和站点目录,或者不同的端口,不同的IP,这个时候需要虚拟主机。
简单的说,一个http服务配置多个站点,就叫虚拟主机。
分类:
a、基于域名(常用)
b、基于端口
c、基于ip
2、环境
192.168.3.40
www.wolf.com
/web/html/www
192.168.3.40
bbs.wolf.com
/web/html/bbs
192.168.3.40
blog.wolf.com /web/html/blog
3、创建目录
[root@python apache]# mkdir -p /web/html/{www,blog,bbs}
[root@python apache]# tree /var/html
/var/html [error opening dir]
0 directories, 0 files
[root@python apache]# tree /web/html
/web/html
bbs
blog
www
3 directories, 0 files
[root@python apache]#
添加相应站点文件
[root@python apache]# for name in www bbs blog;do echo "http://$name.wolf.com">>/web/html/$name/index.html;done
[root@python apache]# for name in www bbs blog;do cat /web/html/$name/index.html;done
http://www.wolf.com
http://bbs.wolf.com
http://blog.wolf.com
4、编辑虚拟主机文件
[root@python extra]# vi httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com 这里配置了,就覆盖主配置文件了
DocumentRoot "/soft/apache/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
编辑
<VirtualHost *:80>
ServerAdmin 1098331428@qq.com
DocumentRoot "/web/html/www"
ServerName www.wolf.com
ServerAlias wolf.com
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1098331428@qq.com
DocumentRoot "/web/html/blog"
ServerName blog.wolf.com
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1098331428@qq.com
DocumentRoot "/web/html/bbs"
ServerName bbs.wolf.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>
5、编辑主配置文件(打开相应的模块)
把井号去掉# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
结果如下
[root@python conf]# grep "^Include" httpd.conf
Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-vhosts.conf
重启
[root@python conf]# ../bin/a
ab apachectl apxs
[root@python conf]# ../bin/apachectl graceful
6、测试(报403错误,因为主配置文件需要配置,禁止其他目录访问)
[root@python conf]# curl -I http://wolf.com
HTTP/1.1 403 Forbidden
Date: Sun, 06 Nov 2016 15:00:55 GMT
Server: Apache/2.2.31 (Unix)
Content-Type: text/html; charset=iso-8859-1
[root@python conf]# curl -I http://www.wolf.com
HTTP/1.1 403 Forbidden
Date: Sun, 06 Nov 2016 15:01:01 GMT
Server: Apache/2.2.31 (Unix)
Content-Type: text/html; charset=iso-8859-1
[root@python conf]# curl -I http://bbs.wolf.com
HTTP/1.1 403 Forbidden
Date: Sun, 06 Nov 2016 15:01:05 GMT
Server: Apache/2.2.31 (Unix)
Content-Type: text/html; charset=iso-8859-1
[root@python conf]# curl -I http://blog.wolf.com
HTTP/1.1 403 Forbidden
Date: Sun, 06 Nov 2016 15:01:14 GMT
Server: Apache/2.2.31 (Unix)
Content-Type: text/html; charset=iso-8859-1
7、修改主配置文件(属于优化)
[root@python conf]# vi httpd.conf
<Directory "/web/html">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
8、再次测试
[root@python conf]# ../bin/apachectl -t
Syntax OK
[root@python conf]# ../bin/apachectl graceful
[root@python conf]# curl -I http://blog.wolf.com
HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 15:07:09 GMT
Server: Apache/2.2.31 (Unix)
Last-Modified: Sun, 06 Nov 2016 14:47:39 GMT
ETag: "637e44-15-540a2fd214556"
Accept-Ranges: bytes
Content-Length: 21
Content-Type: text/html
[root@python conf]# curl -I http://wolf.com
HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 15:07:14 GMT
Server: Apache/2.2.31 (Unix)
Last-Modified: Sun, 06 Nov 2016 14:47:39 GMT
ETag: "64482bf-14-540a2fd214556"
Accept-Ranges: bytes
Content-Length: 20
Content-Type: text/html
[root@python conf]# curl -I http://blog.wolf.com
HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 15:07:18 GMT
Server: Apache/2.2.31 (Unix)
Last-Modified: Sun, 06 Nov 2016 14:47:39 GMT
ETag: "637e44-15-540a2fd214556"
Accept-Ranges: bytes
Content-Length: 21
Content-Type: text/html