2017-09-05 9 views
0

My devマシンと私のサーバーには、インストールされているさまざまな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")); 
} 

作られた特定のPythonの実行可能ファイルの正しいパスを取得するには

$> 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スクリプト内で実行可能パスを見つけるにはどうすればよいですか?

ユーザーパスが異なる。スクリプトは、「誰」ユーザーが実行している私のサーバー上で

答えて

0

:(未テスト推測whichを使用して維持され、最初に私のサーバーアカウントのフル・パス変数を取得し、which前にそれをロードします)。

$PATHのスクリプト内では、/usr/binがfastCGIスクリプトを実行しているこのユーザーに対して実行可能なバイナリパスのみであることが示されます。

whichを実行する前に、私のユーザー環境変数を調達していました。

bashプロファイルファイルは名前とスクリプトディレクトリが異なるため、この関数を使って正しいパスを取得しました。 pythonのバイナリパスを取得するために

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")); 
} 
ました
関連する問題