php 如何正确使用 Wamp Server 设置虚拟主机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22217386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:44:26  来源:igfitidea点击:

How to setup virtual host using Wamp Server properly?

phpapachewampserver

提问by Jerielle

I need your help here in creating a virtual host in wampserver. In office I can create a virtual hosts but when I try to create in my laptop it doesn't work. I still can't figure out what's wrong. Here's what I did.

我需要你的帮助来在 wampserver 中创建一个虚拟主机。在办公室我可以创建一个虚拟主机,但是当我尝试在我的笔记本电脑中创建它时它不起作用。我仍然无法弄清楚出了什么问题。这就是我所做的。

  1. I copy the wordpress file in this folder. This will be the path of my project

    • E:\Subversion\chelle.wpblog.local
  2. I edit the host file

    • C:\Windows\System32\drivers\etc\hosts
    • I added it to the end of file

      127.0.0.1 chelle.wpblog.local

  3. Next is I enable the virtual host in Apache

    • C:\wamp\bin\apache\Apache2.4.4\conf\httpd.conf
    • I uncomment this

      Include conf/extra/httpd-vhosts.conf

  4. Next is I setup the virtual host in WAMP

    • C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf
    • I add this at the bottom

      <VirtualHost *:80>
          ServerName chelle.wpblog.local
          ServerAlias chelle.wpblog.local
          DocumentRoot "E:/Subversion/chelle.wpblog.local/"
          <Directory "E:/Subversion/chelle.wpblog.local/">
              Options Indexes FollowSymLinks MultiViews
              AllowOverride All
              Order allow,deny
              allow from all    
          </Directory>
      </VirtualHost>
      
  5. Last is restart wampserver and open the chelle.wpblog.local in the browser. And it doesn't display. It display only google search results.

  1. 我将wordpress文件复制到这个文件夹中。这将是我项目的路径

    • E:\Subversion\chelle.wpblog.local
  2. 我编辑主机文件

    • C:\Windows\System32\drivers\etc\hosts
    • 我将它添加到文件末尾

      127.0.0.1 chelle.wpblog.local

  3. 接下来是我在Apache中启用虚拟主机

    • C:\wamp\bin\apache\Apache2.4.4\conf\httpd.conf
    • 我取消注释这个

      包括 conf/extra/httpd-vhosts.conf

  4. 接下来是我在 WAMP 中设置虚拟主机

    • C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf
    • 我在底部添加这个

      <VirtualHost *:80>
          ServerName chelle.wpblog.local
          ServerAlias chelle.wpblog.local
          DocumentRoot "E:/Subversion/chelle.wpblog.local/"
          <Directory "E:/Subversion/chelle.wpblog.local/">
              Options Indexes FollowSymLinks MultiViews
              AllowOverride All
              Order allow,deny
              allow from all    
          </Directory>
      </VirtualHost>
      
  5. 最后是重启 wampserver 并在浏览器中打开 chelle.wpblog.local。而且不显示。它只显示谷歌搜索结果。

回答by Sachin

I was installing the zend framework on my local wamp using apache server. First go and decide what will be your domain name for the local url. Ex->zend_tutorialthen go and open the file located at "C:\WINDOWS\system32\drivers\etc\"

我正在使用 apache 服务器在本地 wamp 上安装 zend 框架。首先决定本地 url 的域名是什么。 Ex->zend_tutorial然后打开位于“C:\WINDOWS\system32\drivers\etc\”的文件

hosts

主机

write

127.0.0.1 (use one tab space) zend_tutorial

127.0.0.1(使用一个制表符空间)zend_tutorial

then go to the folder located at

然后转到位于的文件夹

D:\wamp\bin\apache\Apache2.2.17 (whatever is your version) \conf\

D:\wamp\bin\apache\Apache2.2.17(不管你的版本是什么)\conf\

and open the file

并打开文件

httpd.conf

配置文件

and search for text

并搜索文本

Include conf/extra/httpd-vhosts.conf

包括 conf/extra/httpd-vhosts.conf

and uncomment it by removing the # tag from the start of the line.Save the file and close it. Now go to another folder located at

通过从行的开头删除 # 标记来取消注释。保存文件并关闭它。现在转到位于的另一个文件夹

D:\wamp\bin\apache\Apache2.2.17\conf\extra

D:\wamp\bin\apache\Apache2.2.17\conf\extra

