apache虚拟主机的搭建

虚拟主机的介绍

虚拟主机

虚拟主机是指在同一台服务器上运行多个web站点,其中的每一个站点实际上并不占用整个服务器,可以充分利用服务器的硬件资源,节省成本。

类型

  • 基于域名   使用不同的域名,但是对应的ip和端口都是一样的
  • 基于IP     使用不同的域名,但是每个域名对应的ip不一样
  • 基于端口  使用不同的域名,但是ip一样,对应的端口不一样,用户访问的时候需要在域名后面指定端口号

三种类型虚拟主机的搭建

(测试环境www.beyondjie.com,bbs.beyondjie.com,由于实验环境没有DNS服务器,所以需要修改hosts文件)

基于域名

1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[[email protected]_Server conf]# catextra/httpd-vhosts.conf

NameVirtualHost 192.168.254.100:80

<VirtualHost 192.168.254.100:80>

ServerAdmin [email protected]

DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

ServerName www.beyondjie.com

ServerAlias web1

ErrorLog "logs/www.beyondjie.com-error_log"

CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

<VirtualHost 192.168.254.100:80>

ServerAdmin [email protected]

DocumentRoot "/usr/local/httpd-2.4.4/htdocs/bbs"

ServerName bbs.beyondjie.com

ErrorLog "logs/bbs.beyondjie.com-error_log"

CustomLog "logs/bbs.beyondjie.com-access_log" common

</VirtualHost>

2)编辑httpd.conf,支持虚拟主机

[[email protected]_Server conf]# echo"Include conf/extra/httpd-vhosts.conf" >>httpd.conf

[[email protected]_Server conf]# tail -1httpd.conf

Include conf/extra/httpd-vhosts.conf

3)创建对应的测试网页文件。重启Apache

[[email protected]_Server conf]# cd/usr/local/httpd/htdocs/

[[email protected]_Server htdocs]# rm -fr *

[[email protected]_Server htdocs]# mkdir www

[[email protected]_Server htdocs]# mkdir bbs

[[email protected]_Server htdocs]# echo"<h1>www.beyondjie.com</h1>" > www/index.html

[[email protected]_Server htdocs]# echo"<h1>bbs.beyondjie.com</h1>" > bbs/index.html

[[email protected]_Server htdocs]# catwww/index.html

<h1>www.beyondjie.com</h1>

[[email protected]_Server htdocs]# catbbs/index.html

<h1>bbs.beyondjie.com</h1>

重启apache

[[email protected]_Server conf]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:AH00548:NameVirtualHost has no effect and will be removed in the next release/usr/local/httpd-2.4.4/conf/extra/httpd-vhosts.conf:1

[确定]

重启提示NameVirtualHost has no effect and will be removed 意思是httpd-2.4.4版本不用使用NameVirtualHost。所以删掉即可(httpd-2.2.*需要)

在客户机上配置hosts解析,并测试

[[email protected] ~]# echo"192.168.254.100 www.beyondjie.com bbs.beyondjie.com" >>/etc/hosts

[[email protected] ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.combbs.beyondjie.com

[[email protected] ~]# elinks -dumpwww.beyondjie.com

www.beyondjie.com

[[email protected] ~]# elinks -dumpbbs.beyondjie.com

bbs.beyondjie.com

基于域名的虚拟主机创建成功

 

基于IP

1)本机两块网卡,ip如下

[[email protected]_Server ~]# ifconfig

eth0     Link encap:Ethernet  HWaddr 00:0C:29:F2:1A:88

inet addr:172.16.254.29  Bcast:172.16.254.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fef2:1a88/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

RX packets:17 errors:0 dropped:0 overruns:0 frame:0

TX packets:13 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:1637 (1.5 KiB)  TXbytes:1146 (1.1 KiB)

eth1     Link encap:Ethernet  HWaddr00:0C:29:F2:1A:92

inet addr:192.168.254.100  Bcast:192.168.254.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fef2:1a92/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

