设为首页 收藏本站
查看: 1016|回复: 0

[经验分享] Apache配置虚拟主机

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-16 08:37:04 | 显示全部楼层 |阅读模式
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的虚机主机配置文件
1
2
3
4
5
6
7
8
9
10
# 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
1
2
<title>B.COM</title>
<h1>b.com </h1>




# cat /www/a.com/index.html
1
2
<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/

QQ截图20160216083447.png
http://192.168.75.141/



7、日志文件日志
如果没有单独为虚拟主机配置访问日志,和错误日志,则日志记录在默认位置
# ls /var/log/httpd/
access_log  error_log


三、添加基于端口的虚拟主机
1
2
3
4
5
6
7
8
9
10
11
12
# 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/
QQ截图20160216083504.png





四、添加基于域名的虚拟主机
1
2
3
4
5
6
7
8
9
10
11
# 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/
QQ截图20160216083520.png

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


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

# 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/  #不能访问
QQ截图20160216083539.png
http://www.b.com/
QQ截图20160216083549.png

本地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
[iyunv@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
[iyunv@slave0 conf.d]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]


访问:
http://www.b.com/  #需要提供用户名和密码
QQ截图20160216083558.png
QQ截图20160216083608.png




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-178536-1-1.html 上篇帖子: FreeBSD10.2安装apache22 下篇帖子: 解决一次由于SSL证书到期导致的网站不能访问的问题(Nginx,php,Apache) 虚拟主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表