我在Ubuntu 14.04上与Apache/Nginx一起使用VPS(在不同的时间(,并通过exec((在php中执行命令/程序。现在我知道了,我不需要Php来执行这些事情,比如说:
exec('whoami'); or
exec('myexec');
因为php在我的情况下只是作为附加层。那么,我可以使用apache/nginix从向它发出的http请求中获取数据吗(get,post..(,并将"pass"作为参数传递给特定的可执行程序,然后"返回输出"(纯文本(?比方说一个接收3个参数(4,5,+(并返回输出(9(的计算程序。
我已经看过这个ques,但它用Lua脚本说明了过程,而我正试图用c++做一些业余项目。目前我不知道如何进行,因为我只熟悉phpLAMP堆栈,如果我在某个地方错了,一点指导会很有帮助:(
您可能会在Web服务器中使用CGI,因此当请求某个URL时,它会为C++代码的输出提供服务。我不确定这是否可以被称为"好"的做法,但这是实现你想要的一种方法。有关详细信息,请查看此链接http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm
免责声明:我不是C++程序员,所以这可能实际上不起作用。
-
Web服务器配置
确保您的Web服务器支持CGI,并对其进行相应配置。Apache 2虚拟主机文件示例:
<Directory "/var/www/cgi-bin"> AllowOverride None Options ExecCGI Order allow,deny Allow from all </Directory> <Directory "/var/www/cgi-bin"> Options All </Directory>
-
示例C++程序
#include <iostream> using namespace std; int main () { cout << "Content-type:text/html'r'n'r'n"; cout << "<html>'n"; cout << "<head>'n"; cout << "<title>Hello World - First CGI Program</title>'n"; cout << "</head>'n"; cout << "<body>'n"; cout << "<h2>Hello World! This is my first CGI program</h2>'n"; cout << "</body>'n"; cout << "</html>'n"; return 0; }
编译以上代码并将可执行文件命名为cplusplus.cgi。将其放入/var/www/cgi-bin目录或Apache配置文件中配置的任何文件中。别忘了让它可执行(chmod 770 cplusplus.cgi(。现在,如果你访问网址www.example.org/cgi-bin/cplusplus,你应该会看到输出
Hello World! This is my first CGI program