`
haohao-xuexi02
  • 浏览: 208319 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

设置一个虚拟主机在Apache

    博客分类:
  • php
阅读更多

在Apache设置虚拟主机

Setting up a virtual host in the Apache web server is not exactly a PHP topic, but many PHP developers use the Apache web server to test web pages on their development machine.建立一个在Apache Web服务器的虚拟主机是不完全是一个PHP的话题,但是许多PHP开发人员使用Apache Web服务器,以测试他们的开发机器上的网页。

There is a lot of information around on how to do this, but the first time I tried it, I found the existing information to be more confusing than helpful.有一个要做到这一点很多资料市内如何,但我第一次试了一下,我发现了现有的信息更有助于混乱比。 Hopefully, this page will simplify the process a bit.我们希望,此网页将简化程序一点。 Please note that this information pertains to setting up a virtual host in Apache on a Windows machine for use as a local testing server.请注意,此信息关于设立作为测试服务器上的本地虚拟主机在Apache上使用的Windows机器。 Setting up a virtual host for an actual production server is beyond the scope of this article and you should refer to the official Apache documentation for that.实际生产中设置一个虚拟主机服务器超出了本文的范围,这一点,你应该参考官方Apache文件这样做的。

Configuring Apache配置Apache

The first file we'll need to edit is the Apache httpd.conf file.第一个文件,我们需要修改是Apache httpd.conf文件。 If you installed the Apache software using the download from the Apache web site , you should have a menu item that will open this file for editing.如果你安装了Apache软件下载使用从Apache网站 ,你应该有一个菜单项,将打开该文件进行编辑。 Click Start->Programs->Apache HTTP Server->Configure Apache Server->Edit the Apache httpd.conf Configuration File.单击开始->“程序-> Apache的HTTP服务器>”配置Apache服务器->“编辑Apache的httpd.conf配置文件。 If you don't have that start menu item, start your text editor and open the file.如果你没有这样的启动菜单项,启动文本编辑器并打开该文件。 It will be in a sub-folder named conf of your Apache folder.这将是在一个子文件夹命名为你的Apache conf文件夹。 For example, mine is here:例如,我是在这里:

C:\Program Files\Apache Group\Apache\conf\httpd.conf

Notes for Apache Server Versions Since 2.2为Apache Server版本说明自2.2

Configuration配置

Note that Apache changed the preferred method for configuring the Apache server with the release of Apache 2.2.请注意,改变了Apache的配置与2.2版本的Apache Apache服务器的首选方法。 For versions beginning with 2.2, the peferred configuration is more modular.对于版本2.2开始,peferred配置更模块化。 Setting up a virtual host as described here will still work with the newer versions, but to follow the modular approach, the editing of httpd.conf is only to uncomment (remove the # from the beginning of the following line:建立一个虚拟主机描述这里将仍然使用较新的版本,而是要按照模块化的方式,编辑httpd.conf的唯一的注释(删除#从以下行的开头:

#Include conf/extra/httpd-vhosts.conf

Everything else is entered in the file httpd-vhosts.conf , which will be located in the extra folder below the below the folder containing httpd.conf .其他一切都在文件中输入httpd-vhosts.conf ,这将是位于extra包含文件夹下面的文件夹下httpd.conf As mentioned, the method described here will still work.如前所述,这里描述的方法仍然可以工作。

Security安全

Version 2.2 also changed some of the default security configuration parameters. 2.2版本还改变了一些默认的安全配置参数。 To set things up the way you'll need them, you'll need to add the following block to either your httpd.conf file, just above the virtual hosts, or to your httpd-vhosts.conf file:要设置方式,您将需要他们的东西,你需要添加以下块要么你的httpd.conf文件,上面的虚拟主机,或到您的httpd - vhosts.conf文件:

<Directory "C:\ My Sites "> <目录"C:\我的网站">
  Order Deny,Allow为了拒绝,允许
  Allow from all允许从所有
</Directory> </目录>
The above assumes you're using the directory structure described below.上述假设您正在使用的目录结构如下所述。 Adjust that as necessary to reflect your actual directory.调整是必要的,以反映实际的目录。

Now, for this example, we'll assume that you have your web sites located in a folder on your C drive called My Sites.现在,在这个例子中,我们将假设你有你的网站在C盘上的一个文件夹叫我的网站。 Each web site has a sub-folder of its own under that folder, like this:每个网站有它自己的子下像这样的文件夹,文件夹:

   C:\My Sites\Site1 ç:\我的网站\站点1
   C:\My Sites\Site2 ç:\我的网站\站点2

Say also, for this example, that the domains for the two sites are site1.com and site2.com.也说,在这个例子中,这两个网站的域名是site1.com和site2.com。 We're going to set up virtual hosts for those two sites using the domain names site1.local and site2.local.我们要成立这两个使用域名site1.local和site2.local网站的虚拟主机。 That way, you'll be able to tell at a glance whether you're looking at the live site, or your testing site.这样,您就可以一眼就能知道您是否正在寻找在现场站点或您的试验场。

In reality, you can call the domains anything you want.在现实中,你可以调用任何你想要的域名。 You could just as easily name them microsoft.monkeybutt and ibm.greentambourine.你可以很容易地把它们命名为microsoft.monkeybutt和ibm.greentambourine。 I choose to use the convention of using the same domain name along with the .local TLD to simplify and minimize the typing needed to switch between the live site and the testing site.我选择使用一起使用。本地顶级域名相同的域名,简化和减少所需的生活之间切换站点和测试站点键入该公约。 The only important point, and it's really important, is that you NEVER use an actual, real, live domain name.唯一重要的一点,这真的很重要,是你千万不要用实际的,真正的,活的域名。 If you used, for example, site1.com for the local virtual host, you would never be able to actually reach the live site.如果你使用,例如,主机site1.com本地虚拟的,你将永远无法真正到达现场直播。 All requests for the live site would be re-directed to your local virtual host.该活动站点的所有请求将被重新导向到您的本地虚拟主机。

Go to the very bottom of your httpd.conf file in your text editor.到最底层的httpd.conf文件在文本编辑器。 You should see an example of a virtual host there.你应该看到一个虚拟主机有例子。 Each line of that example will begin with an octothorpe (#).每个生产线将于这个例子用井号(#)。 The octothorpe character marks the line as a comment, so the example is not executed.该井号字符标记为一个注释行,所以这个例子是不会被执行。 Add the following lines below that example:下面这个例子中,添加以下几行:

NameVirtualHost 127.0.0.1 NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1> <VirtualHost 127.0.0.1>
   DocumentRoot "C:\My Sites\Site1"的DocumentRoot的“C:\我的网站\站点1”
   ServerName site1.local服务器名site1.local
</VirtualHost> </虚拟主机>

<VirtualHost 127.0.0.1> <VirtualHost 127.0.0.1>
   DocumentRoot "C:\My Sites\Site2"的DocumentRoot的“C:\我的网站\站点2”
   ServerName site2.local服务器名site2.local
</VirtualHost> </虚拟主机>

That's all you need to do!这就是你需要做的! Save and close the file.保存并关闭该文件。 That will tell the Apache server everything it needs to know in order for it to serve the pages using the domain names site1.local and site2.local.这将告诉Apache服务器它需要的一切,为了知道它的网页服务使用的域名site1.local和site2.local。 One note is that, in the above example, we have a space in the path.一个值得注意的是,在上述例子中,我们有一个路径中的空间。 Because of that, we put quotation marks around the document root directory.正因为如此,我们把周围的文档根目录引号。 If the path does not have any spaces in it, do not quote the path.如果路径不包含任何空格,不要引用路径。 If the directory used for your sites were, for example MySites instead of My Sites, the document root line would look like this instead:如果您的网站使用的目录中一样,我的地盘,而不是如MySites,文件根线,而不是看起来像这样:

  DocumentRoot C:\MySites\Site1 DocumentRoot的ç:\ MySites \站点1 

Resolving the DNS issue解决DNS问题

Obviously, if you typed http://site1.local in your browser, it would not be found by your Internet provider's DNS server.显然,如果你输入你的浏览器http://site1.local,也不会找到你的互联网服务提供商 DNS服务器。 We're next going to edit another file to work around that.我们下一步将要编辑另一个文件,以解决这一问题。 The second file you need to edit is called hosts, with no file extension.第二个文件,你需要编辑的称为主机, 没有文件扩展名。 It is a Windows system file and it will enable you to enter specific addresses for specific domains instead of using a DNS lookup.这是Windows系统文件,它将使你能够进入,而不是使用DNS查找特定领域的具体地址。 The normal location for this file is:此文件的正常位置是:

C:\WINNT\system32\drivers\etc\hosts

or

C:\Windows\system32\drivers\etc\hosts

If you don't find it there, do a search in your windows directory for the word hosts in the file name.如果你没有找到它,不要在文件名中的字承载在Windows目录中搜索。 The file you want is called hosts, with no file extension.你想要的是该文件称为主机,没有文件扩展名。 The correct file will begin with the following lines:正确的文件将开始与以下几行:

# Copyright (c) 1993-1999 Microsoft Corp. #版权所有(c)1993-1999微软
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows. #这是一个HOSTS文件由Microsoft TCP / IP的Windows使用的样本。
		

Once again, in this file, the octothorpe character is a comment marker.再次,在这个文件中,井号字符是注释标记。 Lines beginning with it are comments.线,它开始是注释。 In all likelihood, there will be nothing there, except for comments.在所有的可能性,会有什么都没有,除了征求意见。 If there are any other non-commented entries, leave them alone.如果有任何其他非注释的条目,让他们孤独。 Just go to the bottom of the file, below all the comments and any existing entries and add the following two lines:只要到该文件底部,下面所有的意见和任何现有的条目,并添加下面两行:

127.0.0.1 site1.local 127.0.0.1 site1.local
127.0.0.1 site2.local 127.0.0.1 site2.local 

That's all you need to do there.这就是你需要做什么。 Save and close the hosts file.保存并关闭该主机文件。

You're almost done!您快完成了! The only remaining thing you need to do is to re-start the Apache server.剩下的唯一的事情,你需要做的是重新启动Apache服务器。 You need to do this because Apache only reads the configuration file when it first starts up.你需要这样做,因为Apache只能读取配置文件时,第一次启动。 Click Start->Programs->Apache HTTP Server->Control Apache Server->Restart.单击开始->“程序-> Apache的HTTP服务器>”控制Apache服务器->“重新启动。 If you don't have that menu item, open a command prompt and change to the Apache directory, and type the following command and press the Enter key:如果你没有这样的菜单项,打开一个命令提示符并切换到Apache的目录,然后键入以下命令,然后按下回车键:

apache -w -n "Apache" -k restart

You should see a message like this:你应该看到这样的信息:

The Apache service is restarting.
The Apache service has restarted.

That's it!就是这样! You're done!大功告成! Close the command window and start your web browser.关闭命令窗口,并启动Web浏览器。 In the browser's address bar, type http://site1.local and hit the Enter key.在浏览器的地址栏中,键入http://site1.local,然后按下回车键。 You should now see your local copy of your site1.您现在应该可以看到你对你的站点1的本地副本。

Okay, now I'll mention one very small, but possibly important, caveat.好吧,现在我将提到一个非常小的,但也可能很重要,告诫。 When you create the virtual hosts like this, the default http://localhost will no longer work.当您建立这样的虚拟主机,默认http://localhost的将不再起作用。 In many cases, that is unimportant.在许多情况下,这是不重要的。 However, if you're using something like phpMyAdmin, you'll still need it.不过,如果你使用像phpMyAdmin的东西,你还是会需要它。 The solution to that is to create one additional virtual host called "localhost" that points to the original Apache htdocs folder.该解决方案是创建一个额外的虚拟主机所谓的“localhost”,为原来的Apache的htdocs文件夹点。 It might look something like this:它看起来像这样:

 <VirtualHost 127.0.0.1> <VirtualHost 127.0.0.1>
   DocumentRoot C:\Apache\htdocs DocumentRoot的ç:\的Apache \ htdocs中
   ServerName localhost服务器名本地主机
</VirtualHost> </虚拟主机>
	

Don't forget to include that additional virtual host when you edit the Windows hosts file.不要忘记,包括额外的虚拟主机,当您编辑Windows主机文件。

Note that there are other optional settings you can use to configure the virtual host.请注意,还有其他可选的设置,你可以用它来配置虚拟主机。 The above uses only two lines and that's all that's really necessary.以上仅使用两行,这一切,实在是很必要的。 You can read about other options in the Apache documentation .你可以读到其他选项Apache文档 Note that this link is to the Apache web site and it will

原文转载:apptools.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics