PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Aug 18, 2017 3:42 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Building a CGI
PostPosted: Fri Jan 18, 2013 9:19 am 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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


Last edited by Eric! on Mon Jan 21, 2013 3:14 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Fri Jan 18, 2013 1:23 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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");

?>


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Fri Jan 18, 2013 4:10 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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.


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Fri Jan 18, 2013 4:55 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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.


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Fri Jan 18, 2013 7:04 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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.


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Fri Jan 18, 2013 7:16 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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.


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Mon Jan 21, 2013 11:36 am 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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>';


Top
 Profile  
 
 Post subject: Re: Buliding a CGI
PostPosted: Mon Jan 21, 2013 1:25 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
What does the error log say about the 500? Something besides an unexpected end of headers?


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Mon Jan 21, 2013 3:48 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Mon Jan 21, 2013 3:53 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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;
}


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Tue Jan 22, 2013 9:12 am 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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?


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Tue Jan 22, 2013 1:46 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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.


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Tue Jan 22, 2013 2:47 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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.


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Tue Jan 22, 2013 3:11 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
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.


Top
 Profile  
 
 Post subject: Re: Building a CGI
PostPosted: Tue Jan 22, 2013 3:57 pm 
Online
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6487
Location: WA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group