简体   繁体   English

如何从php网页执行程序?

[英]How to execute a program from a php web page?

Summary 摘要

I have apache running on a Linux machine where a certain program is installed. 我在安装了某些程序的Linux机器上运行apache。 I want to call the program from a php page and get the output. 我想从php页面调用该程序并获取输出。

Special constraint 特殊约束

The program needs certain environment variables to run, eg LD_LIBRARY_PATH with path to libraries plus extra variables. 该程序需要某些环境变量才能运行,例如LD_LIBRARY_PATH以及库路径以及其他变量。 The environment is defined for a user X on the machine that is not the apache user. 在不是apache用户的计算机上为用户X定义了环境。

Proposed options 建议方案

So far I have thought of the following options (not meaning that they are all smart or safe): 到目前为止,我已经想到了以下选项(并不意味着它们都很聪明或安全):

  1. Allow user apache to run the program as user X (in a way that allows having the full environment). 允许用户apache以用户X的身份运行程序(以允许拥有完整环境的方式)。 Recipe ? 食谱 ?
  2. Modify the environment of user apache . 修改用户apache的环境。 I don't see how because apache is a nologin user. 我不知道怎么回事,因为apache是nologin用户。
  3. SSH to another machine as a user that has the proper environment. 通过具有适当环境的用户SSH到另一台计算机。 It means to have a no password (public/private keys) connection between the web server and this machine. 这意味着Web服务器与本机之间没有密码(公用/专用密钥)连接。 Is it dangerous ? 危险吗 ?

Questions 问题

  • Did I overlook a simpler or better option ? 我是否忽略了一个更简单或更佳的选择?
  • What option would you use and why ? 您将使用什么选项?为什么?

if you know the (values of) environment variables you need to set, you could simply prepend them to your exec 如果您知道需要设置的环境变量(的值),则只需将它们放在执行程序之前

exec('LD_LIBRARY_PATH="/you/paths"; OTHER_VAR="baz"; /your/bin');

that will execute /your/bin with the given ENV variables set, but as the apache user, not user X . 将使用给定的ENV变量集执行/your/bin ,但作为apache用户,而不是用户X。 If the program needs to run as a specific user, SSH doesn't seem like such a bad idea. 如果程序需要以特定用户身份运行,SSH似乎不是一个坏主意。

Do you have full control over this platform? 您对此平台有完全控制权吗?

You can look into suexec , or mpm-iTK 您可以查看suexecmpm-iTK

You could configure the above within a virtual host, and set the run as user or group to that which has access to your binary. 您可以在虚拟主机中配置上述内容,然后将运行身份作为用户或组设置为有权访问二进制文件的用户或组。

In your current setup, you could create a new group, assign the apache user/group to said group, apply group to the binary you want to execute with read/write or read/execute permissions, and let the good times role. 在当前设置中,您可以创建一个新组,将apache用户/组分配给所述组,将组应用于要执行的具有读/写或读/执行权限的二进制文件,并让其成为美好时光。

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

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