Centos下使用php调用shell脚本

时间:2023-03-09 23:37:43
Centos下使用php调用shell脚本

我们在实际项目中或许会遇到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 != ){
//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代码。目录创建成功~