Apache配置虚拟主机

    Apache虚机主机的技术能让一个Apache服务于多个不同的web站点,可以大大节省资源,一个用户只需要申请一个域名,和web服务器的公网ip绑定,即可提供对外服务,下面简单介绍一下虚拟主机的配置方法

一、环境准备:

操作系统版本:redhat 6.5(x86_64)

cat /etc/redhat-release 

Red Hat Enterprise Linux Server release 6.5 (Santiago)

ip地址:192.168.75.131


1、安装httpd

yum -y install httpd-2.2.15


# cd /etc/httpd/conf.d

# ls

README    welcome.conf


关于欢迎页面的解释


# cat welcome.conf 

# This configuration file enables the default "Welcome"

# page if there is no default index page present for

# the root URL.  To disable the Welcome page, comment

# out all the lines below.

#

<LocationMatch "^/+$">

    Options -Indexes

    ErrorDocument 403 /error/noindex.html #如果没有指定默认页面,就显示欢迎页面,如果要取消,可以删除掉welcome.conf 

</LocationMatch>



2、关闭seLinux

# setenfore 0 #httpd服务受到selinux的影响,关闭selinux

# getenforce   

Permissive




3、为了防止虚拟主机和中心主机有冲突,最好注释掉中心主机的DocumentRoot

vim /etc/httpd/conf/httpd.conf 

 291 #DocumentRoot "/var/www/html"




二、添加基于ip的虚机主机配置文件

# cat virtual_host.conf 
#基于ip的虚拟主机
<VirtualHost 192.168.75.131:80>
ServerName www.a.com   #这里的ServerName就是域名,配置之后,也可以用域名来访问(需要在客户端hosts文件中指定),只要能解析到这个ip地址上就能访问,另外ServerName一定配置,随便配置一个也行,不然启动的时候会有"Failed to resolve server name or specify an explicit ServerName  的警告",虽然后续的访问都正常,但是启动的时候有警告总是不好的
DocumentRoot "/www/a.com"
</VirtualHost>
<VirtualHost 192.168.75.141:80>
DocumentRoot "/www/b.com"
ServerName www.b.com
</VirtualHost>


4、编写index.html主页文件

# mkdir /www/a.com /www/b.com -pv

mkdir: created directory `/www'

mkdir: created directory `/www/a.com'

