一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Apache中配置虚拟主机具体说明

时间:2013-07-16 编辑:简简单单 来源:一聚教程网

在这里,我只介绍一种简单也比较常用的配置虚拟主机的方法。就是基于名称的虚拟主机配置:
第一步:
在conf文件中的httpd.conf中找到

 代码如下 复制代码

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

然后把上面的改为:

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

第二步:

在conf/extra文件下找到httpd-vhosts.conf,在最后面添加如下代码:

# www.xxxx改为你想要的任意域名

 代码如下 复制代码

ServerName www.111com.net

#这里是你的放网址的文件夹地址。。。

 代码如下 复制代码

DocumentRoot “D:/www/php”
DirectoryIndex index.html index.htm index.php

Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all


第三步:

在C:/windows中搜hosts文件,然后添加
127.0.0.1 www.111com.net

第四步:

重启Apache!看到没,配置虚拟主机就是这么简单,这些都是经过本人亲自测试过的,所以可以放心使用。

或许有些同学会出现一些问题,如我配置完上面的虚拟主机之后,www.111com.net 可以正常访问,但是http://localhost反而挂了,不能访问了。怎么办呢?不用急,零星人帮你解决问题。

解决方法如下:

在conf/extra文件下找到httpd-vhosts.conf,在最后面再添加一段代码:

 代码如下 复制代码

ServerName http://localhost
DocumentRoot “D:/www”
DirectoryIndex index.html index.htm index.php

Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all


然后在C:/windows中搜hosts文件,再添加一句
127.0.0.1 localhost

重启Apache!!!

其它操作系统中配置方法


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.
#
#
#    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
#

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

 代码如下 复制代码
#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#

    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

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 指向的域名一致
#

    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

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 安装目录}confextra, 找到 httpd-vhosts.conf 文件.

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

 代码如下 复制代码
#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#

    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

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

 代码如下 复制代码
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4. 重启 Apache 服务.

热门栏目