Centos下使用php呼叫shell指令碼

OldBoy~發表於2018-02-03

我們在實際專案中或許會遇到php呼叫shell指令碼的需求。下面就用簡單案例在Centos環境下實踐

準備

檢視php.ini中配置是否開啟安全模式

//php.ini
safe_mode =   //這個如果為off下面兩個就不用管了。我用的是php7,預設沒有當前配置項
disable_functions = 
safe_mode_exec_dir=

因為safe_mode配置項預設沒有,那麼我修改了php.ini中的disable_function選項,把其中一個被禁用的函式去掉,去掉【passthru】函式

disable_functions = exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen   //這裡我去掉了passthru

儲存修改,重啟php

測試Shell指令碼和php程式碼

<?php
//php程式碼
passthru('/data/wwwroot/default/shell/test.sh',$returnvalue);
if ($returnvalue != 0){
//we have a problem!
echo "wrong";
//add error code here
}else{
//we are okay
echo "ok";
//redirect to some other page
}
//shell指令碼
#!/bin/bash
static_dir="/data/wwwroot/default/test_dir"
mkdir $static_dir

php cli模式執行php程式碼。目錄建立成功~

 

相關文章