and open the file

并打开文件

httpd-vhosts.conf

httpd-vhosts.conf

and paste the code below at the last in this file

并将下面的代码粘贴到此文件的最后

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "D:\wamp\www"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName zend_tutorial
   DocumentRoot "D:\wamp\www\(your project folder name)\public"
    SetEnv APPLICATION_ENV "development"
    <directory "D:\wamp\www\(your project folder name)\public">
        DirectoryIndex index.php
        AllowOverride all
        Order Allow,Deny
        Allow from all
    </directory>
</VirtualHost>

and restart the wamp, now write the zend_tutorialin the browser and you will see the things working.

并重新启动 wamp,现在在浏览器中编写zend_tutorial,您将看到一切正常。

回答by RiggsFolly

If when you use the new domain in the browser it goes to a google search or something like that then the change to your hosts file has not been recognised.

如果当您在浏览器中使用新域时,它会转到 google 搜索或类似的内容,那么对您的主机文件的更改尚未被识别。

You have to either reboot after changing the hosts file or more simply run these 2 commands from a command windows ( as administrator )

您必须在更改主机文件后重新启动,或者更简单地从命令窗口运行这两个命令(以管理员身份)

net stop dnscache
net start dnscache

This will refresh windows DNS cache and pick up your hosts file changes.

这将刷新 Windows DNS 缓存并获取您的主机文件更改。

It is also a good idea to pingyour new domain to check it is being seen, use

ping检查您的新域是否被看到也是一个好主意,使用

ping chelle.wpblog.local

If it reports 127.0.0.1 as the ip address and 4 send and 4 receieved packet then the domain is now known to Windows, if it reports some other ip address the hosts file change has not succeeded.

如果它报告 127.0.0.1 作为 ip 地址和 4 个发送和 4 个接收数据包,那么域现在对 Windows 是已知的,如果它报告某个其他 ip 地址,则主机文件更改未成功。

Also it is a good idea when creating VHOSTS to also create one for localhost or the wamp home page will not work. You also have old Apache 2.2 syntax (Order allow,deny allow from all) you could try using the Apache 2.4 syntax, see below

此外,在创建 VHOSTS 时也为本地主机创建 VHOSTS 也是一个好主意,否则 wamp 主页将不起作用。您还有旧的 Apache 2.2 语法(Order allow,deny allow from all),您可以尝试使用 Apache 2.4 语法,见下文

So try this

所以试试这个

## must be first so the the wamp menu page loads
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "D:/wamp/www"
    ServerName localhost
    <Directory  "D:/wamp/www">
        Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80> 
    ServerAdmin [email protected]
    ServerName chelle.wpblog.local 
    ServerAlias www.chelle.wpblog.local 
    DocumentRoot "E:/Subversion/chelle.wpblog.local" 
    <Directory "E:/Subversion/chelle.wpblog.local"> 
        Options Indexes FollowSymLinks MultiViews 
        AllowOverride All 
        Require all granted
    </Directory>
</VirtualHost>

回答by Thilina

In WampServer 3.0.4 you do not need to run through all those setting up. Go to local host and under Tools area for "Add a Vertual Host" in the first time it will say turn on "Vertual Host sub menu" in wamp server settings. To get there right click on wamp icon > go to wamp server settings > and click on Vertual Host sub menu. Then re-fresh the page and follow the setup process, you just need to provide the server name and path. It will do all the works for you. Simple as that

在 WampServer 3.0.4 中,您不需要运行所有这些设置。第一次转到本地主机并在“添加垂直主机”的“工具”区域下,它会在 wamp 服务器设置中说打开“垂直主机子菜单”。要到达那里,请右键单击 wamp 图标 > 转到 wamp 服务器设置 > 并单击 Vertual Host 子菜单。然后重新刷新页面并按照设置过程进行,您只需要提供服务器名称和路径。它会为你做所有的工作。就那么简单

回答by Vikalp Veer

Method 1:

方法一:

in hosts file

在主机文件中

127.0.0.1   mysite
::1 mysite

in C:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vosts.conf file add your virtual host as follows

在 C:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vosts.conf 文件中添加您的虚拟主机,如下所示

<VirtualHost *:80>
    ServerName mysite
    DocumentRoot "<path_to_your_local_site>"
    <Directory  "<path_to_your_local_site>">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

