如何在本地PHP开发人员设置中设置DOCUMENT_ROOT和站点根目录? [英] How to set the DOCUMENT_ROOT and site root in my local PHP dev setup?

查看:140
本文介绍了如何在本地PHP开发人员设置中设置DOCUMENT_ROOT和站点根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个在线站点的人做一份工作.对我来说,这是一个陌生的站点,我正在慢慢研究奇怪的代码.我在本地拥有MAMP,而我的 http://localhost/具有许多与此相关的客户端文件夹.在这段代码中,有许多$ _SERVER ['document_root']命令和引用,例如,这些引用和引用在我的本地PHP开发人员区中正逐渐消失.

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.

我如何轻松地将document_root引用设置为应该的引用(不过只是在本地,并不真正想要弄乱站点文件,因为我需要再次上传它们,并且不想破坏实时环境有一种间接设置的方法,PHP认为该站点的根目录是root,因此图像的src引用"/images/..."将正确显示...我对该站点的本地PHP开发URL是: http://localhost:8888/_CLIENTS/clientsite/www/ ...但是在代码中'/images/...'开头的'/'引用 http://localhost:8888/ ??

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/ ??

谢谢.

推荐答案

@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.

首先,我将以下条目添加到httpd-vhosts.conf中(在XAMPP中的"etc/extra/"文件夹下):

First I added the following entries to httpd-vhosts.conf (under the "etc/extra/" folder in XAMPP):

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/"
</VirtualHost>
<VirtualHost *:80>
    ServerName client1.my-machine
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client1/"
</VirtualHost>
<VirtualHost *:80>
    ServerName client2.my-machine
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client2/"
</VirtualHost>

请注意,我专门为VirtualHost url使用了通配符而不是"localhost",并在指定每个子域名的位置添加了ServerName配置.还要注意,我使用了机器的名称("my-machine")而不是"localhost",这样可以正确评估来自任何机器(不仅是localhost)的所有请求.我使用OSX开发,但是通过Parallels在各种VM中测试浏览器.使用这种方法,我可以从网络上的任何计算机或VM访问 http://client1.my-machine .指定"localhost"只能在我的开发计算机上工作.

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.

注意:第一个VirtualHost条目用作默认条目(如下所述:

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.

我还将权限设置添加到httpd.conf中,如

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:

  • 在默认XAMPP http根目录之外的目录中放置一个新站点(基本上,任何可通过http访问的路径都必须设置显式权限)
  • 为设置为虚拟主机的站点添加mod_rewrite规则(在默认的http路径下)时,出现错误".htaccess:此处不允许RewriteEngine".阅读httpd.conf中有关AllowOverride选项的注释使错误的原因显而易见,但是我之前忽略了这一点.将其更改为全部"可修复错误.
  • Basing a new site in a directory outside of the default XAMPP http root (basically any path that will be accessible via http must have explicit permissions set)
  • Adding mod_rewrite rules for a site set up as a virtual host (under the default http path), I was getting the error ".htaccess: RewriteEngine not allowed here". Reading the comments in httpd.conf about the AllowOverride option makes the cause of the error obvious, but I had overlooked that before. Changing this to "All" fixed the error.

请注意,在编辑httpd.conf时,您可能需要取消注释以下行(默认情况下已为我注释掉),否则上面所做的vhosts更改将不会生效:

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:

# Virtual hosts
Include /Applications/xampp/etc/extra/httpd-vhosts.conf

最后,我还必须将自定义域名添加到我的主机文件中,如上面的注释中所述.在OSX上,您可以通过编辑"/private/etc/hosts"(在Windows上为"Windows/System32/drivers/etc/hosts")来做到这一点,并添加以下几行:

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:

127.0.0.1   my-machine
127.0.0.1   client1.my-machine
127.0.0.1   client2.my-machine

注意:在默认的OSX Finder UI中,隐藏的文件夹(包括/private)不可见.您可以通过更改内部Finder选项(Google以获得详细信息)来永久更改此设置,或者更简单地进行偶尔的更改,只需使用转到>转到文件夹"菜单选项,即可通过名称直接打开隐藏的文件夹.就个人而言,我会衷心推荐使用名为 PathFinder 的第三方OSX Shell(值得小许可证费用).除了许多其他有用的功能外,它还包括一个用于隐藏/显示隐藏文件的菜单选项.

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.

一件令人费解的事情是,我还必须在指向我的物理开发机的Windows VM主机文件中添加匹配的条目,以便可以通过Apache/OSX解析这些网址:

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:

192.168.1.5 client1.my-machine
192.168.1.5 client2.my-machine

我不需要单独输入机器名称(它会自动解析),但是如果没有这些主机输入,则向其中添加子域将无法正确解析.有时候,我的Mac的IP(通过DHCP)更改了IP,但这确实是个小麻烦.我认为我可以将其设置为不需要那些IP,但是我无法弄清楚并准备继续进行:)(如果有人知道答案,请发表评论)

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.

这篇关于如何在本地PHP开发人员设置中设置DOCUMENT_ROOT和站点根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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