0

I was using VPS with Apache/Nginx (at different times) on Ubuntu 14.04 and was executing commands/programs in php via exec(). Now I know that, I don't need Php for executing these things , let's say :

exec('whoami'); or
exec('myexec');

as php is just acting as additional layer in my case. So can I just use apache/nginx to get data from http requests made to it ( get,post..) and 'pass' as parameters to certain executable program and 'return the output'(plain text) ? Lets say a calc program that receives 3 param ( 4,5,+ ) and return output ( 9 ).

I have already seen this ques , but it states the procedure with Lua script , while I'm trying to do some hobby projects with c++. Currently I have no idea how to proceed as I'm only familiar with php LAMP stack , if I'm wrong somewhere a little guidance would be helpful :)

Community
  • 1
  • 1
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93

1 Answers1

0

You could probably use CGI with your webserver, so it serves the output of your C++ code when a certain URL is requested. I'm not sure if this can be called "good" practice, but it's one way to achieve what you want. For more information, check this link http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm

DISCLAIMER: I'm not a C++ programmer so this may not actually work.

  • Web Server Configuration

    Make sure your webserver supports CGI and configure it accordingly. Example Apache 2 virtual host file:

     <Directory "/var/www/cgi-bin">
         AllowOverride None
         Options ExecCGI
         Order allow,deny
         Allow from all
     </Directory>
    
    <Directory "/var/www/cgi-bin">
        Options All
    </Directory>
    
  • Example C++ program

     #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;
    }
    

    Compile above code and name the executable as cplusplus.cgi. Put it in /var/www/cgi-bin directory or whatever you configured in your Apache configuration file. Don't forget to make it executable (chmod 770 cplusplus.cgi). Now, if you visit the URL www.example.org/cgi-bin/cplusplus you should see the output Hello World! This is my first CGI program

kalatabe
  • 2,909
  • 2
  • 15
  • 24
  • Well I was under the impression that CGI is something that people think should have died long ago (see Shellshock), but if you stick to security best practice and if it's not for a very large/complex project, I see no harm in doing this way. – kalatabe Jan 08 '15 at 12:37