如何在本地 PHP 开发设置中设置 DOCUMENT_ROOT 和站点根目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/789816/
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-24 23:50:47  来源:igfitidea点击:

How to set the DOCUMENT_ROOT and site root in my local PHP dev setup?

phpsettingsdocumentlocalhostroot

提问by

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.

我正在为一个拥有在线网站的人工作。这对我来说是一个陌生的网站,我正在慢慢研究奇怪的代码。我在本地有 MAMP,而我的http://localhost/有许多客户端文件夹从中分离出来。在这段代码中,有很多 $_SERVER['document_root'] 命令和引用,它们在我的本地 PHP 开发区中丢失了。

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

我怎样才能轻松地将 document_root 引用设置为它应该是什么(虽然只是在本地,但真的不想弄乱站点文件,因为我需要再次上传它们并且不想破坏实时站点!和有没有一种间接设置的方法,PHP 认为站点的根是这样的图像的 src 引用“/images/...”将正确显示...我的本地 PHP 开发 URL 为该站点是:http:// localhost:8888/_CLIENTS/clientsite/www/...但在代码中'/images/...'开头的'/'引用了http://localhost:8888/??

Thank you.

谢谢你。

回答by Brian Moeskau

@Eddie's answerhelped 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.

@Eddie 的回答对我帮助很大,但我仍然需要做一些额外的研究才能在 OSX 上使用 XAMPP 为自己解决同样的问题。我想我会在这里添加我的完整解决方案,以造福子孙后代。

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

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

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>

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-machinefrom any machine or VM on my network. With "localhost" specified it would only work on my development machine.

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

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.

注意:第一个 VirtualHost 条目用作默认值(如此处所述:http: //httpd.apache.org/docs/2.2/vhosts/name-based.html)并且是必需的,因此请求不会默认为自定义站点。

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:

我还向 httpd.conf 添加了权限设置,如@Eddie's answer 所示。这并不总是必需的,但我遇到了 2 个不同的情况,我需要进行此更改:

  • 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 AllowOverrideoption makes the cause of the error obvious, but I had overlooked that before. Changing this to "All" fixed the error.
  • 将新站点建立在默认 XAMPP http 根目录之外的目录中(基本上任何可通过 http 访问的路径都必须设置显式权限)
  • 为设置为虚拟主机(在默认 http 路径下)的站点添加 mod_rewrite 规则,我收到错误“.htaccess:此处不允许 RewriteEngine”。阅读 httpd.conf 中关于该AllowOverride选项的注释可以使错误的原因显而易见,但我之前忽略了这一点。将此更改为“全部”修复了错误。

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:

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

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

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:

最后,我还必须将自定义域名添加到我的主机文件中,如上面的评论中所述。在 OSX 上,您可以通过编辑“/private/etc/hosts”(在 Windows 上这将是“Windows/System32/drivers/etc/hosts”)并添加以下行来完成此操作:

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

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

注意:在默认的 OSX Finder UI 中,隐藏文件夹(包括 /private)不可见。您可以通过破解内部 Finder 选项(Google 获取详细信息)来永久更改此设置,或者更简单地进行偶尔更改,只需使用“前往 > 前往文件夹”菜单选项,即可直接按名称打开隐藏文件夹。就个人而言,我使用名为PathFinder 的第三方 OSX shell ,我强烈推荐它(值得支付少量许可费)。它包括一个菜单选项来隐藏/显示隐藏文件,以及许多其他有用的功能。

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:

一件很麻烦的事情是,我还必须在指向我的物理开发机器的 Windows VM 主机文件中添加匹配的条目,以便 url 可以通过 Apache/OSX 解析:

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

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)

我不需要单独的机器名称条目(自动解析),但是如果没有这些主机条目,将子域添加到它就无法正确解析。有时我的 Mac 的 IP 会发生变化(通过 DHCP),这确实很糟糕,但这只是个小麻烦。我假设我可以将它设置为不需要这些 IP,但我无法弄清楚并准备继续:)(如果有人知道答案,请发表评论)

Now I have multiple client sites running in one place and accessible from all of my dev/test environments. Hope this helps someone else.

现在我有多个客户端站点在一个地方运行,并且可以从我的所有开发/测试环境访问。希望这对其他人有帮助。

回答by Eddie

What I would recommend is vhosts so you can serve up "alien site" locally without messing with your default web server.

我推荐的是虚拟主机,这样您就可以在本地提供“外来站点”,而不会弄乱您的默认 Web 服务器。

  • localhost -> your start page or whatever
  • alien.localhost -> clients site, whatever path / doc root you want.
  • x.localhost -> another site
  • 本地主机 -> 您的起始页或其他任何内容
  • Alien.localhost -> 客户端站点,您想要的任何路径/文档根目录。
  • x.localhost -> 另一个站点

In apaches global config file or included vhost.conf;

在 apaches 全局配置文件或包含的 vhost.conf 中;

NameVirtualHost localhost:80
# the mysql tool's url
<VirtualHost phpmyadmin.localhost:80>
# and absolute path
DocumentRoot "/srv/www/phpMyAdmin/"
</VirtualHost>

#Same for the Client Site
<VirtualHost foo.localhost:80>
DocumentRoot "/path/to/desired/webroot/"
</VirtualHost>

You can control permissons and set a overall global site by specifying the below first

您可以通过首先指定以下内容来控制权限并设置全局站点

in apache's global server config

在 apache 的全局服务器配置中

DocumentRoot "/srv/www/htdocs"
#
# Configure the DocumentRoot Properties
#
<Directory "/srv/www/htdocs"> 
    Options All
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    AllowOverride All
    # Controls who can get stuff from this server.
    Order allow,deny
    Allow from all
</Directory>
#
# Configure Sub-Domain Properties. This prevents those nasty 403 errors
#

# mysql administration tool
<Directory "/srv/www/phpMyAdmin/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

# a client web site built with CakePHP
<Directory "/home/eddie/workspace/Digital_Business/app/webroot/">
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

回答by hbw

It's a server-specific setting. If you're running Apache, all you'll need to do is edit your httpd.conffile (on a Unix-based system, it should be either in /etc/apache2/httpd.confor /etc/httpd/httpd.conf, depending on which version of Apache you have). There should be a line in the file that looks like this:

这是特定于服务器的设置。如果您正在运行 Apache,您需要做的就是编辑您的httpd.conf文件(在基于 Unix 的系统上,它应该在/etc/apache2/httpd.conf或 中/etc/httpd/httpd.conf,具体取决于您拥有的 Apache 版本)。文件中应该有一行如下所示:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/whatever/your/document/root/is"

Technically, Eli's way works as well, but I don't think editing server variables is really a good idea, in general.

从技术上讲,Eli 的方法也有效,但我不认为编辑服务器变量真的是一个好主意,一般来说。

回答by Eli

For the current process, you can just do

对于当前的过程,你可以做

$_SERVER["document_root"] = "whatever";

$_SERVER["document_root"] = "随便";

Be careful though.

不过要小心。

回答by Peter Perhá?

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

Tanguay 的教程