虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同的域名.

Apache 是世界上使用最广的 Web 服务器, 从 1.1 版开始支持虚拟主机. 本文将讲解在不同服务器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虚拟主机来搭建多个网站.

Redhat Enterprise Linux

Redhat Enterprise Linux (包括 CentOS Linux), 是使用最广的 Linux 服务器, 大量的网站应用都部署在其上.

1. 打开文件 /etc/httpd/conf/httpd.conf, 搜索 VirtualHost example, 找到代码如下:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/httpdocs/demo_neoease_com
ServerName demo.neoease.com
ErrorLog logs/demo.neoease.com-error.log
CustomLog logs/demo.neoease.com-access.log common
</VirtualHost>
3. 重启 httpd 服务, 执行以下语句.
service httpd restart
Ubuntu Linux
Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同.
1. 打开目录 /etc/apache2/sites-available/, 发现 default 和 default-ssl 两个文件, 其中 default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的. 可以复制一份 default 文件. 并修改配置文件名, 文件名必须与域名一致 (如: demo.neoease.com)
2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:
#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/httpdocs/demo_neoease_com
ServerName demo.neoease.com
ErrorLog ${APACHE_LOG_DIR}/demo.neoease.com-error.log
CustomLog ${APACHE_LOG_DIR}/demo.neoease.com-access.log combined
</VirtualHost>
3. 通过 a2ensite 激活虚拟主机配置
sudo a2ensite demo.neoease.com
4. 打开目录 /etc/apache2/sites-enabled/, 你会发现所有激活的虚拟主机, 可以通过 a2dissite 进行注销
sudo a2dissite demo.neoease.com
5. 重启 Apache 服务, 激活虚拟主机
sudo /etc/init.d/apache2 restart Windows
Windows 是市场占有率最高的 PC 操作系统, 也是很多人的开发环境. 其 VirtualHost 配置方法与 Linux 上有些差异, 以下方式适合原生 Apache, XAMPP 和 WAMP 套件.
1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件.
2. 仿照例子, 添加一段代码来指定某一域名的网站.
#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/workspace/php/demo_neoease_com"
ServerName demo.neoease.com
ErrorLog "logs/demo.neoease.com-error.log"
CustomLog "logs/demo.neoease.com-access.log" common
</VirtualHost>
3. 打开 httpd.conf 文件, 添加如下语句.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
4. 重启 Apache 服务.
Mac OS
近年苹果的雄起, 让 Mac 日催普及, 也成为很多开发人员的选择. 因为与 Linux 同源, 配置方法也相似.
1. 打开文件 /private/etc/apache2/extra/httpd-vhosts.conf.
2. 仿照例子, 添加一段代码来指定某一域名的网站.
#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/docs/httpdocs/demo_neoease_com"
ServerName demo.neoease.com
ErrorLog "/private/var/log/apache2/demo.neoease.com-error_log"
CustomLog "/private/var/log/apache2/demo.neoease.com-access_log" common
</VirtualHost> 3. 打开文件 /private/etc/apache2/httpd.conf, 搜索 Virtual hosts, 找到代码如下: # Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf 去掉前面的注释符号 #, 保存文件.
4. 重启 apache 服务, 执行以下语句.
sudo apachectl restart
wamp5下的虚拟主机的配置

今天配置了一下虚拟目录,以下是我的配置方法。

 1  编辑httpd.conf,查找Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉。
2 编辑httpd-vhosts.conf,我把WAMPServer安装在D:/wamp,所以我这里的路径是D:\wamp\Apache2\conf\extra。 把里面的内容清空掉或者注释,换成下面的内容: NameVirtualHost *:80 这个是新增的努力目录
<VirtualHost *:80>
ServerName www.cms.com
ServerAlias www.cms.com
DocumentRoot "F://workplace"
</VirtualHost> 原来默认的目录
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "D:/wamp/www"
</VirtualHost> 3 编辑httpd.conf,找到DocumentRoot "d:/wamp/www/"这项,这是默认根目录路径,但是要更改的不是这个,一直往下找,找到<Directory "d:/wamp/www">,然后在该</Directoory>后加上如下内容: <Directory "F://workplace"> Options Indexes FollowSymLinks AllowOverride all
Order Allow,Deny
Allow from all
</Directory> <Directory "D:/wamp/www">
Options Indexes FollowSymLinks
AllowOverride all
Order Allow,Deny
Allow from all
</Directory>
--------------------------example----------------------- <VirtualHost *:80>
DocumentRoot D:/ds/www
ServerName www.ds.com
<Directory "D:/ds/www">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost> 4 修改C:/WINDOWS/system32/drivers/etc/host这个文件,用记事本打开,加上如下内容:
127.0.0.1 www.cms.com 好了,然后重启apache,在浏览器里面输入www.cms.com,看看访问到的内容是不是workplace这个目录呢。

