从命令行执行获取Apache文档根(无浏览器) [英] Get Apache Document Root from command line execution (No browser)

查看:191
本文介绍了从命令行执行获取Apache文档根(无浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道标题是否正确,请编辑,如果你认为一个更好的。



我有一个XMLRPC服务,我从命令行调用。它使用Zend框架。



客户端如下所示:



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



文件位于:

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

我现在硬编码,但想要填充'path / to /'。



我试过:

  function url b $ b $ protocol = $ _SERVER ['HTTPS']? https:http; 
return $ protocol。 ://。 $ _SERVER ['HTTP_HOST']。 $ _SERVER ['REQUEST_URI'];
}

echo url();

注意:没有$ _SERVER选项返回我需要的



但是从命令行执行此命令可以让我:

  http:// 

还有 getcwd()给我:

  / var / www / html / path / to 


b $ b

我如何获得:

  http:// path / to 



任何想法?



我想这样做的原因是,如果项目需要更改目录应该自动配置。示例:



如果我在这里移动项目:

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

或此处:

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

或甚至这里:

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

我应该得到:

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


$ b b

感谢任何帮助



更新:



我尝试了但仍然无法正常工作:

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


解决方案

简单答案:你不能。 >

由于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 。这两种方法都相当不可靠,你可能更喜欢坚持使用硬编码值(或者在调用时将信息作为参数传递)。


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

这篇关于从命令行执行获取Apache文档根(无浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