PHP Developers Network
http://forums.devnetwork.net/

Building a CGI
http://forums.devnetwork.net/viewtopic.php?f=30&t=137282
Page 1 of 2

Author:  Eric! [ Fri Jan 18, 2013 9:19 am ]
Post subject:  Building a CGI

I was wondering if anyone could give me an idea of how hard it is to build a CGI from source that requires two external libraries to build. All the code and libraries are open source. I'm not really familiar with building binary packages in Linux, but I'd like to run this CLI-type tool on a shared server. The tool is a bit complex. I've built it on my local machine, but I'll need to target a x86_64 architecture.

I think I can run CentOS 64 bit in a virtual box. After that is is just a matter of compartmentalizing all the libraries and source to run in the cgi-bin folder? Or is there a lot more to it?

Thanks
Eric

Author:  requinix [ Fri Jan 18, 2013 1:23 pm ]
Post subject:  Re: Buliding a CGI

Saying "build a CGI" doesn't make sense. CGI just means that a program that can be run in response to a HTTP request in a web server.

CGI programs need to output HTTP response headers. Assuming your CLI program does not do that, you have to take care of it. Could simply use its output in a regular PHP script:
Syntax: [ Download ] [ Hide ]
<?php

header("Content-Type: text/plain");
passthru("/path/to/program arg1 arg2 arg3");

?>

Author:  Eric! [ Fri Jan 18, 2013 4:10 pm ]
Post subject:  Re: Buliding a CGI

I understand the php/server part of it. I'm just not familiar with Linux enough to know what it will take to run the whole program on the server. It involves several libraries. If these are called dynamically or not I don't know.

My question is probably too generic to make a lot of sense but assume the worst case. I have to build the binary on a host's OS (via virtual box), that the supporting libraries need to be available locally and whatever else that I don't know about needs to be packaged up with it. I'm just trying to gauge the level of difficulty before I dive into it because I don't have a lot of time right now and I don't want to have to stop mid-way through the process.

Thanks.

Author:  requinix [ Fri Jan 18, 2013 4:55 pm ]
Post subject:  Re: Buliding a CGI

Generally not that difficult. Most of the time the package will contain a configure script and makefile and, after making sure you've installed any library dependencies it may need (if you don't have one it'll tell you), run
Code:
./configure && make && make install

That's it. Most of the time.

To tell you much more kinda depends on knowing more about this program and the libraries it needs.

Author:  Eric! [ Fri Jan 18, 2013 7:04 pm ]
Post subject:  Re: Buliding a CGI

I'll probably just have to try to work through it. I can't use the default makefile because I want all the binaries/libraries under the cgi-bin structure. I've got to setup the CentOS environment and just see how far I can get with it. Thanks. I know it's a really vague question.

Author:  requinix [ Fri Jan 18, 2013 7:16 pm ]
Post subject:  Re: Buliding a CGI

Eric! wrote:
I can't use the default makefile because I want all the binaries/libraries under the cgi-bin structure.

The configure script may have an option for that. Otherwise ./configure && make and you can copy whatever files you need wherever they need to go.

Author:  Eric! [ Mon Jan 21, 2013 11:36 am ]
Post subject:  Re: Buliding a CGI

So here's more of a real question. This is the target environment:
Syntax: [ Download ] [ Hide ]
Linux domain.com 2.6.32-279.11.1.el6.x86_64 #1 SMP Tue Oct 16 15:57:10 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
 


Here's my build setup
Syntax: [ Download ] [ Hide ]
Linux livedvd.centos 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
 


Pretty close? When I compile a simple file like the following. I get a "500 Internal server error":
Syntax: [ Download ] [ Hide ]
#include<stdio.h>
#include <sys/utsname.h>

struct utsname unameData;

main()
{
  printf ("Content-type:text/html\r\n\r\n");
  printf ("<html>");
  printf ("<head>");
  printf ("<title>CGI Program</title>");
  printf ("</head>");
  printf ("<body><h1>");
  uname(&unameData); // Might check return value here (non-0 = failure)
  printf("System name: %s<br />\nNodename:%s<br />\nRelease:%s<br />\nVersion:%s<br />\nMachine:%s<br />\n",
       unameData.sysname, unameData.nodename, unameData.release,unameData.version,unameData.machine);
  printf ("</h1></body>");
  printf ("</html>");
}
 


