Apache 虚拟主机 VirtualHost 配置

May 16th, 2011 Translate Add Comment

虚拟主机 (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 zhao.wuz@alibaba-inc.com
    DocumentRoot /var/www/httpdocs/demo_aliluna_com
    ServerName demo.aliluna.com
    ErrorLog logs/demo.aliluna.com-error.log
    CustomLog logs/demo.aliluna.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.aliluna.com)

2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin zhao.wuz@alibaba-inc.com
    DocumentRoot /var/www/httpdocs/demo_aliluna_com
    ServerName demo.aliluna.com
    ErrorLog ${APACHE_LOG_DIR}/demo.aliluna.com-error.log
    CustomLog ${APACHE_LOG_DIR}/demo.aliluna.com-access.log combined
</VirtualHost>

3. 通过 a2ensite 激活虚拟主机配置

sudo a2ensite demo.aliluna.com

4. 打开目录 /etc/apache2/sites-enabled/, 你会发现所有激活的虚拟主机, 可以通过 a2dissite 进行注销

sudo a2dissite demo.aliluna.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 zhao.wuz@alibaba-inc.com
    DocumentRoot "D:/workspace/php/demo_aliluna_com"
    ServerName demo.aliluna.com
    ErrorLog "logs/demo.aliluna.com-error.log"
    CustomLog "logs/demo.aliluna.com-access.log" common
</VirtualHost>

3. 打开 httpd.conf 文件, 添加如下语句.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4. 重启 Apache 服务.

总结

最近我在不同的几台服务器上搭建了一些网站服务, 这篇文章也算是我的个人笔记, 望日后可自用, 也希望对读者有用. 文中介绍了几个主流开发和部署环境上配置虚拟主机的方法, 其他 OS 大同小异.

声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: Apache 虚拟主机 VirtualHost 配置
  1. 最初的梦想 | May 16th, 2011 at 13:03 | #1

    不错的教程,收藏备用。

  2. IM路人 | May 16th, 2011 at 17:14 | #2

    收藏,这东西不懂~~

  3. Jav2me | May 16th, 2011 at 20:54 | #3

    收藏一下~

  4. Hivan | May 16th, 2011 at 21:27 | #4

    我很好奇,你的代码行是用css实现的前边有数字标识还是用的插件?

  5. mg12 | May 16th, 2011 at 21:31 | #5

    @IM路人
    不懂你还收藏. 汗...

    @Hivan
    插件.

  6. Hivan | May 16th, 2011 at 21:34 | #6

    OK,了解了!看到你的css文件了,是wp-Syntax对么?

  7. mg12 | May 16th, 2011 at 21:37 | #7

    @Hivan
    是的. 这个插件虽然有点丑, 功能少. 但因为它使用 table, 所以在 Feed 里面也可以完好地显示. 而且能通过 W3C 认证.

  8. Hivan | May 16th, 2011 at 21:39 | #8

    @mg12 嗯嗯,了解了...话说真不亏是前端的高手,你的博客有很多细节上的东西需要研究呢!我那天就研究了半天你的字体设置,看着很舒服!并且貌似你每次发的文结构都很好...不像我写博客,文章结构很乱!有的时候会乱添加代码,而不是想到用CSS!

  9. loosky | May 16th, 2011 at 23:24 | #9

    很不错哦。还可以介绍一下nginx的,现在用这个的貌似也很多。

  10. wmtimes | May 17th, 2011 at 00:53 | #10

    我都是用绿色套装的,很少自己配置

  11. 邓肯 | May 17th, 2011 at 09:25 | #11

    看过博主的照片 哈哈 过来SEE下

  12. Bolo | May 17th, 2011 at 10:23 | #12

    把服务器上所有网站的设置都写在同一个文件太乱了,可以用

    Include conf/virtualhost/*

    来载入 apache/conf/virtualhost 目录下所有的网站配置文件

  13. 滑动门 | May 18th, 2011 at 10:27 | #13

    这个服务器没怎么接触过,我先了解一下吧,谢谢诶博主分享了。

  14. 自在 | May 18th, 2011 at 14:58 | #14

    前段时间上课时才刚做完站点配置的实验~XD

  15. IM路人 | May 20th, 2011 at 07:06 | #15

    @mg12
    好吧,冲着这句话,我还真想了解下,怎么说哥以前专业也是学这个的~~

  16. aso | May 20th, 2011 at 11:19 | #16

    写的很认真,nginx不知有总结没,可以出一版来

  17. maple's sky | May 20th, 2011 at 23:57 | #17

    留意一下。。

  18. yun77op | May 24th, 2011 at 10:24 | #18

    好像要加

    blahblah

    不然会403Forbidden错误

  19. 求索阁 | May 26th, 2011 at 21:41 | #19

    博主用的哪款本地集成化软件包(比如:apperserv)来测试wordpress的啊?

  20. DVD | May 27th, 2011 at 14:00 | #20

    tag cloud的实际意义何在?

  21. tony | May 27th, 2011 at 20:51 | #21

    我用的主机,只要一发外链就死机,网站打不开,是什么原因啊?

  22. 山野愚人 | Jun 9th, 2011 at 11:32 | #22

    当初我折腾这个折腾好久……

  23. 啊花 | Jun 9th, 2011 at 15:11 | #23

    博主可否共享下现在的主题 啊

  24. ruirui | Jun 9th, 2011 at 18:02 | #24

    我比较喜欢这个主题, 相当简洁大方

  25. yywr | Jun 11th, 2011 at 11:01 | #25

    很实用,适合我这样的~~

  26. mg12 | Jun 12th, 2011 at 18:13 | #26

    @求索阁
    其实都一样的, 最好还是自己搭建, PHP+Apache+MySQL 是老搭配, 配起来很方便的. 如果你实在懒得自己弄, 用 套件的话, XAMPP 和 WAMP 区别也不大.

    @DVD
    Tag Clond 是一种索引方式.

    @啊花
    Sorry, 不行.

    @yywr
    适合就好.

  27. 欧雷 | Jun 29th, 2011 at 00:50 | #27

    我刚刚接触wordpress,请问能否留个即时通讯的方式,方面向您请教问题?

  28. 激光转速表 | Jul 2nd, 2011 at 14:54 | #28

    @求索阁
    其实都一样的, 最好还是自己搭建, PHP+Apache+MySQL 是老搭配, 配起来很方便的. 如果你实在懒得自己弄, 用 套件的话, XAMPP 和 WAMP 区别也不大.

    同意!!

  29. 1freehost | Jul 10th, 2011 at 14:47 | #29

    不是太明白意思……太技术了,要学习的还很多

  30. 丢宝网 | Jul 12th, 2011 at 09:52 | #30

    学习了,很有帮助啊

  31. beginsun | Jul 12th, 2011 at 19:03 | #31

    O(∩_∩)O~来看看

  32. VGA采集卡 | Jul 13th, 2011 at 16:55 | #32

    保存下来真不错

  33. 打死算了 | Jul 25th, 2011 at 16:38 | #33

    Apache 是我在 Linux 下玩儿最熟悉的东西,无鸭梨。

  34. 付延溢 | Oct 17th, 2011 at 14:15 | #34

    centos下不涉及到hosts文件吗?

  1. Loading...