【发布时间】:2020-11-23 17:00:16
【问题描述】:
我正在尝试为我拥有的一些受控插头插座设置 Web 界面,这些插座由 Raspberry Pi 控制。我想在网页上有几个链接,可以用来打开和关闭开关。我一直在尝试使用 PHP 来执行此操作,但由于某种原因它无法正常工作。
我尝试了各种建议(请参阅下面的链接)。每当我单击链接时,我得到的只是一个白页,它没有做它应该做的事情,即打开和关闭开关。从命令行运行 PHP 脚本按预期工作,问题似乎仅在尝试从网页运行时出现。
我查看了权限,并为脚本设置了权限:
chmod 777 /path/to/script
我尝试将脚本存储在我的主文件夹和/var/www/html 文件夹中,但没有任何乐趣。 NGINX 日志或 PHP-FPM 日志中没有显示任何错误。
我已尝试编辑 sudoers 文件以授予对脚本 (www-data ALL:=/path/to/script/) 的 www-data 访问权限,甚至尝试使用 www-data (www-data ALL=(ALL:ALL) ALL) 的所有权限进行操作,但均未奏效。
我确实认为这可能是因为我尝试运行的脚本涉及启动 SSH 会话,但我什至无法获得本地命令来在 /home/pi/ 目录或 @987654333 中创建空白文件@。
我已将我尝试运行的脚本与我用来调用脚本的 PHP 以及我用来尝试其他命令的第二个 PHP 文件放在下面。
任何正确方向的帮助或指示将不胜感激。我认为脚本正在运行,但它在某处失败,我无法确定在哪里。我在网络浏览器中返回的唯一内容是 echo $username 行,所以我知道它的部分工作原理,但是当我尝试执行命令时没有任何反应。
PHP 脚本:
<?php
$username = posix_getpwuid(posix_geteuid())['name'];
echo $username;
exec("/home/pi/scripts/switch2off");
?>
测试脚本:
<?php
exec("touch /var/www/html/s/test.txt");
?>
SWITCH2OFF 脚本
#! /bin/bash
ssh pi@example 'python /home/pi/switches/switch_2_off.py'
NGINX 配置:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
listen [::]:80;
return 301 https://$server_name$request_uri;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location /.well-known/ {
allow all;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
include snippets/ssl-example.conf;
include snippets/ssl-params.conf;
root /var/www/html;
location / {
limit_req zone=one burst=5;
root /var/www/html;
auth_basic "Please Log In";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_set_header X-Content-Type-Options: nosniff;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Frame-Options "allow-from example.com";
}
location /.well-known/ {
allow all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
error_page 401 403 404 /404.html;
}
PHP-FPM 日志:
[03-Aug-2020 05:00:01] NOTICE: Terminating ...
[03-Aug-2020 05:00:01] NOTICE: exiting, bye-bye!
[03-Aug-2020 05:00:29] NOTICE: fpm is running, pid 620
[03-Aug-2020 05:00:29] NOTICE: ready to handle connections
[03-Aug-2020 05:00:29] NOTICE: systemd monitor interval set to 10000ms
我的研究/我尝试过的事情:
Nginx serves .php files as downloads, instead of executing them - 我从这里开始,因为最初我遇到了一个配置问题,而不是运行 PHP 脚本,而是将它们作为下载提供。
Run a shell script with an html button - 这是我获取 PHP 脚本代码的地方
PHP code is not being executed, instead code shows on the page - 与我看到的问题不完全相同。即使查看源代码,Web 浏览器也不会显示 php 文件中的任何代码
https://askubuntu.com/questions/520566/why-wont-this-php-script-execute-bash-script
https://unix.stackexchange.com/questions/115054/php-shell-exec-permission-on-linux-ubuntu
https://www.linode.com/docs/web-servers/nginx/serve-php-php-fpm-and-nginx/
【问题讨论】:
-
“我得到的只是一个白页” - 你的意思是你甚至没有得到
$username的输出?您能否通过尝试<?php echo "Hello world";来验证 PHP 脚本根本没有运行? -
另外 - 我会尝试生成一个 PHP 错误以查看它是否记录在 fpm-php 日志中。
trigger_error("Hello world");应该这样做。 -
啊好吧!然后通过 Nginx,
phpinfo();是否说启用了安全模式? (如果是,安全模式路径是什么?) -
我在输出中看不到任何显示安全模式的内容,我能在其中找到安全的唯一两件事是线程安全:禁用和 filter.default: unsafe_raw。我刚刚用谷歌搜索它并根据 php 手动安全模式在 5.4.0 中被删除?我正在使用 7.3 php.net/manual/en/features.safe-mode.php
-
可能是
disable_functions?检查您的phpinfo();。不要通过php -i进行检查,否则您将获得 cli 配置。