且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从命令行执行获取Apache文档根(无浏览器)

更新时间:2022-10-18 18:01:56

简单答案:你不能。 >

由于NikiC在对你的问题的评论中已经指出,从命令行调用脚本意味着没有可用的Web服务器上下文的概念。 主机名和文档根的概念仅存在于Web服务器上下文中,并且基于服务器的配置。您的 / var / www / something 目录中没有显示嘿,我是文档根目录。



为了说明,假设你有一个Apache配置有两个vhosts,使用两个文档根:




  • vhost A,使用 / var / www / top-docroot

  • vhost B,使用 / var / www / top-docroot / nested-docroot



您的指令码位于:



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



当从命令行调用时,您的脚本应该使用什么路径?




  • vhost A会调用嵌套-docroot / path / to / xmlrpc.server.php

  • vhost B会调用 path / to / xmlrpc.server.php



虽然这是一个非常有用的示例,但它仍然应该证明文档根可以在Web服务器请求的上下文中可靠地确定,因为它是从配置中读取的。



所以当从命令行调用时,所有脚本都可以尝试从Web服务器配置(其中没有可靠的位置)获取信息,或者使用一些启发式方法,例如假设文档根驻留在 / var / www $ c下的公共约定$ c>。这两种方法都相当不可靠,你可能更喜欢坚持使用硬编码值(或者在调用时将信息作为参数传递)。


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";

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