gpt4 book ai didi

php - 在网络服务器上设置 PHP 文档根目录

转载 作者:行者123 更新时间:2023-12-02 07:28:29 24 4
gpt4 key购买 nike

目前我有一个运行 apache2 的 Ubuntu 12.04 网络服务器。我将它设置为通过在/sites/example.com/*/public 下创建新文件夹来动态创建子域/sites/example.com/www/public 是为我的主根站点保留的。

这正在解决,但是我无法将 PHP 的 $_SERVER['DOCUMENT_ROOT'] 配置为动态到新创建的文件夹。

当我回显 $_SERVER['DOCUMENT_ROOT'] 我得到 /etc/apache2/htdocs 我认为这是某种默认路径。我希望这是:/sites/example.com/*/public

# Wildcards
<VirtualHost *:80>
VirtualDocumentRoot /sites/example.com/%1/public
ServerAdmin mike@example.com
ServerAlias *.example.com
</VirtualHost>

很好奇为什么 PHP 没有选择上面的虚拟文档根设置,我可能做错了什么。

最佳答案

解决方案位于:http://joshbenner.me/blog/quick-tip-get-proper-document-root-when-using-mod-vhost-alias/

The Apache module mod_vhost_alias and its VirtualDocumentRoot directive can really be a great time saver for local development (some googling will explain why in more deapth). Basically, my local dev is set up so that I just have to create a directory in my aliases directory, and I just then navigate my browser to a URL matching the name of that new directory, and apache knows exactly what to serve automagically.

However, there are a few evil gotchas when using mod_vhost_alias, one of which is that the PHP global $_SERVER['DOCUMENT_ROOT'] remains set to the apache default DOCUMENT_ROOT environment variable rather than being re-assigned to the document root activated by the VirtualDocumentRoot directive for the current URL. This can cause some PHP applications (that are too trusting) to die for one reason or another.

I found a great solution to this in the related apache bug report: Simply add the following line to your apache configuration inside the VirtualDocumentRoot vhost definition:

php_admin_value auto_prepend_file /path/setdocroot.php

Then, create the referenced PHP file, and put set this as its contents:

<?php $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']); ?>

Now, every page load has this file executed, which properly sets DOCUMENT_ROOT.

关于php - 在网络服务器上设置 PHP 文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25017381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com