RX packets:33 errors:0 dropped:0 overruns:0 frame:0

TX packets:36 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:3865 (3.7 KiB)  TXbytes:4945 (4.8 KiB)

2)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[[email protected]_Server ~]# cat /usr/local/httpd/conf/extra/httpd-vhosts.conf

<VirtualHost 192.168.254.100:80>

ServerAdmin [email protected]

DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

ServerName www.beyondjie.com

ServerAlias web1

ErrorLog "logs/www.beyondjie.com-error_log"

CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

<VirtualHost 172.16.254.29:80>

ServerAdmin [email protected]

DocumentRoot "/usr/local/httpd-2.4.4/htdocs/bbs"

ServerName bbs.beyondjie.com

ErrorLog "logs/bbs.beyondjie.com-error_log"

CustomLog "logs/bbs.beyondjie.com-access_log" common

</VirtualHost>

3)测试文件上面已经创建,现在需要修改hosts文件

[[email protected] ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.com

172.16.254.29 bbs.beyondjie.com

4)重启apache服务,并测试

[[email protected]_Server ~]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:                                           [确定]

[[email protected] ~]# elinks -dump www.beyondjie.com

www.beyondjie.com

[[email protected] ~]# elinks -dumpbbs.beyondjie.com

bbs.beyondjie.com

基于ip的虚拟主机已经完成

基于端口

1)编辑/usr/local/httpd/conf/extra/httpd-vhosts.com文件

[[email protected]_Server ~]# cat /usr/local/httpd/conf/extra/httpd-vhosts.conf

<VirtualHost 192.168.254.100:80>

ServerAdmin [email protected]

DocumentRoot "/usr/local/httpd-2.4.4/htdocs/www"

ServerName www.beyondjie.com

ServerAlias web1

ErrorLog "logs/www.beyondjie.com-error_log"

CustomLog "logs/www.beyondjie.com-access_log" common

</VirtualHost>

<VirtualHost 192.168.254.100:8080>

[email protected]

DocumentRoot“/usr/local/httpd-2.4.4/htdocs/bbs”

ServerNamebbs.beyondjie.com

ErrorLog“logs/bbs.beyondjie.com-error_log”

CustomLog“logs/bbs.beyondjie.com-access_log” common

</VirtualHost>

2)测试文件上面已经创建,现在需要修改hosts文件

[[email protected] ~]# tail -1 /etc/hosts

192.168.254.100 www.beyondjie.combbs.beyondjie.com

3)编辑httpd.conf文件,加入监听的8080端口

Listen 80

Listen 8080

4)重启apache服务并测试

[[email protected]_Server ~]# service httpdrestart

停止 httpd:                                              [确定]

正在启动 httpd:                                           [确定]

[[email protected] ~]# elinks -dumpwww.beyondjie.com:80

www.beyondjie.com

[[email protected] ~]# elinks -dumpbbs.beyondjie.com:8080

bbs.beyondjie.com

基于端口的虚拟主机完成

总结:

1.三种虚拟主机类型的实现主要是修改<virtualhost  >里的参数。

2.基于域名的虚拟主机需要开启NameVirtualhost参数。其他两个用#注释掉即可。

但是注意:httpd-2.4.*版本不需要此参数

3.基于ip的虚拟主机至少需要两块网卡

4.基于端口的虚拟主机需要在httpd.conf中添加监听的端口

时间: 2024-08-27 01:14:17

apache虚拟主机的搭建的相关文章

Apache虚拟主机的搭建及相关问题解决

在开发的过程中,很多时候项目的部署都需要在本地进行虚拟服务器的模拟搭建,所以具体的配置流程为下,并且把自己遇到的问题跟大家分享. 1.Apache配置文件httpd.conf 找到   # Virtual hosts 这句前面的#不用去 //如果去掉了,你就会发现localhost和你设置的域名路径都会访问不了项目!!   #Include conf/extra/httpd-vhosts.conf 去掉本行注释 #  //这个是一定一定要去掉的!!! 还有 severname localhost