Perl example that works ... so mod_cgi should be configured right?
Syntax: [ Download ] [ Hide ]
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>CGI Program</title>';
print '</head>';
print '<body>';
print '<h2>CGI program</h2>';
system("uname -a");
print '</body>';
print '</html>';

Author:  requinix [ Mon Jan 21, 2013 1:25 pm ]
Post subject:  Re: Buliding a CGI

What does the error log say about the 500? Something besides an unexpected end of headers?

Author:  Eric! [ Mon Jan 21, 2013 3:48 pm ]
Post subject:  Re: Building a CGI

They aren't available, but I was able to get tech support to find it and tell me. Pretty vague and I don't think the headers are wrong.

[Mon Jan 21 12:10:34 2013] [error] [client xxxx] Premature end of script headers: test2.cgi

Author:  requinix [ Mon Jan 21, 2013 3:53 pm ]
Post subject:  Re: Building a CGI

Yeah, that's the message I was thinking of. Incredibly helpful, isn't it?

I wouldn't have expected the Perl script to work but maybe something special happens there. For the C try including a Status header.
Code:
printf("Status: 200 OK\r\n");

Alternatively, as the very first header line,
Code:
printf("HTTP/1.1 200 OK\r\n");


Also your program should return a value.
Code:
#include <stdlib.h>

Code:
int main()
{

Code:
  return EXIT_SUCCESS;
}

Author:  Eric! [ Tue Jan 22, 2013 9:12 am ]
Post subject:  Re: Building a CGI

It's stupid. So much for starting with a copy-and-paste working example off the internet. The error was a lack of space after the colon. But I'm puzzled why perl worked.
Syntax: [ Download ] [ Hide ]
printf ("Content-type:text/html\r\n\r\n");


I've managed to compile all my tools, but I've run into the problem I've been dreading. Once I move the binaries to the cgi-bin directory they can't find the library they were sharing:

Syntax: [ Download ] [ Hide ]
./cdo: error while loading shared libraries: libgrib_api-1.9.18.so: cannot open shared object file: No such file or directory


Looks like I'll have to modify the build to use a relative path to shared libraries so I can move the bins and the .so files around?

Author:  Eric! [ Tue Jan 22, 2013 1:46 pm ]
Post subject:  Re: Building a CGI

I'm not sure the best approach. Hopefully someone could give me some advice.

I could recompile, but I don't know the full path that will be used on the server.

I could try "patchelf" to modify the rpath of the binaries.

Since my host environment takes me a couple of hours just to get setup and configured for compiling I'd like to see if someone could recommend a solution to the shared library paths before I set aside another chunk of time to work on it. I found this reference helpful in explaining the methods, but I'm not sure what would be best.

Author:  requinix [ Tue Jan 22, 2013 2:47 pm ]
Post subject:  Re: Building a CGI

You can copy the .so files into the system library path, or probably put them in cgi-bin too.

As for the space after the colon, it's recommended to have one but not actually required. Don't know why it wasn't working without.

Author:  Eric! [ Tue Jan 22, 2013 3:11 pm ]
Post subject:  Re: Building a CGI

It is weird that perl worked and the bin didn't, but I tried lots of things and that space was would break or fix it.

I don't have access to the server's library file system (shared host). I have them in cgi-bin but the bins aren't looking for them there. I could also just set the LD_LIBRARY_PATH with a script, because I need to wrap these tools with some HTML anyway. The side benefit would be preventing remote direct execution of the bins and allow me to make sure only the localhost can run the tools without having to modify the original source code.

Author:  requinix [ Tue Jan 22, 2013 3:57 pm ]
Post subject:  Re: Building a CGI

Depending on your CGI setup a SetEnv/PassEnv in your virtualhost config or .htaccess to change the LD_LIBRARY_PATH just for cgi-bin might work.

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/