php 项目链接在 Wamp 服务器上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23665064/
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
Project Links do not work on Wamp Server
提问by user3638431
I am installing the Wamp Server on another computer to run a mid-sized database and UI. I have been successful in blocking IIS and routing the server to Localhost:8080. But whenever I try to access on of my projects from the localhost homepage, in the www file; I get redirected to a Page not found error.
我正在另一台计算机上安装 Wamp Server 以运行中型数据库和 UI。我已成功阻止 IIS 并将服务器路由到 Localhost:8080。但是每当我尝试从 localhost 主页访问我的项目时,在 www 文件中;我被重定向到找不到页面错误。
When I hover above the links the directory always comes up "http:// ProjectFolderNameHere /". when it's supposed to be "http:// LocalHost:8080 / ProjectFolderNameHere /". What can I do to get the links working properly?
当我将鼠标悬停在链接上方时,目录总是出现“http://ProjectFolderNameHere/”。当它应该是“http://LocalHost:8080/ProjectFolderNameHere/”时。我该怎么做才能使链接正常工作?
My Machine runs on Windows 7 Home Edition 64-bits, and I already have Microsoft's IIS disabled.
我的机器在 64 位 Windows 7 家庭版上运行,并且我已经禁用了 Microsoft 的 IIS。
回答by RiggsFolly
How to create a Virtual Host in WampServer
如何在 WampServer 中创建虚拟主机
WAMPServer 3 has made this process much easier!
WAMPServer 3 使这个过程变得更加容易!
You can do almost everything from a utility provided as part of WAMPServer.
您可以通过作为 WAMPServer 一部分提供的实用程序执行几乎所有操作。
Create a folder inside to contain your project.site. This can be under the
C:\wamp\www\
directory or in a completely seperate folder likeC:\websites
.Create a folder inside the location you have chosen EG
C:\websites\project1\www
or under thec:\wamp\www\project1\www
Now open localhost
wampmanager->localhost
and click on the linkAdd a Virtual Host
under the TOOLS section on the homepage.
在里面创建一个文件夹来包含您的 project.site。这可以在
C:\wamp\www\
目录下或在一个完全独立的文件夹中,如C:\websites
.在您选择的位置内
C:\websites\project1\www
或在您选择的 EG下创建一个文件夹c:\wamp\www\project1\www
现在打开 localhost
wampmanager->localhost
并单击Add a Virtual Host
主页上工具部分下的链接。
You will see a page like this:
你会看到一个这样的页面:
Fill in the fields as specified by the instructions above each field
The Virtual Host config will have been created for you.
Now you must restart the DNS Cache. You can do this from the wampmanager menus like this
right click wampmanager->Tools->Restart DNS
. The DNS Cache will be restarted and then Apache will also be stopped and restarted. When the wampmanager icon goes green again all is completed.Now you must create a simple
index.php
file or install your site into the folder you created above.Assuming your VH was called
project.dev
You should see that name under the Your Virtual HostsSection of the WAMPServer homepage.You can launch the site from this menu, or just use the new Domain Name in the address bar EG
project1.dev
and the site shoudl launch.
按照每个字段上方的说明填写字段
将为您创建虚拟主机配置。
现在您必须重新启动 DNS 缓存。您可以像这样从 wampmanager 菜单中执行此操作
right click wampmanager->Tools->Restart DNS
。DNS 缓存将重新启动,然后 Apache 也将停止并重新启动。当 wampmanager 图标再次变为绿色时,一切就完成了。现在您必须创建一个简单的
index.php
文件或将您的站点安装到您在上面创建的文件夹中。假设您的 VH 被称为
project.dev
您应该在 WAMPServer 主页的您的虚拟主机部分下看到该名称。您可以从此菜单启动站点,或者只使用地址栏中的新域名 EG
project1.dev
和站点应该启动。
Old WAMPServer 2.5 mechanism, or if you want to do it all manually
旧的 WAMPServer 2.5 机制,或者如果您想手动完成所有操作
There has been a change of concept in WampServer 2.5 and above and there is a good reason for this change!
WampServer 2.5 及更高版本的概念发生了变化,这种变化是有充分理由的!
In WampServer it is now STRONGLY encouraged to create a Virtual Host for each of your projects, even if you hold them in a \wamp\www\subfolder
structure.
在 WampServer 中,现在强烈鼓励为您的每个项目创建一个虚拟主机,即使您将它们保存在一个\wamp\www\subfolder
结构中。
The WampServer home page ( \wamp\www\index.php
) now expects you to have created a Virtual Host for all your projects and will therefore work properly only if you do so.
WampServer 主页 ( \wamp\www\index.php
) 现在希望您为所有项目创建一个虚拟主机,因此只有在您这样做时才能正常工作。
History
历史
In order to make life easier for beginners using WampServer to learn PHP Apache and MySQL it was suggested that you create subfolders under the \wamp\www\
folder.
为了让使用 WampServer 的初学者更轻松地学习 PHP Apache 和 MySQL,建议您在该\wamp\www\
文件夹下创建子文件夹。
wamp
|-- www
|-- Chapter1
|-- Chapter2
|-- etc
These subfolders would then show as links in the WampServer Homepage under a menu called 'Your Projects' and these links would contain a link to localhost/subfoldername
.
然后,这些子文件夹将在 WampServer 主页中名为“您的项目”的菜单下显示为链接,这些链接将包含指向localhost/subfoldername
.
Acceptable only for simple tutorials
仅适用于简单教程
This made life easy for the complete beginner, and was perfectly acceptable for example for those following tutorials to learn PHP coding. However it was never intended for use when developing a real web site that you would later want to copy to your live hosted server. In fact if you did use this mechanism it often caused problems as the live sites configuration would not match your development configuration.
这让整个初学者的生活变得轻松,并且对于那些跟随教程学习 PHP 编码的人来说是完全可以接受的。但是,它从未打算用于开发您以后想要复制到实时托管服务器的真实网站时。事实上,如果您确实使用了这种机制,它通常会导致问题,因为实时站点配置与您的开发配置不匹配。
The Problemfor real website development.
实际网站开发的问题。
The reason for this is of course that the default DocumentRoot setting for wamp is
这样做的原因当然是 wamp 的默认 DocumentRoot 设置是
DocumentRoot "c:/wamp/www/"
DocumentRoot "c:/wamp/www/"
regardless of what your subfolder was called. This ment that often used PHP code that queried the structure or your site received different information when running on your development WampServer to what it would receive when running on a live hosted server, where the DocumentRoot configuration points to the folder at the top of the website file hierarchy. This kind of code exists in many frameworks and CMS's for example WordPress and Joomla etc.
不管你的子文件夹叫什么。经常使用 PHP 代码查询结构或您的网站在开发 WampServer 上运行时收到的信息与在实时托管服务器上运行时收到的信息不同,其中 DocumentRoot 配置指向网站顶部的文件夹文件层次结构。这种代码存在于许多框架和 CMS 中,例如 WordPress 和 Joomla 等。
For Example
例如
Lets say we have a project called project1 held in wamp\www\project1
and run incorrectly as localhost/project1/index.php
假设我们有一个名为 project1 的项目,wamp\www\project1
并且运行不正确localhost/project1/index.php
This is what would be reported by some of the PHP command in question:
这是一些有问题的 PHP 命令会报告的内容:
$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = c:/wamp/www
Now if we had correctly defined that site using a Virtual Host definition and ran it as http://project1
the results on the WAMPServer devlopment site will match those received when on a live hosted environment.
现在,如果我们使用虚拟主机定义正确定义了该站点并运行它http://project1
,那么 WAMPServer 开发站点上的结果将与在实时托管环境中收到的结果相匹配。
$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = c:/wamp/www/project1
Now this difference may seem trivial at first but if you were to use a framework like WordPress or one of the CMS's like Joomla for example, this can and does cause problems when you move your site to a live server.
现在,这种差异乍一看似乎微不足道,但是如果您要使用像 WordPress 这样的框架或像 Joomla 这样的 CMS 之一,那么当您将站点移动到实时服务器时,这可能并且确实会导致问题。
How to create a Virtual Host in WampServer
如何在 WampServer 中创建虚拟主机
Actually this should work basically the same for any wndows Apache server, with differences only in where you may find the Apache config files.
实际上,对于任何 wndows Apache 服务器,这应该基本相同,区别仅在于您可以找到 Apache 配置文件的位置。
There are 3 steps to create your first Virtual Host in Apache, and only 2 if you already have one defined.
在 Apache 中创建您的第一个虚拟主机需要 3 个步骤,如果您已经定义了一个,则只需 2 个步骤。
- Create the Virtual Host definition(s)
- Add your new domain name to the HOSTS file.
- Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.
- 创建虚拟主机定义
- 将您的新域名添加到 HOSTS 文件中。
- 取消注释 httpd.conf 中包含虚拟主机定义文件的行。
Step 1, Create the Virtual Host definition(s)
第 1 步,创建虚拟主机定义
Edit the file called httpd-hosts.conf
which for WampServer lives in
编辑httpd-hosts.conf
WampServer 所在的文件
\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf
(Apache version numbers may differ, engage brain before continuing)
(Apache 版本号可能不同,动脑再继续)
If this is the first time you edit this file, remove the default example code, it is of no use.
如果是第一次编辑这个文件,去掉默认的示例代码,没有用。
I am assuming we want to create a definition for a site called project1 that lives in
我假设我们要为一个名为 project1 的站点创建一个定义,该站点位于
\wamp\www\project1
Very important, first we must make sure that localhost still works so that is the first VHOST definition we will put in this file.
非常重要,首先我们必须确保 localhost 仍然有效,以便这是我们将放在此文件中的第一个 VHOST 定义。
<VirtualHost *:80>
DocumentRoot "c:/wamp/www"
ServerName localhost
ServerAlias localhost
<Directory "c:/wamp/www">
Options Indexes FollowSymLinks
AllowOverride All
Require local
</Directory>
</VirtualHost>
Now we define our project: and this of course you do for each of your projects as you start a new one.
现在我们定义我们的项目:当然,当您开始一个新项目时,您会为每个项目都这样做。
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/project1"
ServerName project1
<Directory "c:/wamp/www/project1">
Options Indexes FollowSymLinks
AllowOverride All
Require local
</Directory>
</VirtualHost>
NOTE: That each Virtual Host as its own DocumentRoot
defined. There are also many other parameters you can add to a Virtual Hosts definition, check the Apache documentation.
注意:每个虚拟主机都是它自己DocumentRoot
定义的。您还可以将许多其他参数添加到虚拟主机定义中,请查看 Apache 文档。
Small aside
小边
The way virtual hosts work in Apache: The first definition in this file will also be the default site, so should the domain name used in the browser not match any actually defined virtually hosted domain, making localhost the first domain in the file will therefore make it the site that is loaded if a hack attempt just uses your IP Address. So if we ensure that the Apache security for this domain is ALWAYS SET TO
虚拟主机在 Apache 中的工作方式:此文件中的第一个定义也将是默认站点,因此如果浏览器中使用的域名与任何实际定义的虚拟托管域不匹配,则使 localhost 成为文件中的第一个域如果黑客尝试仅使用您的 IP 地址,则加载该站点。因此,如果我们确保此域的 Apache 安全性始终设置为
Require local
any casual hack from an external address will receive an error and not get into your PC, but should you misspell a domain you will be shown the WampServer homepage, because you are on the same PC as WampServer and therfore local
.
任何来自外部地址的随意入侵都会收到错误信息并且不会进入您的 PC,但是如果您拼错了一个域,您将看到 WampServer 主页,因为您与 WampServer 位于同一台 PC 上,因此local
。
Step 2:
第 2 步:
Add your new domain name to the HOSTS file. Now we need to add the domain name that we have used in the Virtual Host definition to the HOSTS file so that windows knows where to find it. This is similiar to creating a DNS A record, but it is only visible in this case on this specific PC.
将您的新域名添加到 HOSTS 文件中。现在我们需要将我们在虚拟主机定义中使用的域名添加到 HOSTS 文件中,以便 windows 知道在哪里可以找到它。这类似于创建 DNS A 记录,但它仅在这种情况下在此特定 PC 上可见。
Edit C:\windows\system32\drivers\etc\hosts
编辑 C:\windows\system32\drivers\etc\hosts
The file has no extension and should remain that way. Watch out for notepad, as it may try and add a .txt
extension if you have no better editor.
I suggest you download Notepad++, its free and a very good editor.
该文件没有扩展名,应该保持这种方式。注意记事本,因为.txt
如果您没有更好的编辑器,它可能会尝试添加扩展名。我建议您下载 Notepad++,它是免费且非常好的编辑器。
Also this is a protected file so you must edit it with administrator privileges, so launch you editor using the Run as Administratormenu option.
此外,这是一个受保护的文件,因此您必须使用管理员权限对其进行编辑,因此请使用“以管理员身份运行”菜单选项启动您的编辑器。
The hosts file should look like this when you have completed these edits
完成这些编辑后,hosts 文件应如下所示
127.0.0.1 localhost
127.0.0.1 project1
::1 localhost
::1 project1
Note that you should have definitions in here for the IPV4 loopback address 127.0.0.1
and also the IPV6 loopback address ::1
as Apache is now IPV6 aware and the browser will use either IPV4 or IPV6 or both. I have no idea how it decides which to use, but it can use either if you have the IPV6 stack turned on, and most window OS's do as of XP SP3.
请注意,您应该在此处定义 IPV4 环回地址127.0.0.1
和 IPV6 环回地址,::1
因为 Apache 现在支持 IPV6,浏览器将使用 IPV4 或 IPV6 或两者。我不知道它是如何决定使用哪个的,但是如果您打开了 IPV6 堆栈,它可以使用任何一种,并且大多数窗口操作系统从 XP SP3 开始都可以使用。
Now we must tell windows to refresh its domain name cache, so launch a command window again using the Run as Administratormenu option again, and do the following.
现在我们必须告诉 Windows 刷新其域名缓存,因此再次使用“以管理员身份运行”菜单选项再次启动命令窗口,然后执行以下操作。
net stop dnscache
net start dnscache
This forces windows to clear its domain name cache and reload it, in reloading it will re-read the HOSTS file so now it knows about the domain project1
.
这会强制 Windows 清除其域名缓存并重新加载它,在重新加载时它将重新读取 HOSTS 文件,因此现在它知道了域project1
。
Step 3: Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.
第 3 步:取消注释 httpd.conf 中包含虚拟主机定义文件的行。
Edit your httpd.conf, use the wampmanager.exe menus to make sure you edit the correct file.
编辑您的 httpd.conf,使用 wampmanager.exe 菜单确保您编辑了正确的文件。
Find this line in httpd.conf
在 httpd.conf 中找到这一行
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
And just remove the #
to uncomment that line.
只需删除该行#
即可取消注释。
To activate this change in you running Apache we must now stop and restart the Apache service.
要在运行 Apache 时激活此更改,我们现在必须停止并重新启动 Apache 服务。
wampmanager.exe -> Apache -> Service -> Restart Service
Now if the WAMP icon in the system tray does not go GREEN again, it means you have probably done something wrong in the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-hosts.conf
file.
现在,如果系统托盘中的 WAMP 图标没有再次变为绿色,则表示您可能在\wamp\bin\apache\apache2.4.9\conf\extra\httpd-hosts.conf
文件中做错了什么。
If so here is a useful mechanism to find out what is wrong. It uses a feature of the Apache exe (httpd.exe) to check its config files and report errors by filename and line numbers.
如果是这样,这里有一个有用的机制来找出问题所在。它使用 Apache exe (httpd.exe) 的一项功能来检查其配置文件并按文件名和行号报告错误。
Launch a command window.
启动命令窗口。
cd \wamp\bin\apache\apache2.4.9\bin
httpd -t
So fix the errors and retest again until you get the output
因此,修复错误并再次重新测试,直到获得输出
Syntax OK
Now there is one more thing.
现在还有一件事。
There are actually 2 new menu items on the wampmanager menu system. One called 'My Projects'which is turned on by default. And a second one, called 'My Virtual Hosts', which is not activated by default.
wampmanager 菜单系统上实际上有 2 个新菜单项。一个叫做“我的项目”,默认情况下是打开的。还有第二个,称为“我的虚拟主机”,默认情况下未激活。
'My Projects' will list any sub directory of the \wamp\www directory and provide a link to launch the site in that sub directory. As I said earlier, it launches 'project1` and not 'localhost/project1' so to make the link work we must create a Virtual Host definition to make this link actually launch that site in your browser, without the Virtual Host definition it's likely to launch a web search for the site name as a keyword or just return a site not found condition.
“我的项目”将列出 \wamp\www 目录的任何子目录,并提供一个链接以在该子目录中启动站点。正如我之前所说,它启动“project1”而不是“localhost/project1”,因此要使链接正常工作,我们必须创建一个虚拟主机定义,以使此链接在您的浏览器中实际启动该站点,而没有虚拟主机定义,它可能会启动网站名称作为关键字的网络搜索或仅返回网站未找到条件。
The 'My Virtual Hosts' menu item is a little different. It searches the file that is used to define Virtual Hosts ( we will get to that in a minute ) and creates menu links for each ServerName parameter it finds and creates a menu item for each one. This may seem a little confusing as once we create a Virtual Host definition for the sub directories of the \wamp\www folder some items will appear on both of the 'My Projects' menu and the 'My Virtual Hosts' menu's.
“我的虚拟主机”菜单项略有不同。它搜索用于定义虚拟主机的文件(我们将在稍后介绍)并为它找到的每个 ServerName 参数创建菜单链接,并为每个参数创建一个菜单项。这似乎有点令人困惑,因为一旦我们为 \wamp\www 文件夹的子目录创建了虚拟主机定义,一些项目将同时出现在“我的项目”菜单和“我的虚拟主机”菜单上。
How do I turn this other 'My Virtual Hosts' menu on?
如何打开另一个“我的虚拟主机”菜单?
- Make a backup of the \wamp\wampmanager.tpl file, just in case you make a mistake, its a very important file.
- Edit the \wamp\wampmanager.tpl
- Find this parameter
;WAMPPROJECTSUBMENU
, its in the '[Menu.Left]' section. - Add this new parameter
;WAMPVHOSTSUBMENU
either before or after the;WAMPPROJECTSUBMENU
parameter. - Save the file.
- Now right click the wampmanager icon, and select 'Refresh'. If this does not add the menu, 'exit' and restart wampmanager.
- 备份\wamp\wampmanager.tpl 文件,以防万一你弄错了,这是一个非常重要的文件。
- 编辑\wamp\wampmanager.tpl
- 找到这个参数
;WAMPPROJECTSUBMENU
,它在“[Menu.Left]”部分。 ;WAMPVHOSTSUBMENU
在参数之前或之后添加这个新;WAMPPROJECTSUBMENU
参数。- 保存文件。
- 现在右键单击 wampmanager 图标,然后选择“刷新”。如果这没有添加菜单,请“退出”并重新启动 wampmanager。
Big NoteThe new menu will only appear if you already have some Virtual Hosts defined! Otherwise you will see no difference until you define a VHOST.
重要提示只有当您已经定义了一些虚拟主机时,新菜单才会出现!否则,在定义 VHOST 之前,您将看不到任何区别。
Now if you take this to its logical extension
现在如果你把它带到它的逻辑扩展
You can now move your web site code completely outside the \wamp\
folder structure simply by changing the DocumentRoot parameter in the VHOST definition. So for example you could do this:
现在,您\wamp\
只需更改 VHOST 定义中的 DocumentRoot 参数即可将您的网站代码完全移到文件夹结构之外。例如,您可以这样做:
Create a folder on the wamp disk or any other disk ( beware of network drive, they are a bit more complicated)
在 wamp 磁盘或任何其他磁盘上创建一个文件夹(当心网络驱动器,它们有点复杂)
D:
MD websites
CD websites
MD example.com
CD example.com
MD www
You now copy your site code to, or start creating it in the \websites\example.com\www
folder and define your VHOST like this:
您现在将站点代码复制到或开始在\websites\example.com\www
文件夹中创建它并像这样定义您的 VHOST:
<VirtualHost *:80>
DocumentRoot "d:/websites/example.com/www"
ServerName example.dev
ServerAlias www.example.dev
<Directory "d:/websites/example.com/www">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
php_flag display_errors Off
php_flag log_errors On
php_value max_upload_size 40M
php_value max_execution_time 60
php_value error_log "d:/wamp/logs/example_com_phperror.log"
</VirtualHost>
Then add this new development domain to the HOSTS file:
然后将这个新的开发域添加到 HOSTS 文件中:
127.0.0.1 localhost
::1 localhost
127.0.0.1 project1
::1 project1
127.0.0.1 example.dev
::1 example.dev
NOTE: It is not a good idea to use a ServerName or ServerAlias that is the same as your live domain name, as if we had used example.com as the ServerName it would mean we could no longer get to the real live site from this PC as it would direct example.com to 127.0.0.1 i.e. this PC and not out onto the internet.
注意:使用与您的实时域名相同的 ServerName 或 ServerAlias 不是一个好主意,就像我们使用 example.com 作为 ServerName 一样,这意味着我们无法再从这里访问真正的实时站点PC 因为它会将 example.com 定向到 127.0.0.1,即这台 PC 而不是互联网。
ALSO: See that I have allowed this site to be accessed from the internet from within the VHOST definitions, this change will apply to only this site and no other. Very useful for allowing a client to view your changes for an hour or so without having to copy them to the live server. This does mean that we have to edit this file manually to turn this access on and off rather than use the Put Online/Offline menu item on wampmanager.
另外:请注意,我已允许从 VHOST 定义中的 Internet 访问此站点,此更改将仅适用于此站点,不适用于其他站点。对于允许客户端查看您的更改一小时左右而无需将它们复制到实时服务器非常有用。这确实意味着我们必须手动编辑此文件以打开和关闭此访问,而不是使用 wampmanager 上的 Put Online/Offline 菜单项。
Also I have added some modifications to the PHP config, again that will only apply to this one site. Very useful when maintaining a site with specific requirement unlike all the other sites you maintain. I guess we can assume from the parameters used that it has a long running page in it somewhere and it is very badly written and will not run with errors being displayed on the browser without making a horrible mess of the page. Believe me sites like this exist and people still want them maintained badly. But this mean we only have to change these parameters for this specific site and not globally to all Virtual sites running on WampServer.
此外,我还对 PHP 配置添加了一些修改,同样仅适用于该站点。与您维护的所有其他站点不同,在维护具有特定要求的站点时非常有用。我想我们可以根据使用的参数假设它在某个地方有一个长时间运行的页面,它写得非常糟糕,并且不会在浏览器上显示错误的情况下运行,而不会使页面变得一团糟。相信我这样的网站存在,人们仍然希望它们维护得不好。但这意味着我们只需为这个特定站点更改这些参数,而不是全局更改 WampServer 上运行的所有虚拟站点。
回答by Hussain
I believe this is the best solution:
我相信这是最好的解决方案:
Open index.php in www folder and set
在 www 文件夹中打开 index.php 并设置
change line
30:$suppress_localhost = true;
更改第 30 行:$suppress_localhost = true;
to
$suppress_localhost = false;
到
$suppress_localhost = false;
This will ensure the project is prefixed with your local host IP/name
这将确保项目以您的本地主机 IP/名称为前缀
回答by Arun Unnikrishnan
Open index.php
in www
folder and set
index.php
在www
文件夹中打开并设置
$suppress_localhost = false;
This will prepend http://localhost/
to your project links
这将添加http://localhost/
到您的项目链接
回答by Rakesh
In order to access project from the homepage you need to create a Virtual Hostfirst.
为了从主页访问项目,您需要先创建一个虚拟主机。
Most easiest way to do this is to use Wamp's Add a Virtual HostUtility.
最简单的方法是使用 Wamp 的添加虚拟主机实用程序。
Just follow these steps:
只需按照以下步骤操作:
- Create a folder inside "C:\wamp\www\"directory and give it a name that you want to give to your site for eg. 'mysite'. So the path would be "C:\wamp\www\mysite".
- Now open localhost's homepage in your browser, under Toolsmenu click on Add a Virtual Hostlink.
- Enter the name of the virtual host, that name must be the name of the folder we created inside wwwdirectory i.e. 'mysite'.
- Enter absolute pathof the virtual host i.e. "C:\wamp\www\mysite\"without quotes and click the button below saying 'Start the creation of the VirtualHost'.
- Virtual Host created, now you just need to 'Restart DNS'. To do this right click the wamp server's tray menu icon, click on Tools > Restart DNSand let the tray menu icon become green again.
- All set! Now just create 'index.php'page inside "C:\wamp\www\mysite\"directory. Add some code in 'index.php'file, like
<?php echo "<h1>Hello World</h1>"; ?>
- 在“C:\wamp\www\”目录中创建一个文件夹,并为其指定一个名称,例如您想为您的站点提供的名称。' mysite的'。所以路径将是"C:\wamp\www\mysite"。
- 现在在浏览器中打开 localhost 的主页,在工具菜单下单击添加虚拟主机链接。
- 输入虚拟主机的名称,该名称必须是我们在www目录中创建的文件夹的名称,即“ mysite”。
- 输入虚拟主机的绝对路径,即“C:\wamp\www\mysite\”,不带引号,然后单击下面的按钮“开始创建 VirtualHost”。
- 虚拟主机已创建,现在您只需要“重新启动 DNS”。为此,右键单击 wamp 服务器的托盘菜单图标,单击工具 > 重新启动 DNS,然后让托盘菜单图标再次变为绿色。
- 搞定!现在只需在“C:\wamp\www\mysite\”目录中创建“index.php”页面。在“index.php”文件中添加一些代码,比如
<?php echo "<h1>Hello World</h1>"; ?>
Now you can access the projects from the localhost's homepage. Just click the project link and you'll see 'Hello World' printed on your screen.
现在您可以从本地主机的主页访问这些项目。只需单击项目链接,您就会在屏幕上看到“Hello World”。
回答by Khadka Pushpendra
You can follow all steps by @RiggsFolly thats is really good answer, If you do not want to create virtual host and want to use like previous localhost/example/ or something like that you can use answer by @Arunu
您可以按照@RiggsFolly 的所有步骤进行操作,这是非常好的答案,如果您不想创建虚拟主机并想使用以前的 localhost/example/ 或类似的内容,您可以使用@Arunu 的答案
But if you still face problem please use this method,
但是如果您仍然遇到问题,请使用此方法,
- Locate your wamp folder (Eg. c:/Wamp/) where you have installed
- Goto Wamp/www/
- Open index.php file
- find this code
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://' : '').$file.'">'.$file.'</a></li>';
- modify it add localhost after http://
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://localhost' : '').$file.'">'.$file.'</a></li>';
- Restart wamp server
- open localhost see the updated links
- 找到已安装的 wamp 文件夹(例如 c:/Wamp/)
- 转到 Wamp/www/
- 打开 index.php 文件
- 找到这个代码
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://' : '').$file.'">'.$file.'</a></li>';
- 修改它在 http:// 之后添加 localhost
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://localhost' : '').$file.'">'.$file.'</a></li>';
- 重启 wamp 服务器
- 打开 localhost 查看更新的链接
Hope you got your url like previous version of wamp server.
希望你的 url 像以前版本的 wamp 服务器一样。
回答by hyp0xia
How To Fix The Broken Icon Links (blank.gif, text.gif, etc.)
如何修复损坏的图标链接(blank.gif、text.gif 等)
Unfortunately as previously mentioned, simply adding a virtual host to your project doesn't fix the broken icon links.
不幸的是,如前所述,简单地将虚拟主机添加到您的项目并不能修复损坏的图标链接。
The Problem:
问题:
WAMP/Apache does not change the directory reference for the icons to your respective installation directory. It is statically set to "c:/Apache24/icons" and 99.9% of users Apache installation does not reside here. Especially with WAMP.
WAMP/Apache 不会将图标的目录引用更改为您各自的安装目录。它静态设置为“c:/Apache24/icons”并且 99.9% 的用户 Apache 安装不驻留在此处。尤其是使用 WAMP。
The Fix:
修复:
Find your Apache icons directory! Typically it will be located here: "c:/wamp/bin/apache/apache2.4.9/icons". However your mileage may vary depending on your installation and if your Apache version is different, then your path will be different as well.\
Open up httpd-autoindex.conf in your favorite editor. This file can usually be found here: "C:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-autoindex.conf". Again, if your Apache version is different, then so will this path.
Find this definition (usually located near the top of the file):
Alias /icons/ "c:/Apache24/icons/" <Directory "c:/Apache24/icons"> Options Indexes MultiViews AllowOverride None Require all granted </Directory>
Replace the "c:/Apache24/icons/" directories with your own. IMPORTANTYou MUST have a trailing forward slash in the first directory reference. The second directory reference must have no trailing slash. Your results should look similar to this. Again, your directory may differ:
Alias /icons/ "c:/wamp/bin/apache/apache2.4.9/icons/" <Directory "c:/wamp/bin/apache/apache2.4.9/icons"> Options Indexes MultiViews AllowOverride None Require all granted </Directory>
Restart your Apache server and enjoy your cool icons!
找到您的 Apache 图标目录!通常它位于:“c:/wamp/bin/apache/apache2.4.9/icons”。但是,您的里程可能会因您的安装而异,如果您的 Apache 版本不同,那么您的路径也会不同。\
在您喜欢的编辑器中打开 httpd-autoindex.conf。这个文件通常可以在这里找到:“C:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-autoindex.conf”。同样,如果您的 Apache 版本不同,那么这条路径也会不同。
找到这个定义(通常位于文件顶部附近):
Alias /icons/ "c:/Apache24/icons/" <Directory "c:/Apache24/icons"> Options Indexes MultiViews AllowOverride None Require all granted </Directory>
用您自己的目录替换“c:/Apache24/icons/”目录。重要信息在第一个目录引用中必须有一个尾随正斜杠。第二个目录引用必须没有尾部斜杠。您的结果应该与此类似。同样,您的目录可能有所不同:
Alias /icons/ "c:/wamp/bin/apache/apache2.4.9/icons/" <Directory "c:/wamp/bin/apache/apache2.4.9/icons"> Options Indexes MultiViews AllowOverride None Require all granted </Directory>
重新启动您的 Apache 服务器并享受您的酷图标!
回答by Valtec
$suppress_localhost = false;
This did the trick for me.
这对我有用。
回答by Arnaud Leyder
This works on Wamp 3+.
这适用于 Wamp 3+。
- Go to wamp folder (wamp/ or wamp64/)
- Open wampmanager.conf
- Find urlAddLocalhost param and set it on:
urlAddLocalhost = "on"
- 转到 wamp 文件夹(wamp/ 或 wamp64/)
- 打开 wampmanager.conf
- 找到 urlAddLocalhost 参数并将其设置为:
urlAddLocalhost = "on"
There should not be the need to tweak the index.php in www folder.
不需要调整 www 文件夹中的 index.php。
回答by TheSteven
Re: Wampserver LocalHost links not working correctly
This is as of June 2014 with Wampserver2.5 (maybe they'll fix this in later builds).
Note: to use LocalHost:8080 instead of LocalHost just make the appropriate changes in the edits mentioned below.
回复:Wampserver LocalHost 链接无法正常工作
这是截至 2014 年 6 月的 Wampserver2.5(也许他们会在以后的版本中解决这个问题)。
注意:要使用 LocalHost:8080 而不是 LocalHost 只需在下面提到的编辑中进行适当的更改。
There are 2 aspects of this issue -
The first is to be able to access items under "Your Projects" from the Wamp localhost homepage.
The second is to be able to correctly access items listed in the Wampserver Icon Taskbar's "My Projects" list.
此问题有两个方面 -
首先是能够从 Wamp localhost 主页访问“您的项目”下的项目。
第二个是能够正确访问 Wampserver 图标任务栏的“我的项目”列表中列出的项目。
To fix the first (to be able to access items under "Your Projects" from the Wamp localhost homepage) you will need to do the following...
要修复第一个(为了能够从 Wamp localhost 主页访问“您的项目”下的项目),您需要执行以下操作...
There are 2 editsthat you must make in the index.php file located in your wamp\www folder (usually C:\wamp\www)
您必须在 wamp\www 文件夹(通常是 C:\wamp\www)中的 index.php 文件中进行 2 次编辑
1) on Line 30change
1)30号线改
$suppress_localhost = true;
to
到
$suppress_localhost = false;
2) on line 338change
2)338线上改
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://' : '').$file.'">'.$file.'</a></li>';
to
到
$projectContents .= '<li><a href="'.($suppress_localhost ? 'http://' : 'http://localhost//').$file.'">'.$file.'</a></li>';
After you've made the above edits - if the Wampserver is running just refresh the local host page and the changes become immediately effective.
完成上述编辑后 - 如果 Wampserver 正在运行,只需刷新本地主机页面,更改就会立即生效。
To fix the 2nd item (the Wampserver Icon Taskbar's "My Projects" list): You need to edit C:\wamp\scripts\refresh.php
要修复第二个项目(Wampserver 图标任务栏的“我的项目”列表):您需要编辑 C:\wamp\scripts\refresh.php
Locate line 651 and change the section of the line that reads
找到第 651 行并更改读取的行的部分
Parameters: "http://'.$projectContents[$i].'/"; Glyph: 5
to
到
Parameters: "http://localhost//'.$projectContents[$i].'/"; Glyph: 5
After you make these 2nd set of changes you may have to force Wampserver to refresh the "My Projects" list by toggling the Put Online/Offline option at the bottom of the Wamp Icon Tray App.
进行第二组更改后,您可能必须通过切换 Wamp 图标托盘应用程序底部的在线/离线选项来强制 Wampserver 刷新“我的项目”列表。
回答by SuN
I find it's a lot easier (than accepted answer) to create a local subdomain by project and tell Apache to serve multiple sites by name.
我发现按项目创建本地子域并告诉 Apache 按名称为多个站点提供服务要容易得多(比接受的答案)。
For example, let's say you created a project under c:/wamp64/www/sites/mysite
, to be able to access it at http://mysite.localhostyou simply need to do the following:
例如,假设您在 下创建了一个项目c:/wamp64/www/sites/mysite
,为了能够在http://mysite.localhost访问它,您只需执行以下操作:
1. Tell your machine to answer to different namesAdd 127.0.0.1 mysite.localhost
to C:\windows\system32\drivers\etc\hosts
1. 告诉你的机器回答不同的名字添加127.0.0.1 mysite.localhost
到C:\windows\system32\drivers\etc\hosts
2. Flush your DNS cacheOpen a Command Prompt as administrator and type net stop dnscache
, then net start dnscache
.
2. 刷新您的 DNS 缓存以管理员身份打开命令提示符并键入net stop dnscache
,然后net start dnscache
。
3. Tell Apache where to lookClick on Wamp's icon in tray, go to Apache -> httpd.conf, and add this at the end:
3. 告诉 Apache 在哪里查看单击托盘中 Wamp 的图标,转到 Apache -> httpd.conf,并在最后添加:
# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName client1.localhost
# Folder where the files live
DocumentRoot "C:/wamp64/www/sites/mysite"
# A few helpful settings...
<Directory "C:/wamp64/www/sites/mysite">
allow from all
order allow,deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
(source)
(来源)
4. Restart ApacheClick on Wamp's icon in tray, select "restart"
4.重启Apache点击托盘中Wamp的图标,选择“重启”
5. Define a base urlGo to your project folder, add <base href="http://mysite.localhost" />
to your <head>
section to prevent /links to server root from being broken.
5. 定义基本 url转到您的项目文件夹,添加<base href="http://mysite.localhost" />
到您的<head>
部分以防止 /links 到服务器根目录被破坏。
Personally, I inject this html code dynamically into my template using PHP (something like $site_root = (IS_LOCALHOST) ? '<base href="http://mysite.localhost" />' : null;
) so I don't have to bother removing that once on production.
就我个人而言,我使用 PHP(类似$site_root = (IS_LOCALHOST) ? '<base href="http://mysite.localhost" />' : null;
)将此 html 代码动态注入到我的模板中,因此我不必在生产中删除一次。