WAMP - 从项目 URL 中删除 localhost [英] WAMP - Remove localhost from project URL

查看:24
本文介绍了WAMP - 从项目 URL 中删除 localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WAMP 中的项目的 URL 没有像我预期的那样解析.例如,我希望文件夹 c:\wamp\www\project1 中的项目具有 URL http://project1/,但它实际上有 URL http://localhost/project1/.

这可能会导致访问服务器变量时出现问题.我该如何解决这个问题?

解决方案

您也可以 查看此答案 如果您现在正在使用 WAMPServer 3 或更高版本,请特别查看此答案,以通过简单的点击和创建虚拟主机的方式.

实际上,此更改是 WAMPServer 开发人员有意为之,并且是有充分理由的.

使用 localhost/project1 url 和默认 WAMPServer DocumentRoot 存在问题,因为它会导致某些框架和 WordPress 类型环境出现问题,如果您使用的代码是您自己的代码取决于对服务器环境的了解.

正确的解决方案是为所有项目创建虚拟主机,即使是那些存储在 \wamp\www\project1 样式文件夹中的项目.

这样做时 DocumentRoot 是 \wamp\www,这就是导致这些问题的原因.

这些工具希望 DocumentRoot 是站点的根目录,即 \wamp\www\project1 以便当它们使用像

这样的 PHP 变量时

$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']$_SERVER['DOCUMENT_ROOT']

他们得到了正确的答案,即他们会在仅托管该站点的真实服务器上得到的答案.

所以使用 localhost\project1 样式的 url 意味着这些变量会返回

$_SERVER['HTTP_HOST'] = 本地主机$_SERVER['SERVER_NAME'] = 本地主机$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www

他们应该什么时候回来

$_SERVER['HTTP_HOST'] = project1$_SERVER['SERVER_NAME'] = project1$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1

因此,要使 My Projects 菜单正常工作并减少将站点复制到实时服务器的痛苦,您应该做的是:

像这样在 HOSTS 文件中为每个项目创建一个条目,并记住创建一个用于通过 IPV4(127.0.0.1) 访问和一个用于通过 IPV6 (::1) 访问:-

127.0.0.1 本地主机127.0.0.1 项目 1::1 本地主机::1个项目1

记住在对此文件进行任何更改后刷新 Windows DNS 缓存,如下所示:-

使用以管理员身份运行启动命令窗口并运行:-

net stop Dnscache网络启动 Dnscache

现在你必须创建一个虚拟主机定义,所以编辑\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf 文件(apache 版本可能不同)

在您第一次执行此操作时删除其中的默认内容.然后像这样创建您的虚拟主机定义:-

<预><代码>## 使用基于名称的虚拟主机.# 如果您使用的是 Apache 2.4.x,下一行不是必需的,应该删除名称虚拟主机 *:80## 应该是第一个,以便 wamp 菜单页面加载并且是默认站点## 也不应该从只允许从本地机器访问更改##从临时IP地址探测中获得一些额外的安全性<虚拟主机 *:80>DocumentRoot "C:/wamp/www"服务器名称本地主机服务器别名本地主机<目录C:/wamp/www">允许覆盖所有<IfDefine APACHE24>需要本地</IfDefine><IfDefine !APACHE24>订单拒绝,允许拒绝所有人允许来自 127.0.0.1 本地主机 ::1</IfDefine></目录></虚拟主机><虚拟主机 *:80>DocumentRoot "C:/wamp/www/project1"服务器名称 project1服务器别名项目1<目录C:/wamp/www/project1">允许覆盖所有<IfDefine APACHE24>需要本地</IfDefine><IfDefine !APACHE24>订单拒绝,允许拒绝所有人允许来自 127.0.0.1 本地主机 ::1</IfDefine></目录></虚拟主机>

