如何在本地 PHP 开发设置中设置 DOCUMENT_ROOT 和站点根目录?
我正在为一个拥有在线网站的人做一份工作。 这对我来说是一个陌生的网站,我正在慢慢地研究奇怪的代码。 我本地有 MAMP,我的 http://localhost/ 有许多来自其中的客户端文件夹。 这段代码中有很多 $_SERVER['document_root'] 命令和引用,例如 ,它们在我本地的 PHP 开发区域中丢失了。
我如何轻松地将 document_root 引用设置为应有的样子(尽管只是在本地,但不想弄乱站点文件,因为我需要再次上传它们并且不想破坏实时站点!并且有没有一种间接设置方法,PHP 认为站点的根目录是这样图像的 src 引用“/images/...”将正确显示...我的本地 PHP 开发 URL 该站点是: http://localhost:8888/_CLIENTS/clientsite/www/ ...但是在代码中 '/ ' 在 '/images/...' 开头引用 http://localhost:8888/
谢谢你。
I'm doing a job for a guy with a site online. It's an alien site to me, and I'm slowly working through the strange code. I have MAMP locally and my http://localhost/ has many client folders coming off from that. Inside this code there is a lot of $_SERVER['document_root'] commands and references like which are just getting lost on my local PHP dev area.
How can I easily set the document_root reference to what it should be (just locally though, don't really want to mess with the site files, as I'll need to upload them again and don't want to break live site! And is there a way of indirect setting where PHP thinks the root of the site is so the image's src references "/images/..." will show up properly... My local PHP dev URL for this site is: http://localhost:8888/_CLIENTS/clientsite/www/ ...but in the code the '/' at the beginning of '/images/...' is referencing http://localhost:8888/ ??
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@Eddie 的回答对我帮助很大,但我仍然需要做一些额外的研究才能在 OSX 上使用 XAMPP 为自己解决同样的问题。 我想我应该在这里添加完整的解决方案,以造福子孙后代。
首先,我将以下条目添加到 httpd-vhosts.conf(在 XAMPP 中的“etc/extra/”文件夹下):
请注意,我专门使用通配符而不是“localhost”作为 VirtualHost url,并在我指定的位置添加了 ServerName 配置每个子域名。 另请注意,我使用了我的计算机名称(“my-machine”)而不是“localhost”——这样可以正确评估来自任何计算机(而不仅仅是 localhost)的所有请求。 我在 OSX 中进行开发,但通过 Parallels 在各种虚拟机中测试浏览器。 使用这种方法,我可以从网络上的任何计算机或虚拟机访问 http://client1.my-machine。 指定“localhost”后,它只能在我的开发计算机上运行。
注意:第一个 VirtualHost 条目用作默认值(如下所述:http://httpd.apache.org/docs/2.2/vhosts/name-based.html)并且是必需的,以便请求不会默认发送到自定义站点之一。
我还将权限设置添加到 httpd.conf,如 @Eddie 的回答。 这并不总是必需的,但我遇到了 2 个不同的情况,需要进行此更改:
AllowOverride
选项的注释可以让错误的原因显而易见,但我之前忽略了这一点。 将其更改为“全部”修复了错误。请注意,在编辑 httpd.conf 时,您可能需要取消注释以下行(默认情况下已为我注释掉),否则上面所做的 vhosts 更改将不会生效:
最后,我还必须将自定义域名添加到我的主机文件如上面的评论所述。 在 OSX 上,您可以通过编辑“/private/etc/hosts”(在 Windows 上为“Windows/System32/drivers/etc/hosts”)并添加以下行来完成此操作:
注意:默认的 OSX Finder UI,隐藏文件夹(包括 /private)不可见。 您可以通过破解内部 Finder 选项(详细信息请参阅 Google)来永久更改此设置,或者更简单地进行偶尔的更改,只需使用“转到 > 转到文件夹”菜单选项,该选项将允许您直接按名称打开隐藏文件夹。 就我个人而言,我使用一个名为 PathFinder 的第三方 OSX shell,我衷心推荐它(它值得小许可证费用)。 它包括一个隐藏/显示隐藏文件的菜单选项以及许多其他有用的功能。
麻烦的一件事是,我还必须在 Windows VM 主机文件中添加指向我的物理开发机器的匹配条目,以便 URL 可以通过 Apache/OSX 解析:
我不需要单独的机器名称条目(自动解析)但是如果没有这些主机条目,向其中添加子域将无法正确解析。 有时我的 Mac 的 IP 会发生变化(通过 DHCP),这确实会带来麻烦,但这只是一个小麻烦。 我假设我可以将其设置为不需要这些 IP,但我无法弄清楚并准备继续:)(如果有人知道答案,请发表评论)
现在我有多个客户端站点在一个中运行放置并可从我的所有开发/测试环境访问。 希望这对其他人有帮助。
@Eddie's answer helped me a lot, but I had to still do a little extra research to solve the same problem for myself using XAMPP on OSX. I thought I would add my full solution here for the benefit of posterity.
First I added the following entries to httpd-vhosts.conf (under the "etc/extra/" folder in XAMPP):
Note that I specifically used a wildcard instead of "localhost" for the VirtualHost urls and added the ServerName config where I specified each subdomain name. Note also that I used my machine's name ("my-machine") instead of "localhost" -- that way all requests from any machine (not just localhost) can be properly evaluated. I develop in OSX but test browsers in various VM's via Parallels. Using this approach I can access http://client1.my-machine from any machine or VM on my network. With "localhost" specified it would only work on my development machine.
NOTE: The first VirtualHost entry is used as the default (as explained here: http://httpd.apache.org/docs/2.2/vhosts/name-based.html) and is required so that requests do not default to one of the custom sites.
I also added the permissions settings to httpd.conf as shown in @Eddie's answer. This is not always required, but I ran into 2 separate cases where I needed to make this change:
AllowOverride
option makes the cause of the error obvious, but I had overlooked that before. Changing this to "All" fixed the error.Note that while editing httpd.conf, you may need to uncomment the following line (it was commented out for me by default), or the vhosts change made above will not take effect:
Finally, I also had to add the custom domain names to my hosts file as noted in the comments above. On OSX, you do this by editing "/private/etc/hosts" (on Windows this would be "Windows/System32/drivers/etc/hosts") and added the following lines:
NOTE: In the default OSX Finder UI, hidden folders (including /private) are not visible. You can change this permanently by hacking internal Finder options (Google for details), or more simply to make an occasional change, just use the "Go > Go to folder" menu option which will let you open hidden folders directly by name. Personally, I use a third party OSX shell called PathFinder that I would heartily recommend (it is worth the small license fee). It includes a menu option to hide/show hidden files, among many other useful features.
One thing that's a drag is that I also did have to add matching entries in my Windows VM hosts file pointing to my physical dev machine so that the urls would resolve via Apache/OSX:
I don't need an entry for the machine name alone (that resolves automatically) but adding the subdomain to it does not resolve correctly without those host entries. This does suck in that on occasion my Mac's IP changes (via DHCP), but it's a minor nuisance. I would assume that I could set it up to not need those IP's, but I could not figure that out and am ready to move on :) (If someone knows the answer please leave a comment)
Now I have multiple client sites running in one place and accessible from all of my dev/test environments. Hope this helps someone else.
我推荐的是虚拟主机,这样您就可以在本地提供“外部站点”,而不会干扰您的默认 Web 服务器。
在 apaches 全局配置文件或包含的 vhost.conf 中;
首先指定以下内容来控制权限并设置总体全局站点
您可以通过在 apache 的全局服务器配置中
What I would recommend is vhosts so you can serve up "alien site" locally without messing with your default web server.
In apaches global config file or included vhost.conf;
You can control permissons and set a overall global site by specifying the below first
in apache's global server config
对于当前进程,您可以执行
$_SERVER["document_root"] = "whatever";
不过要小心。
For the current process, you can just do
$_SERVER["document_root"] = "whatever";
Be careful though.
这是特定于服务器的设置。 如果您正在运行 Apache,您所需要做的就是编辑
httpd.conf
文件(在基于 Unix 的系统上,它应该位于/etc/apache2/httpd .conf
或/etc/httpd/httpd.conf
,具体取决于您拥有的 Apache 版本)。 文件中应该有一行如下所示:从技术上讲,Eli 的方法也有效,但一般来说,我认为编辑服务器变量并不是一个好主意。
It's a server-specific setting. If you're running Apache, all you'll need to do is edit your
httpd.conf
file (on a Unix-based system, it should be either in/etc/apache2/httpd.conf
or/etc/httpd/httpd.conf
, depending on which version of Apache you have). There should be a line in the file that looks like this:Technically, Eli's way works as well, but I don't think editing server variables is really a good idea, in general.
也许您会发现以下网站很有用。 有一些简单易懂的教程向您展示了要操作哪些设置才能让您满意。 我发现那里有很多解释。
Tanguay 的教程
Maybe you would find the following site useful. There are simple-to-follow tutorials that show you just what settings to manipulate to set up this'n'that to you satisfaction. I found a lot explained there.
Tanguay's tutorial