Apache虚拟主机搭建(单IP多网站服务器配置)

一些小型网站若一个网站放在一台服务器上会大量浪费服务器资源,如何在一个服务器上架设多个网站呢? 我们已经知道,架设一个网站需要http服务,安装Apache后,我们可以把我们网站的内容放入到/usr/local/apache2/htdocs/里面,启动httpd服务,设置好防火墙,就可以浏览我们的网站了,那么若是想要部署多个网站要怎么做呢?这就需要apache的一个vhost虚拟主机功能了.我们来用一个实验实现这个效果:计算机浏览器输入www.a.com 显示"I am A!",输入w

二、 搭建Apache虚拟主机

二. 搭建Apache虚拟主机 1.背景: 虚拟主机:一台web主机配置多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,或者是不同的IP. 假设网站的域名为:52linux.com,网站下面设有 http://www.52linux.com;: http://blog.52linux.com;: http://bbs.52linux.com: 三个站点,这样我们可以在一台apache主机上配置虚拟主机来实现. 通常虚拟主机分为3种: 基于域名,基于端口,基于IP,以及它们的混合

解决Apache虚拟主机报错问题apache AH01630: client denied by server configuration错误解决方法

今天同事咨询通过Apache搭建创建虚拟主机,搭建好发现一直报错,提示 "apache AH01630: client denied by server configuration",在网上搜索了一下, 发现这个错误的原因是,apache2.4 与 apache2.2 的虚拟主机配置写法不同导致. apache2.2的写法: [plain] view plain copy 在CODE上查看代码片派生到我的代码片 <VirtualHost *:80> ServerName f

Apache 虚拟主机 VirtualHost 配置

虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同的域名. Apache 是世界上使用最广的 Web 服务器, 从 1.1 版开始支持虚拟主机. 本文将讲解在不同服务器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虚拟主机来搭建多个网站. Redhat Enterpri

43.apache虚拟主机的使用

apache虚拟主机的使用 虚拟主机技术是使用一台服务器实现发布多个网站的技术. 实现一台服务器多个网站的方法有:基于网卡,基于端口,基于域名. 基于网卡:一台主机添加多块网卡,就相当于有多个IP 基于端口:一台主机通过同一个IP不同的端口 基于域名:一台主机通过同一个IP同一个端口不通的域名 Rpm版apache文件路径: 配置文件路径:/etc/httpd/conf/httpd.conf 网站根文件路径:/var/www/html/index.html 执行命令文件路径:/usr/sbin

Apache虚拟主机and虚拟端口

Apache虚拟目录1.在httpd子配置目录下创建一个以conf结尾的文件,并添加配置信息 2.修改httpd主配置文件vim /etc/httpd/conf/httpd.conf 3.创建虚拟目录,并且放入一个首页4.重启httpd服务,关闭防火墙5.在win上访问,虚拟目录 6.设置访问权限 修改虚拟目录的配置文件vim vdir.conf,重启服务7.创建一个httpd用户,切家目录放在/etc/httpd/user,进行测试 Apache 虚拟主机实验:相同IP搭建两个或者以上的网站1

Apache虚拟主机配置

在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录.Apache虚拟主机配置有3中方法:基于IP配置.基于域名配置和基于端口配置,这里介绍基于域名配置和基于端口配置,基于IP配置方法类似. 1. Apache基于域名配置虚拟主机: 打开Apache安装目录下的配置文件conf/extra/httpd-vhosts.conf,添加如下配置信息: <VirtualHost _default_:80> DocumentRoot &quo

Apache虚拟主机及别名配置

Apache虚拟主机配置 增加监听端口(修改conf/httpd.conf文件) Listen 127.0.0.1:8092 修改主站点权限(修改conf/httpd.conf文件) <Directory /> AllowOverride none # Require all denied </Directory> 允许虚拟主机配置(修改conf/httpd.conf文件) # Virtual hosts Include conf/extra/httpd-vhosts.conf 增