2017-09-05 3 views
0

My dev machine과 내 서버에는 설치된 다른 python 버전에 대한 경로가 다릅니다. fastCGI 스크립트 내에서 실행 경로 얻기

내가이

$> php -r 'require("./class.my.php"); $path=MyClass::pythonPath("2.7"); var_dump($path); var_dump(file_exists($path));' 
string(18) "/usr/bin/python2.7" 
bool(true) 

을 할 수있는 내 dev에 컴퓨터에서이 방법

static function pythonPath ($version='') { 
    $python = $version === '' ? 'python': ''; 
    if (preg_match('/^\d(\.?\d)?$/', $version)) { 
     $python = 'python'.$version; 
    } 
    return trim(shell_exec("/usr/bin/which $python 2>/dev/null")); 
} 

을 만들어 서버에 나는이

을 얻을 특정 파이썬 실행 파일의 올바른 경로를 얻으려면
$> php -r 'require("./class.my.php"); $path=MyClass::pythonPath("2.7"); var_dump($path); var_dump(file_exists($path));' 
string(27) "/opt/python27/bin/python2.7" 
bool(true) 

그러나이 방법을 fastCGI에서 사용하면 which의 결과는 비어 있습니다 (CentOS 6). 내가 읽은만큼 which은 사용자의 $PATH을 검색합니다. 그리고 이것은 내가 스크립트를 실행하는 사용자 (나의 추측 httpd)가 계정 사용자와 같은 경로가 아니기 때문에 which python2.7에 대한 결과를 얻지 못하는 이유 일 수 있습니다.

그렇다면 fastCGI 스크립트에서 실행 경로를 어떻게 찾을 수 있습니까?

사용자 경로가 서로 다릅니다. 테스트되지 않은 추측 : which을 사용하고 내 서버 계정의 전체 경로 변수를 가져 와서 which 앞에로드하십시오.

답변

0

내 서버에서 스크립트는 "nobody"사용자에 의해 실행됩니다.

스크립트 내에서 $PATH을 인쇄하면 /usr/bin이 fastCGI 스크립트를 실행하는이 사용자에 대해 실행 가능한 유일한 바이너리 경로임을 나타냅니다.

which을 실행하기 전에 트릭이 내 사용자 환경 변수를 소싱하고있었습니다.

bash 프로필 파일의 이름이 다를 수 있으므로 스크립트 디렉토리가 달라질 수 있으므로이 기능을 사용하여 올바른 경로를 얻었습니다.

static function getBashProfilePath() { 
    $bashProfilePath = ''; 
    $userPathData = explode('/', __DIR__); 
    if (!isset($userPathData[1]) || !isset($userPathData[2]) || $userPathData[1] != 'home') { 
     return $bashProfilePath; 
    } 

    $homePath = '/'.$userPathData[1].'/'.$userPathData[2].'/'; 
    $bashProfileFiles = array('.bash_profile', '.bashrc'); 

    foreach ($bashProfileFiles as $file) { 
     if (file_exists($homePath.$file)) { 
      $bashProfilePath = $homePath.$file; 
      break; 
     } 
    } 

    return $bashProfilePath; 
} 

최종 구현은 파이썬 바이너리 경로가이

static function pythonPath ($version='') { 
    $python = $version === '' ? 'python': ''; 
    if (preg_match('/^\d(\.?\d)?$/', $version)) { 
     $python = 'python'.$version; 
    } 

    $profileFilePath = self::getBashProfilePath(); 
    return trim(shell_exec(". $profileFilePath; /usr/bin/which $python 2>/dev/null")); 
} 
했다 얻을 수
관련 문제