Get Apache Document Root from command line executi

2019-04-21 04:17发布

Not sure if the title is correct, Please edit if you think of a better one.

I have a XMLRPC service that I call from the command line. It's using Zend framework.

the client looks like this:

$server = new Zend_XmlRpc_Client('http://hostname/path/to/xmlrpc.server.php');

The file is located:

/var/www/html/path/to/xmlrpc.server.php

I have it hard coded now but wanted to populate the 'path/to/' generically.

I've tried:

function url(){
  $protocol = $_SERVER['HTTPS'] ? "https" : "http";
  return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

echo url();

Note: None of the $_SERVER options returned what I needed

but executing this from the command line gives me:

http://

Also getcwd() give me:

 /var/www/html/path/to

How can I get:

http://path/to

Any thoughts?

The reason I would like this is if the project needs to change directories it should auto configure. Example:

If I move the project here:

/var/www/html/path/to/another/location

or here:

/var/www/public_html/path/to/another/location

or even here:

/path/to/document/root/path/to/another/location

I should get:

http://hostname/path/to/another/location

Thanks for any help

UPDATE:

I tried this but still not working as expected:

$hostname = `hostname`;
echo 'http://'.trim($hostname).'/'.basename(getcwd())."\n";

5条回答
我命由我不由天
2楼-- · 2019-04-21 04:24

http://www.php.net/getcwd

Works perfectly when you're running a CLI app. Just make sure you know in which subdirectory you're running the application from and apply something with dirname() accordingly.

For example, if your docroot is set to: /var/www and your CLI app is inside /var/www/jobs/foobar/myapp.php, you could do something like this:

Method 1:

// Store the current docroot
$cwd = getcwd(); 
// Change directory and get the absolute path using getcwd again.
chdir($cwd . '/../../'); 
$docroot = getcwd();
// Restore the original cwd.
chdir($cwd);

Method 2:

// Get absolute path of docroot:
$docroot = dirname(getcwd() . '/../../');
查看更多
SAY GOODBYE
3楼-- · 2019-04-21 04:34

This might be sort of a hack job, but i just tried it and it worked nicely:

$fullPath = dirname(__FILE__);
$wwwRoot = getenv("DOCUMENT_ROOT");
$list = explode($wwwRoot, $fullPath);

echo $list[1];

Like I said, I'm sure it's far from perfect; but it's a start!

查看更多
爷、活的狠高调
4楼-- · 2019-04-21 04:36

Doesn't $_SERVER['SCRIPT_FILENAME'] get you the path you need? As I understand it this should get you the absolute path when requested through the webserver, which I assume you're doing judging by the http url in your post. When called through the php command line it could be a relative path, which should be relative to cwd(). More info here

查看更多
Root(大扎)
5楼-- · 2019-04-21 04:45

I always use these below and they work everywhere, either be called from CLI or any script or when being included under sub script(s):

// Application Paths:
echo "<br>appPath:".$appPath = dirname(__FILE__);
echo "<br>appParent:".$appParent = dirname($appPath);
echo "<br>appGrandParent:".$appGrandParent = dirname($appParent);
查看更多
聊天终结者
6楼-- · 2019-04-21 04:46

Simple Answer: You can't.

As NikiC already stated in his comment to your question, invoking the script from the command line implies that there is no notion of a web server context available. The concept of a 'hostname' and a 'document root' only exist in the web server context, and are based on the configuration of the server. There is nothing in your /var/www/something directory that says 'Hey, I am a document root'.

To illustrate, assume you have an Apache configured with two vhosts, using two document roots:

  • vhost A, using /var/www/top-docroot
  • vhost B, using /var/www/top-docroot/nested-docroot.

Your script is located at:

/var/www/top-docroot/nested-docroot/path/to/xmlrpc.server.php

What path should your script use, when invoked from the command line?

  • vhost A would call for nested-docroot/path/to/xmlrpc.server.php
  • vhost B would call for path/to/xmlrpc.server.php

While this is a pretty contrived example, it should still demonstrate the point that a document root can only be reliably determined in the context of a web server request, as it is read from configuration.

So when invoked from the command line, all your script could do would be to try to get the information from the web server configuration (which has no reliable place either), or to use some heuristics, such as assuming the common convention of document roots residing under /var/www. Both methods would be pretty unreliable, and you are probably better of sticking to hard coded values (or passing the information as a parameter on invocation).

查看更多
登录 后发表回答