Building a CGI
Moderator: General Moderators
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
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
Last edited by Eric! on Mon Jan 21, 2013 2:14 pm, edited 1 time in total.
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:
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:
Code: Select all
<?php
header("Content-Type: text/plain");
passthru("/path/to/program arg1 arg2 arg3");
?>
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.
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.
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
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.
Code: Select all
./configure && make && make install
To tell you much more kinda depends on knowing more about this program and the libraries it needs.
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.
Re: Buliding a CGI
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.Eric! wrote:I can't use the default makefile because I want all the binaries/libraries under the cgi-bin structure.
Re: Buliding a CGI
So here's more of a real question. This is the target environment:
[text]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
[/text]
Here's my build setup
[text]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
[/text]
Pretty close? When I compile a simple file like the following. I get a "500 Internal server error":
Perl example that works ... so mod_cgi should be configured right?
[text]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
[/text]
Here's my build setup
[text]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
[/text]
Pretty close? When I compile a simple file like the following. I get a "500 Internal server error":
Code: Select all
#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>");
}
Code: Select all
#!/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>';
Re: Buliding a CGI
What does the error log say about the 500? Something besides an unexpected end of headers?
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
[Mon Jan 21 12:10:34 2013] [error] [client xxxx] Premature end of script headers: test2.cgi
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.
Alternatively, as the very first header line,
Also your program should return a value.
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: Select all
printf("Status: 200 OK\r\n");
Code: Select all
printf("HTTP/1.1 200 OK\r\n");
Code: Select all
#include <stdlib.h>
Code: Select all
int main()
{
Code: Select all
return EXIT_SUCCESS;
}
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.
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:
[text]./cdo: error while loading shared libraries: libgrib_api-1.9.18.so: cannot open shared object file: No such file or directory[/text]
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?
Code: Select all
printf ("Content-type:text/html\r\n\r\n");
[text]./cdo: error while loading shared libraries: libgrib_api-1.9.18.so: cannot open shared object file: No such file or directory[/text]
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?
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.
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.
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.
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.
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.
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.
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.