简体   繁体   English

在Linux下通过PHP网页运行C编译程序

[英]Running a C compiled program through a PHP webpage under linux

My page.php file looks like this at the moment: 我的page.php文件目前看起来像这样:

    <?php

    $cmd = './foo < input.txt > output.txt';

    $result = shell_exec($cmd);

    echo system('date'); //just to see change when refreshing the page

    ?>

Whenever I type the same command in the shell, it works perfectly (with the input.txt file ready with an example input). 每当我在shell中键入相同的命令时,它就可以完美地工作(使用input.txt文件并带有示例输入)。

My idea was to create an interface using php to communicate with the input.txt file and then communicate with the output.txt file to return the results on the page. 我的想法是使用php创建一个接口来与input.txt文件进行通信,然后与output.txt文件进行通信以在页面上返回结果。 But, first of all, I need to make sure I can run the program I have. 但是,首先,我需要确保我可以运行我拥有的程序。

I've compiled with 我已经用

    gcc -Wall -o foo foo.c

The foo executable is on the same directory as the page.php. foo可执行文件与page.php位于同一目录中。

I've tried with 777 permissions on all the files (page.php, input.txt and foo) 我尝试对所有文件(page.php,input.txt和foo)使用777权限

The site is up and running and the date changes when I refresh the page but there's no output.txt on the directory. 该网站已启动且正在运行,当我刷新页面时日期会更改,但目录上没有output.txt。

I've tried 我试过了

    $cmd = 'ls -la';

    $result = shell_exec($cmd);

    echo $result;

and it works as intended, showing the contents of the proper directory. 并按预期工作,显示正确目录的内容。

One thing which can cause this type of problem is if Apache does not have write access to the directory where output.txt is supposed to go. 可能导致这种类型问题的一件事是,如果Apache没有对output.txt本应到达的目录的写访问权。 Do an ls -l ../ to see whether or not you actually have the requisite permissions. 执行ls -l ../来查看您是否实际上具有必要的权限。 I would also echo out the result, as that might give you more of an idea of what is going on. 我还将回显结果,因为这可能使您对正在发生的事情有更多的了解。

As stated in a comment, the problem was permission based. 如评论中所述,问题是基于权限的。 The minimum permissions required for this to work are as follows 此功能所需的最低权限如下

"711 on the executable files, 644 on input file, 666 on output file and 755 on containing folder." “可执行文件上有711,输入文件上有644,输出文件上有666,包含文件夹中有755。”

Thanks for all the contributions. 感谢您的所有贡献。

Another possible problem is that if you have SELinux enabled (as is the default in Fedora and Red Hat), it will block Apache from running commands which have not been marked as being in the context of Apache. 另一个可能的问题是,如果您启用了SELinux(这是Fedora和Red Hat中的默认设置),它将阻止Apache运行尚未标记为在Apache上下文中的命令。 This is a security feature meant to make it so that if someone hacks your web server or web application they can't use it to run arbitrary commands on your server. 这是一项安全功能,旨在使它能够在有人入侵您的Web服务器或Web应用程序的情况下无法使用它在您的服务器上运行任意命令。

To set the correct context for the application, you'd want to run the command chcon -t httpd_sys_script_exec_t execname (where execname is replaced with the name of your executable). 要为应用程序设置正确的上下文,您需要运行命令chcon -t httpd_sys_script_exec_t execname (将execname替换为可执行文件的名称)。 You'll also need to change the context of the input and output files to allow them to be written by apache scripts via chcon. 您还需要更改输入和输出文件的上下文,以允许它们通过chcon由apache脚本编写。 You'll want to use type httpd_sys_script_ro_t for read-only input files and type httpd_sys_script_rw_t for read/write output files or type httpd_sys_script_ra_t for read/append output files. 你要使用类型httpd_sys_script_ro_t只读输入文件,并键入httpd_sys_script_rw_t读/写输出文件或键入httpd_sys_script_ra_t进行读/追加输出文件。 The chcon command generally only changes things until the next server reboot, though. chcon命令通常只会更改内容,直到下次服务器重新启动为止。 File contexts are remapped based on directories at that time, so in the long run, you'll want to adjust your policy to set those contexts as the default contexts for those files. 当时,文件上下文是根据目录重新映射的,因此从长远来看,您将需要调整策略以将这些上下文设置为这些文件的默认上下文。 I don't remember how to do that off the top of my head, however, so you'll need to look that up. 但是,我不记得该怎么做,所以您需要查一下。

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

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