虚拟主机 (Virtual Host)的更多相关文章

  1. Apache 创建虚拟主机目录和设置默认访问页面

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...

  2. Apache 虚拟主机 VirtualHost 配置

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...

  3. centos LAMP第二部分apache配置 下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转 配置apache的访问日志 配置静态文件缓存 配置防盗链 访问控制 apache rewrite 配置开机启动apache tcpdump 第二十节课

    centos    LAMP第二部分apache配置  下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转  配置apache的访问日志  配置静态文件缓存  配置防盗链 ...

  4. NGINX : 如何屏蔽未被定义的虚拟主机的访问

    参考: [ how to prevent undefined server names ] nginx 的默认虚拟主机 Nginx 支持基于域名和端口的虚拟主机(virtual host), 根据获取 ...

  5. tomcat配置虚拟主机

    在眼下,非常多server都是一台server对外能够訪问非常多个javaEE的项目,这样的实现方式就须要在tomcat里面配置虚拟主机了!以下就说说怎样配置虚拟主机: 找到tomcat的安装文件夹, ...

  6. VPS,虚拟主机,云主机,独立服务器区别

    作者:张朝权链接:http://www.zhihu.com/question/25507629/answer/105594087来源:知乎著作权归作者所有,转载请联系作者获得授权.   独立服务器独立 ...

  7. 独立服务器 云主机、VPS以及虚拟主机三者之间的区别是什么?哪个更好?

    https://www.zhihu.com/question/21442353#answer-2442764 云主机(如 EC2,[1] )和 VPS (如 Linode,[2])都是完整的操作系统( ...

  8. Tomcat定义虚拟主机案例

    Tomcat定义虚拟主机案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备环境 1>.创建web程序的根目录 [root@yinzhengjie ~]# mkdir ...

  9. [svc]tomcat目录结构/虚拟主机/nginx反向代理cache配置

    tomcat目录文件 /usr/local/tomcat/bin/catalina.sh stop sleep 3 /usr/local/tomcat/bin/catalina.sh start to ...

随机推荐

  1. 第二章 Qt常用工具的介绍

    第二章 Qt常用工具的介绍 (1)No.1 qmake 相信编写过Makefile的开发人员,随着工程中源码的级数递增和以类型.功能.模块组织源码的子目录的增多,都不愿意重复机械地手工编写这个工程管理 ...

  2. QtSQL学习笔记(4)- 使用SQL Model类

    除了QSqlQuery,Qt提供了3个高级类用于访问数据库.这些类是QSqlQueryModel.QSqlTableModel和QSqlRelationalTableModel. 这些类是由QAbst ...

  3. ECshop网店系统百万级商品量性能优化-加快首页访问速度

    如果ECshop的商品数达到几万,十几万的时候,如果首页没有缓存,第一次访问的时候,你会发现其慢无比,原因就是清空了Cache后或者没有Cache的情况下,ECshop会Bulid一些Cache数据, ...

  4. UI表单

    Monk.UI表单美化插件诞生记!   阅读目录 背景 预览效果 表单组件 普通文本框 多行文本框 复选框 切换滑块 单选框 下拉选择框 数字输入框 时间选择 文件选择 显示文本 按钮 开源地址 背景 ...

  5. poj 1348 Computing (四个数的加减乘除四则运算)

    http://poj.org/problem?id=1348 Computing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  6. The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.

    Exception when executing ) br is a binary reader. The data to peak is D000 (D0=208) The cause is, fo ...

  7. GC的代机制

    1.代为CLR垃圾回收器采用的一种机制,基于以下几点假设:对象越新,生存期越短:对象越老,生存期越长: 2.托管堆仅支持3代(GC.MaxGeneration,第0代.第1代.第2代),CLR初始化会 ...

  8. hadoop-streaming 配置之---参数分割

    map: -D stream.map.output.field.separator=. 定义mapoutput字段的分隔符为. 用户可以自定义分隔符(除了默认的tab) -D stream.num.m ...

  9. Fckeditor漏洞利用总结

    查看编辑器版本FCKeditor/_whatsnew.html——————————————————————————————————————————————————————— —————— 2. Ver ...

  10. uva 1475 - Jungle Outpost

    半平面交,二分: 注意,题目的点是顺时针给出的: #include<cstdio> #include<algorithm> #include<cmath> #def ...