简体   繁体   中英

Apache & PHP: DocumentRoot isn't working as expected

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!

Javascript is a client side scripting language. So your browser treats / as the root of your domain when it processes your request URI (the message from the browser to the server).

PHP is a server side language, so it needs to know the path to the file. You can include a file in the same folder with a relative path. This example would be inside the includes folder:

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

It's best to include the path for mobility of code like this using server vars:

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

You might for the sake of ease in programming make an $includes_dir variable:

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

$_SERVER['DOCUMENT_ROOT'] also only resolves to whatever DocumentRoot is set to in Apache. You should not use $_SERVER['DOCUMENT_ROOT'] in a shared server or virtual host environment as you may not always have the correct directory.

it is recommended that in your index file you use something like

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

is this creates a globally defined DOCUMENT_ROOT that always correctly points to the root of your app.

Some virtual servers based on how they work don't use $_SERVER['DOCUMENT_ROOT'] to point to a shared hosting document root, they point to /var/www which is incorrect. they do however provide $_SERVER['VIRTUAL_DOCUMENT_ROOT'] which is something you should check for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM