SCGI

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

SCGI

Post by infratec »

Hi,

I need now a web script which accesses a database and this very often by many clients.
Up to now I used a CGI program written in PB.
But always loading the program needs to long and is inefficient.

So I looked for the well known FastCGI...

But I could not get a friend of it.
After 2 days I found SCGI: That's it :!:

Install the module, enable it
a2enmod mod_scgi
and add something like

Code: Select all

<Location "/scgi">
  SGIHandler On
</Location>
SCGIMount /scgi 127.0.0.1:4000
in your apache site.conf file.
You don't need to create /scgi physically.

Than use this PB code as first test base for a SCGI server:

Code: Select all

InitNetwork()

Server = CreateNetworkServer(#PB_Any, 4000, #PB_Network_TCP,  "127.0.0.1")
If Server
  
  *Buffer = AllocateMemory(1024)
  If *Buffer
    
    Repeat
      If NetworkServerEvent() = #PB_NetworkEvent_Data
        Client = EventClient()
        Size = ReceiveNetworkData(Client, *Buffer, MemorySize(*Buffer))
        If Size
          
          Answer$ = "Status: 200 OK" +#CRLF$
          Answer$ + "Content-type: text/plain" + #CRLF$
          Answer$ + #CRLF$
          
          Ptr = 0
          Repeat
            Buffer$ = PeekS(*Buffer + Ptr, -1, #PB_UTF8)
            Ptr + StringByteLength(Buffer$) + 1
            Answer$ + Buffer$ + #CRLF$
          Until Ptr >= Size
          
          SendNetworkString(Client,  Answer$)
          CloseNetworkConnection(Client)
          
        EndIf
      Else
        Delay(10)
      EndIf
    ForEver
    
  EndIf
  
EndIf
To test enter something like this:
http://yourserver/scgi?para=123
in your browser.

This shows you the way...
More work to be done by you :mrgreen:

Bernd
Last edited by infratec on Tue Sep 08, 2015 4:04 pm, edited 4 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5864
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SCGI

Post by idle »

makes things much simpler :)
Windows 11, Manjaro, Raspberry Pi OS
Image
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SCGI

Post by infratec »

And you need no additional lib or something like that.
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: SCGI

Post by useful »

infratec wrote: ...........

I need now a web script which accesses a database and this very often by many clients.
Up to now I used a CGI program written in PB.
But always loading the program needs to long and is inefficient.

So I looked for the well known FastCGI...

............
If I own server, it is all wonderful. As possible examples of commercial hosting, where you can place PB FastCgi?
Dawn will come inevitably.
dige
Addict
Addict
Posts: 1396
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: SCGI

Post by dige »

What does this mean? All traffic that goes to SCGI, will be redirected to another network server?
"Daddy, I'll run faster, then it is not so far..."
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SCGI

Post by infratec »

Hi,

SCGI is like FastCGI a solution that the CGI program is not loaded at every call.
It is running as server and so no time for loading and initialization is needed.

The communication between the Web-Server and the 'CGI' is done via network.

This gives further possibilities:

You can run the SCGI program on any machine, not requierd on the Web-Server.
This maybe more secure.

The SCGI program can run as any user and it is not running as the Web-Server user,
which is also more secure.

In my case, where I need as much speed as possible, I run it on the same machine on localhost interface.

At the moment I try to implement multithreading for each client connection.

Hope this answers your questions.

Bernd
dige
Addict
Addict
Posts: 1396
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: SCGI

Post by dige »

Bernd, muchas gracias!! :-)
"Daddy, I'll run faster, then it is not so far..."
User avatar
Kiffi
Addict
Addict
Posts: 1492
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: SCGI

Post by Kiffi »

Hello Bernd,
infratec wrote:Install the module
OK (the module was already installed in my XAMPP-Package)
infratec wrote:enable it
OK
infratec wrote:and add something like

Code: Select all

<Location "/scgi">
  SGIHandler On
</Location>
SCGIMount /scgi 127.0.0.1:4000
Doesn't work for me. If i add this lines into the httpd.config, my Apache won't start.
[Apache] Apache shutdown unexpectedly.
[Apache] This may be due to a blocked port, missing dependencies,
[Apache] improper privileges, a crash, or a shutdown by another method.
[Apache] Press the Logs button to view error logs and check
[Apache] the Windows Event Viewer for more clues
[Apache] If you need more help, copy and post this
[Apache] entire log window on the forums
The Apache error.log contains no hint, nor the Windows Eventlog.

Any idea, what i'm doing wrong?

Thanks in advance & Greetings ... Peter
Hygge
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SCGI

Post by infratec »

Hi Kiffi,

I never used XAMPP on windows.
I always use a linux server (Debian) with Apache 2.2 (at the moment)


Oh, httpd.conf ???
I think that's the wrong place.
It should be in the site.conf file.
Somewhere inside the <VirtualHost > tags

Because it belongs to the website and not apache in general.

Else:
Do you run it as Administrator?
Try to switch off the firewall (normally a box should appear to allow access for apache to access the port)

Bernd
User avatar
Kiffi
Addict
Addict
Posts: 1492
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: SCGI

Post by Kiffi »

Hello Bernd,

thanks for your fast reply! In the meantime i have tried it with EasyPHP and get a more detailed error-message:
AH00526: Syntax error on line 1103 of C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/binaries/apache/conf/httpd.conf:
Invalid command 'SGIHandler', perhaps misspelled or defined by a module not included in the server configuration
But finally i found a solution that works for me:

Code: Select all

ProxyPass /scgi-bin/ scgi://localhost:4000/
Thanks again & Greetings ... Peter
Hygge
Post Reply