Make sure vhost configuration is enable in httpd.conf file is enabled as follows C:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf

确保在 httpd.conf 文件中启用 vhost 配置启用如下 C:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf

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

After you have changed configuration as above, restart all service in wamp and load website as follows http://mysiteyour local site should open.

如上所述更改配置后,重新启动 wamp 中的所有服务并加载网站,如下所示 http://mysite您的本地站点应该打开。

Method 2:

方法二:

In latest versions of wamp, you can do this configuration via webpage. Open localhost in your browser. You would see wamp welcome page.

在最新版本的 wamp 中,您可以通过网页进行此配置。在浏览器中打开 localhost。你会看到 wamp 欢迎页面。

Down below under Tools, there is option to add virtual host. click on the link which would take you to add virtual host page.

在下面的工具下,有添加虚拟主机的选项。单击将带您添加虚拟主机页面的链接。

In Name of the host, enter your site name eg: mysite Under Complete absolute path of the VirtualHost folder, enter the path of your site.

在主机名称中,输入您的站点名称,例如:mysite 在 VirtualHost 文件夹的完整绝对路径下,输入您站点的路径。

Save the config and restart all services. you should be able to open mysite with the link http://mysite

保存配置并重新启动所有服务。您应该可以通过http://mysite链接打开mysite

As pointed in another answer, in newer versions of wam

正如另一个答案中所指出的,在较新版本的 wam 中

回答by Michael C.

Fast and easy,

快速简便,

  1. Create the folder for your local website
  2. Go to localhost (browser)
  3. Click Add a Virtual Host button
  4. Add the name of the virtual host
  5. Add the complete absolute path
  6. Restart DNS
  1. 为您的本地网站创建文件夹
  2. 转到本地主机(浏览器)
  3. 单击添加虚拟主机按钮
  4. 添加虚拟主机名
  5. 添加完整的绝对路径
  6. 重启DNS

Since this thread is old, perhaps you can just download WampServer from their website and install it. Once you have WampServer installed, you can check this article on How to Create Virtual Host Using WampServer & Install WordPressand follow the instruction. It is super fast and easy.

由于这个线程很旧,也许您可​​以从他们的网站下载 WampServer 并安装它。安装 WampServer 后,您可以查看有关如何使用 WampServer 创建虚拟主机和安装 WordPress 的文章并按照说明进行操作。它超级快速和简单。

回答by Keith Nordstrom

The answers above are overcomplicating the problem somewhat, and in fact the question is including extra information that's misleading the responders.

上面的答案在某种程度上使问题过于复杂,事实上,问题包括误导响应者的额外信息。

The clues lie in step 2 and the symptoms described in 5. The hosts file is set up but when the request is made to "blah.local", google responds. Apache is not involved.

线索在第 2 步和第 5 步中描述的症状中。hosts 文件已设置,但是当向“blah.local”发出请求时,google 会做出响应。阿帕奇不参与。

The first step to diagnosis would be pinging blah.local and seeing that it maps correctly to 127.0.0.1. I have never (in many years as a developer) found it necessary to restart local DNS on either Windows or Linux/Mac so I would expect this to work out of the box. But if it doesn't, of course restart DNS and see that it does.

诊断的第一步是 ping blah.local 并查看它是否正确映射到 127.0.0.1。我从来没有(作为开发人员多年)发现有必要在 Windows 或 Linux/Mac 上重新启动本地 DNS,所以我希望这能开箱即用。但如果没有,当然重新启动 DNS 并查看它。

However, late versions of Chrome at least do not recognize the TLD '.local' and so when you put the URL back in the browser, it's passed on to Google as a search term. There may be a setting in your browser to tell it to handle the unsanctioned TLD, but your simplest solution would be to use a TLD that's sanctioned by W3C like '.com'.

但是,最新版本的 Chrome 至少无法识别 TLD '.local',因此当您将 URL 放回浏览器时,它会作为搜索词传递给 Google。您的浏览器中可能有一个设置来告诉它处理未经批准的 TLD,但最简单的解决方案是使用 W3C 批准的 TLD,如“.com”。

In short, if your hosts file reads something like

简而言之,如果您的主机文件读取类似

127.0.0.1     local.chelle.wpblog.com

rather than

而不是

127.0.0.1     chelle.wpblog.local

everything should work.

一切都应该工作。