虚拟主机 (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

Apache 虚拟主机 VirtualHost 配置的更多相关文章

  1. Windows下Apache 虚拟主机 VirtualHost 配置

    以下方式适合原生 Apache, XAMPP 和 WAMP 套件 1.修改Apache配置文件(httpd.conf),如下: # Virtual hostsInclude conf/extra/ht ...

  2. Nginx 虚拟主机 VirtualHost 配置

    Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...

  3. apache 虚拟主机详细配置:http.conf配置详解

    apache 虚拟主机详细配置:http.conf配置详解 Apache的配置文件http.conf参数含义详解 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd. ...

  4. Apache虚拟主机&伪静态配置

    Apache基本操作 安装:yum install httpd 启动:systemctl start httpd 查看进程:ps -ef | grep httpd 查看端口:sudo netstat ...

  5. Apache虚拟主机的配置

    虚拟主机的配置 基于IP地址的虚拟主机配置Listen 80DocumentRoot /www/example1ServerName www.example1.comDocumentRoot /www ...

  6. 【转】Apache虚拟主机的配置和泛解析域名的绑定

    基于IP地址的虚拟主机配置 Listen 80 DocumentRoot /www/example1 ServerName www.example1.com DocumentRoot /www/exa ...

  7. Apache虚拟主机(vhost)配置教程

    使用apache来配置虚拟主机,在单一系统上运行多个网站. 现在很多linux主机使用apache作为web服务器的,大部分是基于这个原理来配置虚拟主机的. 下面就windows下以apache 2. ...

  8. apache 虚拟主机的配置

    一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhost root]# ifconfig et ...

  9. CentOS中Apache虚拟主机(virtualHost)设置在/home目录下的若干问题

    在Ubuntu中安装LAMP是非常简单的意见事情.但是在CentOS中却遇到了很多问题. 首先是CentOS中必须手动配置iptables,把80端口开放出来,不然,是访问不到的,开放80端口在/et ...

随机推荐

  1. Java源代码分析与生成

    源代码分析:可使用ANTLRANTLR是开源的语法分析器,可以用来构造自己的语言,或者对现有的语言进行语法分析. JavaParser 对Java代码进行分析 CodeModel 用于生成Java代码 ...

  2. sea.js,spm学习

    安装spm 下载sea.js 运行spm npm install spm@2.x -g npm install spm-build -g 下载sea.js git clone https://gith ...

  3. 全面理解.htaccess语法中RewriteCond和RewriteRule意义

    RewriteCond的语法 RewriteCond TestString CondPattern [Flags]其中的TestString是指一个文本格式的条件,例子中用的是环境变量名HTTP_HO ...

  4. 在同一台机器上让Microsoft SQL Server 2000/ SQL2005/ SQL2008共存

    可能很多朋友都遇到想同时在自己的机器上运行Microsoft SQL Server 2000以及Microsoft SQL Server 2005和Microsoft SQL Server 2008. ...

  5. hadoop1中mapreduce原理详解

    剖析Mapreduce作业运行机制:原理如下图: 原理图的解释的可以分为以下几个部分 1.客户端提交一个mapreduce的jar包给JobClient 2.JocClient通过RPC和JobTra ...

  6. UIlabel - 富文本属性

    1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...

  7. client denied by server configuration

    http://blog.csdn.net/fdipzone/article/details/40512229

  8. 【POJ1743】 Musical Theme (二分+后缀数组)

    Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)note ...

  9. win7+ubuntu双系统安装攻略

    一1.下载分区软件,为ubuntu安装分出一个区 2.磁盘管理器,选中该区,右键,删除卷,该区变为绿色,成为空闲区 3.成功 二为ubunt添加开机导引项 1,安装好easybcd2.0后,启动软件: ...

  10. 【Xamarin挖墙脚系列:应用的性能调优】

    原文:[Xamarin挖墙脚系列:应用的性能调优] 官方提供的工具:网盘地址:http://pan.baidu.com/s/1pKgrsrp 官方下载地址:https://download.xamar ...