mkdir: created directory `/www/b.com'


# cat  /www/b.com/index.html

<title>B.COM</title>
<h1>b.com </h1>


# cat /www/a.com/index.html 

<title>A.com</title>
<h1>a.com</h1>




5、添加虚拟ip

# ip addr add 192.168.75.141/24 dev eth1

# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c5:d9:00 brd ff:ff:ff:ff:ff:ff

    inet 192.168.75.131/24 brd 192.168.75.255 scope global eth1

    inet 192.168.75.141/24 scope global secondary eth1

    inet6 fe80::20c:29ff:fec5:d900/64 scope link 

       valid_lft forever preferred_lft forever


#httpd -t  #检查配置文件语法是否正确

Syntax OK


# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]

  

  

6、访问

http://192.168.75.131/


wKiom1bBinHTchDlAABAVPIOb98648.jpg

http://192.168.75.141/

wKioL1bBjp2xSAMgAABCDde1olI617.jpg



7、日志文件日志

如果没有单独为虚拟主机配置访问日志,和错误日志,则日志记录在默认位置

# ls /var/log/httpd/

access_log  error_log



三、添加基于端口的虚拟主机

# cat virtual_host.conf 
#基于端口的的虚拟主机
listen 888
listen 889
<VirtualHost 192.168.75.131:888>
ServerName www.a.com  
DocumentRoot "/www/a.com"
</VirtualHost>
<VirtualHost 192.168.75.131:889>
DocumentRoot "/www/b.com"
ServerName www.b.com 
</VirtualHost>


#httpd -t 

Syntax OK

# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]


访问:

http://192.168.75.131:889/

Apache配置虚拟主机_第1张图片


http://192.168.75.131:888/


Apache配置虚拟主机_第2张图片


四、添加基于域名的虚拟主机

# cat virtual_host.conf 
#基于域名的的虚拟主机
NameVirtualHost 192.168.75.131:80  #httpd 2.2需要添加NameVirtualHost 指定域名对应的ip地址和端口号,httpd2.4之后不用添加该指令
<VirtualHost 192.168.75.131:80>
ServerName www.a.com  
DocumentRoot "/www/a.com"
</VirtualHost>
<VirtualHost 192.168.75.131:80>
DocumentRoot "/www/b.com"
ServerName www.b.com
</VirtualHost>

#httpd -t 

Syntax OK


# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]



客户端hosts添加地址映射

C:\WINDOWS\System32\drivers\etc\hosts

192.168.75.131 www.b.com

192.168.75.131 www.a.com


访问:


http://www.a.com/

Apache配置虚拟主机_第3张图片

http://www.b.com/

wKioL1bBi8yj1VbNAAA-GP9GdlM510.jpg


如果此时按照ip来访问,http://192.168.75.131/  访问到了是的第一个www.a.com,如果用ip访问基于域名的虚拟主机,则按照先后的顺序来

wKioL1bBi-nQ1_rlAABAVPIOb98309.jpg



五、为虚机主机定义单独的日志目录


# cat virtual_host.conf 

#基于域名的的虚拟主机

NameVirtualHost 192.168.75.131:80

<VirtualHost 192.168.75.131:80>

ServerName www.a.com  

DocumentRoot "/www/a.com"

ServerAlias a.com

ErrorLog "logs/a.com-error_log"

CustomLog "logs/a.com-access_log" common


</VirtualHost>


<VirtualHost 192.168.75.131:80>

DocumentRoot "/www/b.com"

ServerName www.b.com

ServerAlias b.com

ErrorLog "logs/b.com-error_log"

CustomLog "logs/b.com-access_log" common

</VirtualHost>



#httpd -t 

Syntax OK


# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]



# cd /etc/httpd/logs  #自动生成了日志文件

# ls

access_log  a.com-access_log  a.com-error_log  b.com-access_log  b.com-error_log  error_log


访问:

http://www.a.com/

http://www.b.com/



查看日志:

# tailf a.com-access_log 

192.168.75.1 - - [15/Feb/2016:00:39:42 +0800] "GET / HTTP/1.1" 304 -


# tail b.com-access_log 

192.168.75.1 - - [15/Feb/2016:00:39:33 +0800] "GET / HTTP/1.1" 304 -




六、定义虚拟主机的访问权限,让192.168.75.1无法访问www.a.com


# vim virtual_host.conf 


#基于域名的的虚拟主机

NameVirtualHost 192.168.75.131:80

<VirtualHost 192.168.75.131:80>


ServerName www.a.com

DocumentRoot "/www/a.com"

ServerAlias a.com

ErrorLog "logs/a.com-error_log"

CustomLog "logs/a.com-access_log" common


<Directory "/www/a.com">

Options none

AllowOverride none

Order deny,allow

Deny from 192.168.75.1

</Directory>



</VirtualHost>


<VirtualHost 192.168.75.131:80>

DocumentRoot "/www/b.com"

ServerName www.b.com

ServerAlias b.com

ErrorLog "logs/b.com-error_log"

CustomLog "logs/b.com-access_log" common

</VirtualHost>


#httpd -t 

Syntax OK


# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]




客户端192.168.75.1访问:

http://www.a.com/  #不能访问

Apache配置虚拟主机_第4张图片

http://www.b.com/

wKioL1bBjEDTC8AmAAA-GP9GdlM585.jpg


本地192.168.75.131访问:

添加hosts文件

# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.75.131 www.a.com

192.168.75.131 www.b.com

192.168.75.131 slave0.localdomain.com 


# elinks --dump www.a.com

      a.com

 

 

# elinks --dump www.b.com

      b.com 


七、定位虚拟主机的访问权限,访问www.b.com需要用户名和密码

# cat virtual_host.conf 

#基于域名的的虚拟主机

NameVirtualHost 192.168.75.131:80

<VirtualHost 192.168.75.131:80>


ServerName www.a.com  

DocumentRoot "/www/a.com"

ServerAlias a.com

ErrorLog "logs/a.com-error_log"

CustomLog "logs/a.com-access_log" common


<Directory "/www/a.com">

Options none

AllowOverride none

Order deny,allow

Deny from 192.168.75.1

</Directory>



</VirtualHost>


<VirtualHost 192.168.75.131:80>

DocumentRoot "/www/b.com"

ServerName www.b.com

ServerAlias b.com

ErrorLog "logs/b.com-error_log"

CustomLog "logs/b.com-access_log" common



<Directory "/www/b.com">

Options none

AllowOverride authconfig

AuthType basic

AuthName "restrict area."

AuthUserFile "/etc/httpd/.htpasswd"

Require valid-user 

</Directory>

</VirtualHost>




添加密码文件

# htpasswd -c -m /etc/httpd/.htpasswd tom  #第一次创建需要用-c选项

New password: 

Re-type new password: 

Adding password for user tom

[root@slave0 conf.d]# htpasswd  -m /etc/httpd/.htpasswd jerry  #第二次创建不需要-c选项


# htpasswd  -m /etc/httpd/.htpasswd jerry

New password: 

Re-type new password: 

Adding password for user jerry




# httpd -t

Syntax OK

[root@slave0 conf.d]# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd:                                            [  OK  ]



访问:

http://www.b.com/  #需要提供用户名和密码

Apache配置虚拟主机_第5张图片

Apache配置虚拟主机_第6张图片






本文出自 “小鱼的博客” 博客,转载请与作者联系!

你可能感兴趣的:(apache,配置,虚拟主机)