现在您还需要进行一项更改,您必须取消注释 httpd.conf 中包含我们刚刚更改的上述文件的行.因此,请编辑 httpd.conf 文件,使用 wampmanager 菜单执行此操作,因为它可以确保您编辑正确的文件.

找到这一行 #Include conf/extra/httpd-vhosts.conf 并从该行的开头删除注释 # 符号,如下所示:-

包含 conf/extra/httpd-vhosts.conf

当然,您现在需要重新启动 Apache,以便它接收您的配置更改.

如果 Apache 没有重新启动,您可能在配置中犯了一个错误,要找出问题所在,试试这个.

打开一个命令窗口,将CD放入\wamp\bin\apache\apache2.4.9\bin文件夹中.

然后运行:-

httpd -t

如果错误在 httpd.confhttpd-vhost.conf 文件中,它会告诉您错误并给您行号以查找很容易出错.

The URLs of my projects in WAMP are not resolving as I'd expect. For example, I'd expect the project in the folder c:\wamp\www\project1 to have the URL http://project1/, but it actually has the URL http://localhost/project1/.

This can cause problems when accessing server variables. How do I fix this?

解决方案

You can also look at this answer specially if you are now using WAMPServer 3 or greater, for a simple clikc and go way to create Virtual hosts.

Actually this change was intended by the WAMPServer developers and for a good reason.

There is a problem using the localhost/project1 url and the default WAMPServer DocumentRoot in that it causes problems for some frameworks and WordPress type environments, as well as your own code if you are using code which depends on knowing anything about the server environment.

The correct solution is to create Virtual Hosts for all your projects even those that you store in the \wamp\www\project1 style folders.

When doing that the DocumentRoot is \wamp\www and that is what causes these problems.

These tools expect the DocumentRoot to be the root of the site i.e. \wamp\www\project1 so that when they use PHP variables like

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']

they get the correct answer i.e. the answer they would get on a real live server hosting just that site.

So using the localhost\project1 style url would mean these variables would return

$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www

When they should return

$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1

So what you should do to make the My Projects menu work and reduce your pain in copying sites to live servers is:

Create an entry in the HOSTS file for each project like so and remember to create one for access via IPV4(127.0.0.1) and one for access via IPV6 (::1):-

127.0.0.1 localhost
127.0.0.1 project1

::1 localhost
::1 project1

Remember to refresh the Windows DNS Cache after any change to this file like so :-

Start a command window using Run as Administrator and run :-

net stop Dnscache
net start Dnscache

Now you must create a Virtual Host definition, so edit the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf file ( apache versions may differ )

Delete the default stuff in there the first time you do this. And then create your Virtual Host definitions like so :-

#
# Use name-based virtual hosting.
# This next line is not required if you are using Apache 2.4.x and should be deleted
NameVirtualHost *:80

## should be first so the wamp menu page loads and is the default site
## should also never be changed from only allowing access from the local machine
## for a bit of extra security from casual ip address probing
<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName  localhost
    ServerAlias localhost
    <Directory  "C:/wamp/www">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/project1"
    ServerName  project1
    ServerAlias project1
    <Directory  "C:/wamp/www/project1">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>

    </Directory>
</VirtualHost>

Now you need one more change, you must uncomment the line in httpd.conf that includes the above file we have just changed. So edit the httpd.conf file, use the wampmanager menus to do this as it ensures you edit the correct file.

Find this line #Include conf/extra/httpd-vhosts.conf and remove the comment # symbol from the beginning of the line like so :-

Include conf/extra/httpd-vhosts.conf

Now of course you will need to restart Apache so that it picks up your configuration changes.

If Apache does not restart, you probably made a mistake in the config, to find out what is wrong try this.

Open a command window and CD into the \wamp\bin\apache\apache2.4.9\bin folder.

Then run this :-

httpd -t

If the error is in httpd.conf or the httpd-vhost.conf files it will tell you the error and also give you the line number to make finding the error very easy.

这篇关于WAMP - 从项目 URL 中删除 localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