本地架设服务器设置多个虚拟主机

By | 2009/08/26

     很多网站项目都要在本地测试,再放到服务器上去。 所以在本地架设AMP服务器是很有必要的,我想只要有点基础的朋友,都知道应该怎么去架设。 但很多时候有多个网站在本也测试时,我想很多朋友都会把网站放在根目录下的不同文件夹中。这样一来网站的地址就会目录,要我们直接放上服务器上有区别,本地文件上传到服务器时还要修改一些设置。

    有没有想过,在本地测试时,我们就用不同的测试主机,这样对网站来说都在根目录下面。会少了很多网站上传后的目录方面的麻烦。 要解决这个问题,就要在本地架设多个虚拟主机。

   本架设多个虚拟主机其实也很简单,只要把Apache配置一下就行了

   我们知道Apache的配置都是在httpd.conf文件设置。我们在文件的最后加上以下代码:

<VirtualHost *:80>

 ServerName 192.168.1.85

 ServerAlias 192.168.1.85

 DocumentRoot "D:/www/htdocs/85"

<Directory "D:/www/htdocs/85">

 Options All ExecCGI FollowSymLinks Includes IncludesNOEXEC Indexes MultiViews

 DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.shtml index.aspx default.aspx  

 AllowOverride All 

 Order Deny,Allow 

 Allow from all 

 RewriteEngine On

</Directory>

</VirtualHost>

    <VirtualHost *:80></VirtualHost>就是配置一个虚拟主机,里面设置服务的地址和目录还有一些权限参数,重启Apach设置就生效了。我们就可以通过上面的设置 ServerName来访问这个虚拟主机,本例中是 http://192.168.1.85

    从上面的例子我们可以看到,本地可以设置多个虚拟主机,只要这个URL能访问到本志的服务器就行了,因为解析权到Apach后,它会把不同的URL地址转到不同的目录中去Localhost127.0.0.1,本地局域网的IP都可以设置不同的虚拟主机,其它我们还可以通过修改windows的Hosts文件使用任意域名。 Hosts文件一般都在%systemroot%\system(32)\drivers\etc里面,打开文件可以看到:

#

# 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



127.0.0.1       localhost

 

    在后面加入想要的域名就可以了,如你想通过xxxx.com访问本地的主机只要加入:

127.0.0.1    xxxx.com

  

   Mac os 中也有hosts文件 可以参考: 修改Mac OS 中的Hosts文件

  

    有很多服务器软件可以直接在图形化界面下设置虚拟主机,原理是一样的。推荐几个好用的服务软件

    windows    AMPServ

    Mac OS     MAMP
 

One thought on “本地架设服务器设置多个虚拟主机

  1. Pingback: APMServ修改HOSTS文件的BUG - 登高望远

Comments are closed.