最近做了一個項目需要 php+shell_exec() 調用python,在win下能正常調用到,然而轉移到centos上時結果一直是null,各種嘗試后總結如下:
1.檢查 php.ini disable_functions= 中是否有shell_exec,若有則刪除,保存后重啟php
2.python 要用全路徑,比如 shell_exec('/opt/python37/bin/python3 /xxx/xx/xx/x.py'),而不是只寫 shell_exec('python3 /xxx/xx/xx/x.py')
3.可以在shell_exec()括號內字符串的最后加上 2>&1 以返回STDERR以及STDOUT。例如 shell_exec('python3 /xxx/xx/xx/x.py 2>&1') 它就報 "sh: python3: command not found\n"