dongni8969 2012-03-13 16:18
浏览 58

使用php shell_exec()执行命令行以启动服务器上的摄像头

I am using opencv for initiating the camera on my arch linux. Its getting initiated and works well when I actually do it from the command line on the server itself. I want to initialize it using php. I tried doing it using shell_exec from PHP.

My PHP file looks like:

<?php
$output=shell_exec('LD_LIBRARY_PATH=usr/local/lib ./a.out 0 2>&1 1>/dev/null');
echo $output;
?>

It gives this output: ERROR: capture is NULL

I am running this through my windows web browser as a client and the opencv and the related files are on the server that is my arch linux.

I want to start the camera and capture images when I run this php file from the windows web browser, but when executed it throws the error as mentioned.

  • 写回答

1条回答 默认 最新

  • dounabi6295 2012-06-18 22:33
    关注

    While this may work when you are SSHed into your server. The webserver user is most likely different than the user you login as. Popular user ids/groups that webservers run as on Linux machines are http, www-data, nobody, and others.

    From this point you have two options.

    1. You can make sure the script you are trying to run from PHP (and all of it's children, if any) is able to be run by the webserver user.

    2. You can modify your /etc/sudoers file which gives the webserver user access to elevate permissions for that script only. (NOTE: This potentially opens up security holes so be careful).

    To find out what user your webserver runs as execute this: ps aux Take a look at the output and the first column in the output lists the user that that process is running at. Here's an excerpt of my webserver (nginx) on one of my boxes:

    www-data 26852 0.0 0.0 29768 3840 ? S Jun04 0:50 nginx: worker process

    You can see that nginx runs with the user www-data here. You can also execute the command with grep to help you find the process quicker. Grep will only show you those lines which match what you send to it. Here's an example: ps aux | grep nginx

    Ok now that you know what user the webserver is running as, let's try giving that user access to the script. Let's say your script is foo and is located in /usr/local/bin. You would do the following commands:

    chown www-data /usr/local/bin/foo

    After changing ownership on the file try to rerun your command again from your PHP page and see if it works.

    For completeness I also said you could give your webserver user sudo privileges to that file. To do that you would need to append the following line to the bottom of your /etc/sudoers file:

    www-data ALL= NOPASSWD: /usr/local/bin/foo

    Then your shell_exec command could switch to this:

    shell_exec('sudo /usr/local/bin/foo');

    Please keep in mind that doing this would allow your webserver user to run the script as root which is incredibly dangerous in a variety of situations. However, the camera may require elevated permissions to work. I'm not sure what the permissions requirements are on the camera setup you are trying to invoke.

    Good luck. Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式