1

I'm working with a LAMP server with several sites configured as subdomains in Apache. As standard, each site/subdomain lives in a tidy directory inside /var/www/. I've told Apache where each site lives in the /etc/apache2/sites-available/ config files, using several VirtualHosts and specifying DocumentRoot and ServerName for each.

When I'm writing javascript on pages, I can reference other pages based on the site root, like this:

include '/includes/something.js';

But I can't do the same thing in PHP. If I want to include a file in PHP code, I need to type:

include '/var/www/site_name/includes/something.php';

(I can still do relative references, such as ../includes/something.php, but that's beside the point here.) I was confused on why I would need the /var/www/, but up to now it was just an annoyance. I could code around it, once I figured out that the full directory was needed. But now I'm trying to install PHPlist, and every time I save a setting, I'm redirected to a URL like https://phplist.mydomain.org/var/www/phplist/admin/?page=configure - which of course gives me a 'Page not found' error.

It looks like something is messed up about how my Apache / PHP setup treat the site root. I haven't dug into the PHPlist code, and don't particularly want to. But I assume that if I can fix my settings such that I can include files in PHP without needing the /var/www/ prefix, then PHPlist will also stop giving me over-prefixed URLs.

For reference, here's how most of my Apache virtual hosts look. I guess my specific questions are: 1) do you see anything in what I've told you, that needs correcting? 2) where would you look or what would you try next to figure out this weirdness?

<VirtualHost 95.102.96.250:443>
  ServerName phplist.mydomain.org
  DocumentRoot /var/www/phplist
  ServerAdmin admin@mydomain.org

  <Directory /var/www/phplist/>
    Options +FollowSymLinks
    AllowOverride All
    order allow,deny
    allow from all
  </Directory>

</VirtualHost>

Hopefully this is just a basic misunderstanding on my part. I've tried RingTFM but haven't been able to figure out what's happening here. Thanks in advance!

4

3 回答 3

3

Javascript 是一种客户端脚本语言。因此,您的浏览器/在处理您的请求 URI(从浏览器到服务器的消息)时将其视为您的域的根。

PHP 是一种服务器端语言,因此它需要知道文件的路径。您可以在具有相对路径的同一文件夹中包含文件。此示例将在包含文件夹中:

// so /includes/header.php might include /includes/config.php like this
include('config.php');

最好使用服务器变量包含这样的代码移动路径:

include($_SERVER['DOCUMENT_ROOT'].'/includes/config.php');

为了便于编程,您可能会创建一个$includes_dir变量:

$includes_dir = $_SERVER['DOCUMENT_ROOT'].'/includes';
include($includes_dir.'/config.php');
于 2013-04-30T18:56:38.033 回答
2

一些基于其工作方式的虚拟服务器不使用$_SERVER['DOCUMENT_ROOT']指向共享托管文档根目录,它们指向不正确的 /var/www。但是,它们确实提供$_SERVER['VIRTUAL_DOCUMENT_ROOT']了您应该检查的内容。

于 2013-05-05T17:27:59.597 回答
2

$_SERVER['DOCUMENT_ROOT']也只解析为 Apache 中设置的任何 DocumentRoot。您不应该$_SERVER['DOCUMENT_ROOT']在共享服务器或虚拟主机环境中使用,因为您可能并不总是拥有正确的目录。

建议在您的索引文件中使用类似

define('DOCUMENT_ROOT', dirname(__FILE__));

DOCUMENT_ROOT这是否会创建一个始终正确指向应用程序根目录的全局定义。

于 2013-04-30T19:05:19.553 回答