DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 21st March 2016
pmaddams
-Guest-
 
Posts: n/a
Default FCGI.pm on OpenBSD httpd

Hello everyone.

My goal is to use Perl FastCGI with httpd as simply as possible. As I understand it this will require the following steps:

1. get the FCGI perl module with CPAN
2. write a perl script that creates a unix socket in /var/www/run
3. configure httpd to talk to the socket
4. run the script and start the server

One step which I have left out is copying the perl executable and libraries into the chroot. I don't think this should be necessary, since you can chroot within the perl script to "/var/www", chdir to "/", and create the socket in "run". httpd will not be aware of this and simply talk to the socket.

So of course I tried these steps myself before coming here, but did not meet with great success, rather "500 Internal Server Error." This is my story.

First of all I appended httpd_flags="" to /etc/rc.conf.local. Then I did "mv /etc/examples/httpd.conf /etc/httpd.conf". The results of my tweaking were

Code:
server "default" {
        listen on $ext_addr port 80
        root "/"
}
Then I whipped up a quick index.html consisting of <h2>Hello world</h2>. After running "rcctl start httpd" I can type in “localhost” in my browser’s address bar and see “Hello world”. So far so good.

So then I ran cpan as root to get the needed module. perl -V shows that the first entry in @INC is "/home/pmaddams/perl5/lib/perl5/amd64-openbsd". So "install FCGI" put FCGI.pm there.

Now I write a test script "test.fcgi" in /var/www/cgi-bin/ that goes like so:

Code:
#!/usr/bin/perl -T

use strict;
use warnings;

use FCGI;

chroot "/var/www";
chdir "/";

print "done.\n";
Uh-oh. It says "Can't locate FCGI.pm in @INC" and does not show /home/pmaddams anywhere. So I do a quick "mv /home/pmaddams/perl5/lib/perl5/amd64-openbsd/* /usr/local/libdata/perl5/site_perl/amd64-openbsd/" and now it works, printing a nice "done."

Then we get to the gnarly part where lots of things change at once. I altered my perl script according to the instructions at cpan.org. Now it looks like

Code:
#!/usr/bin/perl -T

use strict;
use warnings;

use FCGI;

chroot "/var/www";
chdir "/";

my $in = \*STDIN;
my $out = \*STDOUT;
my $err = \*STDERR;
my $env = \%ENV;

my $socket = FCGI::OpenSocket "run/test.sock", 10;
my $request = FCGI::Request $in, $out, $err, $env, $socket;

while ($request->Accept() >= 0) {
        print "<h2>Hello world</h2>\n";
}
Indeed it has created run/test.sock. So I edit my httpd.conf to look like:

Code:
server "default" {
        listen on $ext_addr port 80
        location “/cgi-bin/*” {
	        fastcgi socket “/run/test.sock”
	        root “/”
        }
}
Then run "test.fcgi & rcctl restart httpd". But I get the 500 error and do not know how to troubleshoot. Please help.

Last edited by pmaddams; 21st March 2016 at 08:20 PM. Reason: link
Reply With Quote
  #2   (View Single Post)  
Old 21st March 2016
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,977
Default

You may want to use slowcgi(8) as your FastCGI <-> CGI interface, as discussed in this misc@ thread.

http://marc.info/?t=141665013300001&r=1&w=2
Reply With Quote
  #3   (View Single Post)  
Old 22nd March 2016
pmaddams
-Guest-
 
Posts: n/a
Default

Dudebro! It's working. I read the man page for slowcgi and it gave me an essential hint: the socket that httpd communicates with has to be owned by www:www.

Making this change got rid of the 500 error. My quick hack was to just chown the socket after starting the Perl process. However, I was still looking at a blank screen, so I then changed the script to include the line

Code:
print "Content-Type: text/html\n\n";
There are two things I like about this. One is that it's faster since the Perl process keeps running instead of recompiling every time, and the second is that I didn't have to copy a bunch of crap into /var/www for chroot purposes.
Reply With Quote
  #4   (View Single Post)  
Old 22nd March 2017
fvgit's Avatar
fvgit fvgit is offline
Spikes in tights
 
Join Date: May 2016
Location: perl -MMIME::Base64 -le 'print decode_base64("U2hlcndvb2QgRm9yZXN0")'
Posts: 314
Question

Sorry to dig up this old topic, but since I'm trying to accomplish the same thing as the OP, I thought why open a new one?

I've been successfull with the following setup:

Code:
server "default" {
        listen on $ext_addr port 80
        root "/"
        location "/cgi-bin/*" {
                fastcgi
        }
}

Code:
#!/usr/bin/perl -T

use strict;
use warnings;

chroot "/var/www";
chdir "/";

print "Content-Type: text/html\n\n";
print "done.\n";
This works, but only if I copy the perl binary, its dependencies and the strict & warnings modules into the chroot environment.

The more advanced my test scripts get, the more modules I have to move into the chroot, leading me each time further into the rabbit hole.

Instead I'd like to set it up the way the OP described it in his first post, without the necessity to copy all the perl stuff into /var/www/...
But I can't seem to get past even the very first example. If I try the httpd.conf at the very beginning it treats my scripts as downloadable files, if I use my own with the fastcgi directive it complains about not finding the perl stuff.

Any ideas how to do it correctly?
Reply With Quote
  #5   (View Single Post)  
Old 26th April 2017
fvgit's Avatar
fvgit fvgit is offline
Spikes in tights
 
Join Date: May 2016
Location: perl -MMIME::Base64 -le 'print decode_base64("U2hlcndvb2QgRm9yZXN0")'
Posts: 314
Default

Replying to myself here, but I gave it another go and figured it out. The perl script needs to be started from the command line first, then it works as advertised by the OP. I've tested it by converting a few of my cgi scripts to use the FCGI method.

This is pretty neat. Now I just need to turn FCGI into an OpenBSD package for cleaner installation.
Reply With Quote
Reply

Tags
cpan, fastcgi, httpd, openbsd, perl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Install OpenBSD httpd on FreeBSD? notooth FreeBSD Ports and Packages 5 3rd November 2019 06:03 PM
Patch for OpenBSD 5.6 httpd(8) J65nko OpenBSD General 12 7th January 2015 12:49 PM
httpd in OpenBSD 5.6 jorisvh OpenBSD Packages and Ports 8 4th November 2014 12:14 AM
is nginx going to be default OpenBSD httpd? ershiba OpenBSD General 4 6th January 2013 03:55 AM
Problems getting lighttpd working with php and fcgi pormogo FreeBSD Ports and Packages 3 6th June 2008 10:02 PM


All times are GMT. The time now is 10:57 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick