测试环境, centos8下通过yum install nginx安装的nginx, php7.3
[root@localhost ~]# cat /etc/nginx/nginx.conf
...
root /usr/share/nginx/html;
可以找到我们可以执行的默认目录为 /usr/share/nginx/html
[root@localhost ~]# cd /usr/share/nginx/html
[root@localhost html]# ll -la
总用量 44
drwxr-xr-x. 2 root root 173 7月 6 12:00 .
drwxr-xr-x. 4 root root 33 7月 6 00:55 ..
-rw-r--r--. 1 root root 3971 10月 7 2019 404.html
-rw-r--r--. 1 root root 4020 10月 7 2019 50x.html
-rw-r--r--. 1 root root 4057 10月 7 2019 index.html
-rw-r--r--. 1 root root 368 10月 7 2019 nginx-logo.png
-rw-r--r--. 1 root root 4148 10月 7 2019 poweredby.png
-rw-r--r--. 1 root root 497 7月 6 03:25 publish_code.php
-rw-r--r--. 1 root root 12288 7月 6 03:26 .publish_code.php.swp
-rwxrwxrwx. 1 root root 20 7月 6 03:18 test_shell.sh
[root@localhost html]# vim publish_code.php
<?php
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : '';
$cmd = str_replace(["../","./","/"],"",$cmd);
if(!$cmd) {
die('参数不能为空');
}
echo $cmd;
echo "<pre>";
system('sh /usr/share/nginx/html/' . $cmd . '.sh', $status);
echo '</pre>';
if($status) {
echo "成功";
} else {
echo "失败";
}
[root@localhost html]# vim test_shell.sh
#!/bin/bash
ls -la
chmod 777 test_shell.sh
[root@localhost html]# curl http://127.0.0.1/publish_code.php?cmd=test_shell
test_shell<pre>total 44
drwxr-xr-x. 2 root root 173 Jul 6 12:00 .
drwxr-xr-x. 4 root root 33 Jul 6 00:55 ..
-rw-r--r--. 1 root root 12288 Jul 6 03:26 .publish_code.php.swp
-rw-r--r--. 1 root root 3971 Oct 7 2019 404.html
-rw-r--r--. 1 root root 4020 Oct 7 2019 50x.html
-rw-r--r--. 1 root root 4057 Oct 7 2019 index.html
-rw-r--r--. 1 root root 368 Oct 7 2019 nginx-logo.png
-rw-r--r--. 1 root root 4148 Oct 7 2019 poweredby.png
-rw-r--r--. 1 root root 497 Jul 6 03:25 publish_code.php
-rwxrwxrwx. 1 root root 20 Jul 6 03:18 test_shell.sh
</pre>执行成功