简体   繁体   English

PHP执行python脚本

[英]PHP execute python script

I want to turn an LED on and off through a website (index.php) which is located in "/var/www/html" the two scripts are located in "/var/www/html/wspace/Server/" . 我想通过位于"/var/www/html"的网站(index.php)打开和关闭LED,这两个脚本位于"/var/www/html/wspace/Server/" Both scripts work perfectly fine when I execute them with ssh but I am not able to execute them through my php file. 当我使用ssh执行它们时,两个脚本都可以正常工作,但是我无法通过我的php文件执行它们。

In my index.php I wrote the following code for on-/off.py: 在我的index.php中,我为on- / off.py编写了以下代码:

exec("/usr/bin/python3.5 /wspace/Server/on.py");

(I located the python installation with which python3.5 . The answer was /usr/bin/python3.5 .) (我找到了which python3.5所在的python安装。答案是/usr/bin/python3.5 。)

I have no idea what is wrong. 我不知道怎么了。 Is it he command itself or do I have to change sth. 是他自己指挥还是我必须改变……。 in apache? 在阿帕奇? What has to be changed that it works? 它必须改变什么才能起作用?

As further information the owner and permissions of the files: 作为进一步的信息,文件的所有者和权限:

index.php pi:pi 777
on.py pi:pi 777
off.py pi:pi 777

The apache user is likely "www-data" which doesn't, hopefully, have sudo access. apache用户很可能是“ www-data”,希望没有sudo访问权限。 Also multiple words in exec may act weird, I don't know. 我也不知道exec中的多个单词可能表现得很奇怪。 exec() may not even be executing bash so things like "&&" won't work. exec()甚至可能没有执行bash,因此“ &&”之类的东西将无法工作。

Put

#!/usr/bin/python3

as the first line of the python script and 作为python脚本的第一行,

$chmod +x on.py

Then 然后

exec("/var/www/html/wspace/Server/on.py")

Using relative pathing here isn't a good idea either because you don't know what the working directory is, though I'm sure can be specified. 在这里使用相对路径也不是一个好主意,因为虽然我确定可以指定工作目录,但您不知道工作目录是什么。

If this still doesn't work then environment variables are the likely culprit, and you'll see something along the lines of "unknown environment: python3". 如果仍然不能解决问题,则可能是环境变量造成的,您将看到类似“未知环境:python3”的内容。 I'm quoting from memory here so I may be off. 我在这里从内存中引用,所以我可能会离开。 If this is the case you'll have to call a bash script which exports PATH. 如果是这种情况,则必须调用一个导出PATH的bash脚本。 You can log on to the box and echo $PATH as a regular user, don't matter for this case and then the bash script would be: 您可以登录到该框并以普通用户的身份回显$ PATH,这种情况无关紧要,那么bash脚本将是:

#!/bin/bash
export PATH=$PATH:<the stuff from 'echo $PATH'>
/var/www/html/wspace/Server/on.py

Don't forget to turn the executable bit on for this script either, chmod +x 也不要忘记为此脚本打开可执行位chmod + x

Finally, your PHP would call the above bash script: 最后,您的PHP将调用上述bash脚本:

exec("<full path to bash script>")

It's important to realize that in all likely hood the argument passed to exec will not be run under the bash interpreter so the above may need to be done in a standalone script. 重要的是要意识到,在所有可能的情况下,传递给exec的参数都不会在bash解释器下运行,因此上述操作可能需要在独立脚本中完成。

I had the same issue a while back. 我前一段时间也遇到过同样的问题。

For this scenario, you need to consider where the scripts are in relativity to the executing file. 对于这种情况,您需要考虑脚本与执行文件的相对位置。 At the moment, you're preceding with a slash, so it's going to look from an absolute path from the root directory. 目前,您以斜杠开头,因此它将从根目录的绝对路径中查找。 If you remove this then it'll look relatively and should work. 如果删除它,它将看起来相对并且应该可以工作。

Also, I found I had to include the whole path when executing python. 另外,我发现执行python时必须包括整个路径。 Eg 例如

exec('/usr/local/bin/python3 wspace/Server/on.py')

If this still doesn't work, try changing the directory and then executing the file: 如果仍然无法解决问题,请尝试更改目录,然后执行文件:

exec('cd wspace/Server && /usr/local/bin/python3 on.py')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM