Apache虚拟主机基本概念
Apache的虚拟主机功能允许在同一台物理服务器上托管多个网站,每个虚拟主机可绑定独立域名并指向特定目录。当用户访问这些域名时,Apache会自动打开对应目录中的内容。配置虚拟主机需修改Apache的配置文件。由于英文名为virtual host,配置文件可能是httpd.conf,也可能是与virtual host相关的文件如vhost.conf,具体取决于安装环境。
以下以新浪微博虚拟机开发为例,介绍具体的配置步骤及说明。
启用模块与加载虚拟主机配置
第一步:由于后续虚拟机中需要使用Rewrite功能,首先编辑Apache conf目录下的httpd.conf文件(可根据实际需求调整)。
添加mod_rewrite.so模块支持。请移除下列行前面的#号注释符号。
LoadModule rewrite_module modules/mod_rewrite.so
第二步:配置Apache支持虚拟机。这一步至关重要。
复制代码 代码如下: # Virtual hosts #Include conf/extra/httpd-vhosts.conf
找到上述相关文本,去掉#Include conf/extra/httpd-vhosts.conf前的#号。
复制代码 代码如下: # Virtual hosts Include conf/extra/httpd-vhosts.conf
保存并退出该文件。
定义虚拟主机域名映射
第三步:编辑conf目录下extra文件夹中的httpd-vhosts.conf文件。
移除原有的实例配置,添加新的配置项。以新浪微博为例,配置文件内容如下:
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any block.
#
#管理员邮箱
ServerAdmin jiangfeng3@staff.sina.com.cn
#项目根目录
DocumentRoot "D:/htdocs/frame_export"
#域名
ServerName test.t.sina.com.cn
#别名
ServerAlias test.t.sina.com.cn
#错误日志路径
ErrorLog "logs/test.t.sins.com.cn-error.log"
CustomLog "logs/test.t.sins.com.cn-access.log" common
RewriteEngine on
#重写规则,可根据实际需要添加
RewriteRule ^/(.*)$ /apps/index.php [L]
配置本地Hosts文件解析
第四步:编辑本地hosts文件,此处以Windows系统为例。
进入路径C:/Windows/System32/drivers/etc。
使用记事本打开hosts文件。
在文件末尾添加以下内容。
127.0.0.1 localhost 127.0.0.1 test.t.sina.com.cn
示例效果如图所示:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
127.0.0.1 test.t.sina.com.cn
保存并退出hosts文件。
第五步:重启Apache服务,然后在浏览器中输入http://test.t.sina.com.cn进行测试。
关于Apache虚拟机服务器配置的介绍就到这里,希望对大家有所帮助!