简体   繁体   English

如何让 PHP 以 root 身份执行 Python3 脚本?

[英]How to make PHP execute Python3 script as root?

I want PHP to execute a Python3 script.我希望 PHP 执行 Python3 脚本。 I have done我已经做好了

$command = escapeshellcmd('pathtofile.py');
$output = shell_exec($command);
echo $output;

But whenever the script is run, I get an error :Permission Denied .但是每当运行脚本时,我都会收到错误:Permission Denied

I'm running a web server (Apache2 on Ubuntu 18.04) and the VPS has only one user- root.我正在运行 web 服务器(Ubuntu 18.04 上的 Apache2)并且 VPS 只有一个用户 root。

I have also done chmod +x path.py and added the shebang line in the scipt.我还完成了chmod +x path.py并在 scipt 中添加了 shebang 行。

Basically, the script is run when a user successfully registers.基本上,脚本在用户成功注册时运行。

You must to add apache user in sudoers file like: www-data ALL=(ALL:ALL) NOPASSWD:ALL then you can execute script like this: 'sudo pathtofile.py'您必须在 sudoers 文件中添加 apache 用户,例如: www-data ALL=(ALL:ALL) NOPASSWD:ALL 然后您可以执行如下脚本:'sudo pathtofile.py'

Or you can use compiled wrapper.或者您可以使用已编译的包装器。 Write C program, that executes script, than compile it.编写执行脚本的 C 程序,而不是编译它。 Than set sticky bit: chown root:root wrapper chmod a+rs wrapper比设置粘性位: chown root:root wrapper chmod a+rs wrapper

Than include www-data in the root group.比在根组中包含 www-data 。 Now can execute wrapper from your php script, and wrapper will execute python script as root.现在可以从您的 php 脚本执行包装器,并且包装器将以 root 身份执行 python 脚本。

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

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