我希望能够在 php 文件中使用 shell_exec() 远程删除/添加开发横幅。从 php/apache 运行时,sed -i 函数不起作用。SELINUX 被禁用。以另一个用户身份从终端运行 test.sh 就可以了。浏览器返回以下内容:
Reset/Set Environments
Running now...
/sbin/nologin
sed is /bin/sed
sed: couldn't open temporary file /var/www/html/folder/folder/folderwithoutwritepermission/sed63Klhk: Permission denied
EOF
这是代码:
PHP 文件:
<!DOCTYPE HTML>
<html>
<head>
<title>Reset/Set Environments</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Reset/Set Environments</h1>
<?php
$output = shell_exec('sh /var/www/html/pages/test.sh');
echo("<pre>{$output}</pre>");
?>
</body>
</html>
测试.sh:
#!/bin/bash
cd /var/www/html/pages/;
echo "Running now...";
echo $SHELL;
type -a sed;
find -L /var/www/html/ -name 'template.php' 2>&1 | xargs sed -i 's/<body>/<body><div style=\"position: fixed; padding: 14px; background-color: #00ff00; width:100%; opacity: 0.5;\"><span style=\"color: #009900; font-size: 14px; font-weight: bold;\">DEVELOPMENT ENVIRONMENT<\/span><\/div>/g' 2>&1
echo "EOF";