FCGI (FastCGI) step by step, using fpweb/fcl-web for Windows/Linux lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- bobo · 2011-05-31 17:45 I've created this readme in the past few days as I was running some tests, hope it will help people set up and use FCGI with Lazarus/FPC, or at least help to understand it better Note, you will need the latest SVN version of /packages/fcl-web/ of FPC for everything to work as explained here (version 2.5.1 or later). There are also example programs under /packages/fcl-web/examples/  . Many of them has all three of CGI/FCGI/Apache module versions. ====================================================================== ========= FCGI-README.txt Last modified: Attila Borka 05/31/2011 ---------------------------------------------------------------------- --------- This readme will try to explain and help developers how to be able to use the FastCGI protocol in Lazarus (package fpweb/lazweb) and FPC (/packages/fcl-web) applications with the Apache web server. There are two FCGI implementations we are able to use at the moment (FPC rev17597 /2.5.1/) with FPC/Lazarus: 1. mod_fcgid maintained by Apache (ASF) http://httpd.apache.org/mod_fcgid/ Easy to set up and use, Apache develops and maintains it. Just needs a download/install and it is ready to go, even on Linux. There are a lot of options and fine tuning that can be used, please refer to their documentation at http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html . 2. mod_fastcgi from http://www.fastcgi.com/ the original FCGI "inventors" The latest downloads are available from http://www.fastcgi.com/dist/ . Do not be alarmed if the latest one is years old. FCGI is a very stable protocol since many years (started in 1996). On Linux, you will need to compile the mod_fastcgi module from the source codes, as binaries are only available for Windows as a DLL. Note, that mod_fastcgi supports the use of External FCGI Servers, which makes it possible to run the FCGI application from within Lazarus for example, and be able to debug it with the GUI on the fly while it is generating the response pages. This can be a good choice for a development environment. The documentation is at http://www.fastcgi.com/drupal/node/6 . There are benefits to use both, everyone needs to decide which one fits their needs the best for a development environment or a live web server after some research on these modules. Do not forget, that unless you use the External FCGI Server way, you need to restart your web server every time you recompile your FCGI program. In case you use the External FCGI Server, you need to restart the application itself after every recompile for the changes to take effect in the web server responses. The Apache server does not need to be restarted every time in this latter case. ====================================================================== ========= 1. mod_fcgid from Apache 1.1 on Windows 1.2 on Linux 2. mod_fastcgi from fastcgi.com 2.1 on Windows 2.1 on Linux ====================================================================== ========= 1. mod_fcgid from Apache: ========================= 1.1 mod_fcgid from Apache on Windows: ------------------------------------- Setup Steps: 1.1.1 Download from http://httpd.apache.org/download.cgi#mod_fcgid the latest Windows zip file and unpack as it instructs you to, over the installed Apache directory. This puts the mod_fcgid.* files under the current /modules directory of Apache. 1.1.2 Now, mod_fcgid is ready to be used as soon as we load it with Apache. 1.1.3 If you have not done so yet, compile your FCGI application. 1.1.4 Then, edit the Apache /conf/httpd.conf file and add to the end: ############ LoadModule fcgid_module "modules/mod_fcgid.so"   ">     SetHandler fcgid-script #    Options +ExecCGI  <- not needed if ScriptAlias is used below     Order allow,deny     Allow from all   #optionally, to shorten the URL and to not display the executable file name #(if ScriptAlias is used, no +ExecCGI is needed above)   ScriptAlias /myfcgid "/" ############ Example: LoadModule fcgid_module "modules/mod_fcgid.so"       SetHandler fcgid-script     Order allow,deny     Allow from all     ScriptAlias /myfcgid "C:/My Programs/LazarusFCGITest/helloworld.exe" Note, there are many ways to configure the FCGI applications, this is just but one example. You can check the Apache and mod_fcgid documentation for alternatives. 1.1.5 Start/Restart your Apache server so it will load your FCGI application. If everything went according to plan, your FCGI application should be listed in the Windows task manager as running. 1.1.6 Open your web browser and try to call your new FCGI application. Example: http://127.0.0.1:8080/myfcgid/func1call or http://127.0.0.1/myfcgid/func1call depending on your Apache installation and configuration. "myfcgid" is the ScriptAlias name specified for the FCGI application, func1call is the action name we want to call within our default web module. If you have multiple web modules, you can enter the desired web module name before the action name, for example: http://127.0.0.1:8080/myfcgid/webmodule1/func1call or http://127.0.0.1/myfcgid/webmodule1/func1call If there is any problem, you can try and check the Apache error.log for clues. 1.2 mod_fcgid from Apache on Linux: ----------------------------------- Setup Steps: 1.2.1 Install the mod_fcgi module for Apache with your distro's package manager. Example on Ubuntu: sudo apt-get install libapache2-mod-fcgid This downloads, installs and configures mod_fcgid without the need to do anything else. 1.2.2 Now, mod_fcgid is ready to be used as soon as we load it with Apache. mod_fcgid.so should be sitting in the /usr/lib/apache2/modules/ directory (on Ubuntu) 1.2.3 If you have not done so yet, compile your FCGI application. 1.2.4 Edit the Apache configuration file (/etc/apache2/apache2.conf on Ubuntu) and add to the end: ############ LoadModule fcgid_module "/mod_fcgid.so"   ">     SetHandler fcgid-script #    Options +ExecCGI  <- not needed if ScriptAlias is used below     Order allow,deny     Allow from all   #optionally, to shorten the URL and to not display the executable file name #(if ScriptAlias is used, no +ExecCGI is needed above)   ScriptAlias /myfcgid "/" ############ Example: LoadModule fcgid_module "/usr/lib/apache2/modules/mod_fcgid.so"       SetHandler fcgid-script     Order allow,deny     Allow from all     ScriptAlias /myfcgid "/home/johndoe/LazarusFCGITest/helloworld" (the project was compiled into directory /home/johndoe/LazarusFCGITest/ , and the FCGI application is called helloworld with no file extension) Note, there are many ways to configure the FCGI applications, this is just but one example. You can check the Apache and mod_fcgid documentation for alternatives. 1.2.5 Start/Restart your Apache server so it will load your FCGI application. If everything went according to plan, and you do a "sudo netstat -l" (on Ubuntu) from a terminal window, there should be a new line in the result list looking something like this: ... LISTENING   XXXX   /var/lib/apache2/fcgid/sock/... indicating, that the Apache mod_fcgid module has loaded your FCGI application. 1.2.6 Open your web browser and try to call your new FCGI application. Example: http://127.0.0.1/myfcgid/func1call "myfcgid" is the ScriptAlias name specified for the FCGI application, and func1call is the action name we want to call within our default web module. If you have multiple web modules, you can enter the desired web module name before the action name, for example: http://127.0.0.1/myfcgid/webmodule1/func1call . If there is any problem, you can try and check the Apache error.log for clues. 2. mod_fastcgi from fastcgi.com: ================================ Unlike the Apache developed mod_fcgid, mod_fastcgi has two main operating modes for FCGI applications. One is very similar to mod_fcgid (where Apache itself loads the FCGI application at startup), and one called External FCGI Server mode. With this latter, Apache will not load the FCGI application at startup, but it has to be running on its own when a web request arrives (either as a system service/daemon, or a simple running application) and listening. This makes it possible to run the FCGI application from within a debugger interactively, to see/track the request handling like any normal GUI application debugging. 2.1 mod_fastcgi from fastcgi.com on Windows: -------------------------------------------- Setup Steps: 2.1.1 Download the latest SNAP or stable DLL from http://www.fastcgi.com/dist/ and put it into the Apache /modules/ directory. 2.1.2 If you have not done so yet, compile your FCGI application. In case you want to set up an External FCGI Server, then you must specify a port number in your main project file (.lpr) before the Application.Run instruction (this is the only change needed). Example: <...snip...>   Application.Initialize;   Application.Port:=9999;//Port the FCGI application is listening on   Application.Run; <...snip...> 2.1.3 Edit the Apache /conf/httpd.conf file and add to the end:  2.1.3.a External FCGI Server ############ LoadModule fastcgi_module "modules/"   "> #    Options +ExecCGI  <- not needed if ScriptAlias is used below     Order allow,deny     Allow from all   #External FCGI app, has to be started and running when a request comes in   FastCgiExternalServer "/" -host 127.0.0.1: -idle-timeout 30 -flush #optionally, to shorten the URL and to not display the executable file name (if used, no +ExecCGI is needed above):   ScriptAlias /myfcgi "/" ############ Example: LoadModule fastcgi_module "modules/mod_fastcgi-2.4.6-AP22.dll"       Order allow,deny     Allow from all     FastCgiExternalServer "C:/My Programs/LazarusFCGITest/helloworld.exe" -host 127.0.0.1:9999 -idle- timeout 30 -flush   ScriptAlias /myfcgi "C:/My Programs/LazarusFCGITest/helloworld.exe"  2.1.3.b Regular FCGI Server Replace the FastCgiExternalServer line above with   FastCgiServer "/" -idle- timeout 30 Example: LoadModule fastcgi_module "modules/mod_fastcgi-2.4.6-AP22.dll"       Order allow,deny     Allow from all     FastCgiServer "C:/My Programs/LazarusFCGITest/helloworld.exe" -idle- timeout 30   ScriptAlias /myfcgi "C:/My Programs/LazarusFCGITest/helloworld.exe" 2.1.4 Start/Restart your Apache server. If you will use the FastCgiExternalServer, then start your application manually, so it can start accepting incoming requests from the web server. If you use the FastCgiServer, then your FCGI application should be launched by the Apache server when it starts and should be listed in the Windows task manager as running. 2.1.5 Open your web browser and try to call your new FCGI application. Example: http://127.0.0.1:8080/myfcgi/func1call or http://127.0.0.1/myfcgi/func1call depending on your Apache installation and configuration. "myfcgi" is the ScriptAlias name specified for the FCGI application, and func1call is the action name we want to call within our default web module. If you have multiple web modules, you can enter the desired web module name before the action name, for example: http://127.0.0.1:8080/myfcgi/webmodule1/func1call or http://127.0.0.1/myfcgi/webmodule1/func1call If there is any problem, you can try and check the Apache error.log for clues. 2.2 mod_fastcgi from fastcgi.com on Linux: ------------------------------------------ Setup Steps: 2.2.1 There are no binaries offered for download for Linux, so we need to get the mod_fastcgi source codes from http://www.fastcgi.com/dist/ and compile it.  2.2.1.1. If the Apache development package headers are not installed, then we need to get them first: (on Ubuntu) >sudo apt-get install apache2-dev  2.2.1.2. Get the fastcgi module sources (get the latest SNAP or highest version stable release from http://www.fastcgi.com/dist/ ) >cd /home/johndoe >wget http://www.fastcgi.com/dist/mod_fastcgi-xxxxxxxxxxx.tar.gz Example: wget http://www.fastcgi.com/dist/mod_fastcgi- SNAP-0910052141.tar.gz  2.2.1.3. Unpack the source code files. >tar -xzf mod_fastcgi-*.tar.gz  2.2.1.4. Configure the makefile (we use Apache 2.2, so need the Makefile.AP2). >cd mod_fastcgi-* >cp Makefile.AP2 Makefile  2.2.1.5. Edit the copied Makefile and set top_dir to the proper apache source directory created by the Apache development package install. For example, on Ubuntu it is /usr/share/apache2 (containing the .mk file).  2.2.1.6. Compile and install (on Ubuntu) >make >sudo make install 2.2.2. Now, we should have a mod_fastcgi.so in /usr/lib/apache2/modules (on Ubuntu) 2.2.3. If you have not done so yet, compile your FCGI application. In case you want to set up an External FCGI Server, then you must specify a port number in your main project file (.lpr) before the Application.Run instruction (this is the only change needed). Example: <...snip...>   Application.Initialize;   Application.Port:=1234;//Port the FCGI application is listening on   Application.Run; <...snip...> 2.2.4 Edit the Apache configuration file (/etc/apache2/apache2.conf on Ubuntu) and add to the end:  2.2.4.a External FCGI Server ############ LoadModule fastcgi_module "/mod_fastcgi.so"   "> #    Options +ExecCGI  <- not needed if ScriptAlias is used below     Order allow,deny     Allow from all   #External FCGI app, has to be manually started and running when a request comes in   FastCgiExternalServer "/" -host 127.0.0.1: -idle-timeout 30 -flush #optionally, to shorten the URL and to not display the executable file name (if used, no +ExecCGI is needed above):   ScriptAlias /myfcgi "/" ############ Example: LoadModule fastcgi_module "/usr/lib/apache2/modules/mod_fastcgi.so"       Order allow,deny     Allow from all     FastCgiExternalServer "/home/johndoe/LazarusFCGITest/helloworld" -host 127.0.0.1:1234 -idle-timeout 30 -flush   ScriptAlias /myfcgi "/home/johndoe/LazarusFCGITest/helloworld"  2.2.4.b Regular FCGI Server Replace the FastCgiExternalServer line above with   FastCgiServer "/" -idle- timeout 30 Example: LoadModule fastcgi_module "/usr/lib/apache2/modules/mod_fastcgi.so"       Order allow,deny     Allow from all     FastCgiServer "/home/johndoe/LazarusFCGITest/helloworld" -idle- timeout 30   ScriptAlias /myfcgi "/home/johndoe/LazarusFCGITest/helloworld" (the project is compiled into directory /home/johndoe/LazarusFCGITest/ , and the FCGI application is called helloworld with no file extension) Note, there are many ways to configure the FCGI applications, this is just but one example. You can check the Apache and mod_fastcgi documentation for alternatives. For example, some more information available at http://www.fastcgi.com/docs/faq.html#typical_httpd.conf 2.2.5 Start/Restart your Apache server. If FastCgiExternalServer is used, then start your application manually, so it can start accepting incoming requests from the web server. On the other hand, if FastCgiServer is used and everything went according to plan, then the Apache error.log should contain a warning message about your FCGI application, saying that it was started. Something like: [warn] FastCGI: server "/home/johndoe/LazarusFCGITest/helloworld" started... 2.2.6 Open your web browser and try to call your new FCGI application. Example: http://127.0.0.1/myfcgi/func1call "myfcgi" is the ScriptAlias name specified for the FCGI application, and func1call is the action name we want to call within our default web module. If you have multiple web modules, you can enter the desired web module name before the action name, for example: http://127.0.0.1/myfcgi/webmodule1/func1call If there is any problem, you can try and check the Apache error.log for clues. ====================================================================== ========= ---------------------------------------------------------------------- nicke85 · 2011-06-20 21:10 Thank you for your time bobo..tutorial is very nice..do you plan to make pdf maybe!? ---------------------------------------------------------------------- bobo · 2011-06-20 21:19 I did not plan to make it in other formats, anyone is free to do so.  This is now included in the latest FPC SVN under /packages/fcl- web/src/base/FCGI-README.txt If something is not clear, please let me know so the file can be updated. We have also moved (as well as updated/fixed for the latest fcl-web changes) the fpweb examples from Lazarus to FPC under /packages/fcl- web/examples/ because LCL is no longer required for fpweb applications. ---------------------------------------------------------------------- xenblaise · 2011-06-25 03:09 hi Just want to ask first before applying this on the near future, Thus this make my web app rans faster? thanks ---------------------------------------------------------------------- bobo · 2011-06-25 05:47 The short answer: Yes. The long answer and the comparison between CGI and FCGI can be found here: http://www.fastcgi.com/drupal/node/6?q=node/15 However, for simple, low traffic websites the difference might not be noticeable. ---------------------------------------------------------------------- jixian.yang · 2011-07-26 15:11 FastCGI can not be found in SVN18020 of Freepascal. ---------------------------------------------------------------------- Silvio Clécio · 2011-08-01 23:30 The long thread about FCGI: http://www.mail- archive.com/lazarus@lists.lazarus.freepascal.org/msg20061.html ---------------------------------------------------------------------- kodok_buncit · 2011-08-12 08:24 hi bobo and all.. is this fcgi app multhithread? ---------------------------------------------------------------------- bobo · 2011-08-20 17:49 kodok_buncit: The CGI modules are handling one request at a time - there is no need for multi-thread theoretically - because the web server starts up a new instance of the module for each request. If there are multiple simultaneous requests are arriving, then there will be multiple instances of the CGI application running at the same time. For FCGI modules, since they are sitting in the memory, it would be possible to do multi-threading, but the standard is to have a queue for multiple request handling and the protocol has an identifier for every request so the server does not mixes up the responses from the same FCGI application instance. A "pseudo" multi-threading is done kind of the same way as with CGI modules (where every request spawns a new copy of the CGI application) because the web server (Apache in these examples) can spawn multiple instances of the FCGI module to sit in the memory and to wait for incoming requests. There are many many configuration settings for the web server on how to do the FCGI request/response handling, the best if you read up on them in the documentation. For example, how many instances of the FCGI application to spawn, after how many requests to restart an FCGI instance, after how much idle time to restart an FCGI instance, etc. (these vary between the mod_fcgid and mod_fastcgi, each has their own settings) ---------------------------------------------------------------------- kodok_buncit · 2011-08-20 21:21 thanks bobo, i'm curious about how implemented connection pooling(database) on fcgi environment and share object instance accross all client/request so the app can be more efficient. I will read more about fastcgi ---------------------------------------------------------------------- GiovanniF · 2012-02-27 08:14 " using fpweb/fcl-web for Windows/Linux" How can one "use fpweb/fcl-web" with LINUX? Does ANYONE know a way to install the components ( fpweb ) under LINUX since the package ( lpk ) file is missing? Thanks for ANY helpö. ---------------------------------------------------------------------- Leledumbo · 2012-02-28 03:35 QuoteHow can one "use fpweb/fcl-web" with LINUX? Does ANYONE know a way to install the components ( fpweb ) under LINUX since the package ( lpk ) file is missing? Thanks for ANY helpö.Missing? What do you have under components/fpweb then? ---------------------------------------------------------------------- GiovanniF · 2012-03-02 11:47 Well...... Read the message:  I am talking about LINUX version of fpweb. IF you go to the components DIR and look inside the fpwep component DIR you will see that the file weblaz.lpk is MISSING. Have a nice day. ---------------------------------------------------------------------- BigChimp · 2012-03-02 13:29 On Debian x64 with FPC fixes_2.6, Lazarus trunk (today) I have: Code: [Select]ls -al /home/pascaldev/lazarus/components/fpweb/weblaz.lpk -rw-r--r-- 1 pascaldev pascaldev 3867 Feb 18 15:16 /home/pascaldev/lazarus/components/fpweb/weblaz.lpk It compiles as well... Perhaps you have a faulty download/buggy snapshot/svn version? ---------------------------------------------------------------------- TheBlackSheep · 2012-03-29 11:39 Hi all I struggled with this for a while but think I'm getting somewhere now; Using the httpd-2.4.1-win32-ssl_0.9.8t.zip  & mod_fcgid-2.3.7-win32.zip from ApacheLounge - installed apache as a service and put the fcgid module in the modules folder in the apache folder structure. Created a folder "C:\FastCGI" and set the following in the apache http.conf Code: [Select] Options All AllowOverride All Require all granted LoadModule fcgid_module modules/mod_fcgid.so      SetHandler fcgid-script     Order allow,deny     Allow from all     ScriptAlias /myfcgid "C:/FastCGI/helloworld.exe"   ScriptAlias /fcgilist "C:/FastCGI/listrecords.exe" The bit I struggled with was that the demos have the listening "port" set to 2015 in the application source (the lpr file) - once you comment this out it seems to work fine with this setup - both applications are launched the first time they are called from the browser and remain in memory for each subsequent request. Using the other fastcgi dll (i.e. not the apache fcgid module but the fastcgi module) with the dll renamed and the following configuration (the above fcgi version commented out) LoadModule fastcgi_module modules/mod_fastcgi-2.4.6-AP22.so I get an error trying to start apache; "cannot load C:/Apache24/modules/mod_fastcgi-2.4.6-AP22.so into server: The specified procedure could not be found." so this version might not work with Apache 2.4.1 - does anyone know of a verison that does or have I missed something? I'm not really bothered about this latter module unless it's the only way to do realtime debugging so the question is using apache's own fcgid module (the first setup) is there an easy way to setup the configuration to do realtime debugging (i.e. set breakpoints etc)? TheBlackSheep ---------------------------------------------------------------------- bobo · 2012-11-22 08:34 Quote"so this version might not work with Apache 2.4.1 - does anyone know of a verison that does or have I missed something?" mod_fastcgi does not seem to support Apache 2.4.X , the maintenance is totally stopped on the project it seems. However, Apache 2.4.X has a new way to do the same functionality now, to do external FCGI server (FastCgiExternalServer) via the new mod_proxy_fcgi module. I will update the top post once it is figured out how to make it work with fpc/lazarus , and the SVN is updated for fpc as needed to support it. Most probably this new way will be the preferred FCGI external server functionality instead of the old third party mod_fastcgi . mod_fcgid still works as before, so we still have a perfectly working FCGI way with the latest Apache. Both mod_fcgid and mod_proxy_fcgi are included in Apache from 2.3.X . BTW, mod_fcgid is not for real time debugging (unless you are using log files) because in this case Apache loads the FCGI application at startup so there is no way to use a debugger like with the mod_fastcgi method (or with the new mod_proxy_fcgi module). The  two latter you need to start the FCGI app yourself, hence you can use a debugger to execute them. Compared to the debugging benefits of the two latter ones, mod_fcgid instead might be a good way for live web servers though, because Apache can start the FCGI app instances as needed, automatically. Quote"The bit I struggled with was that the demos have the listening "port" set to 2015 in the application source (the lpr file) - once you comment this out it seems to work fine with this setup - both applications are launched the first time they are called from the browser and remain in memory for each subsequent request."The port is only used for the mod_fastcgi  FastCgiExternalServer mode (and the new Apache 2.3.X+ mod_proxy_fcgi module) . All other modes do not need that line because they are not using a port to listen on. ---------------------------------------------------------------------- GiovanniF · 2014-03-07 13:26 One simple question: Using this method: AResponse.Contents.LoadFromFile('aaa.html'); HOW do I incluse a image into my 'aaa.html' like ? Thanks in advance. Rgds GiovanniF ---------------------------------------------------------------------- RalphMuc · 2014-03-31 17:35 Hi there thanks for the docu. Silvio's FastCGI Sample works fine. Do you know how to pass environment Vars to fastcgi ? Code: [Select]procedure TMyAction.Get; begin  for I := 1 to ParamCount do    WriteLn('Param ' + inttostr(I)  +  ': ' + ParamStr(I));   WriteLn( 'myREMOTE_ADDR: ' + GetEnvironmentVariable('myREMOTE_ADDR') );  WriteLn( 'BROOK_CLT_ENV_HTTP_USER_AGENT: ' + GetEnvironmentVariable(BROOK_CLT_ENV_HTTP_USER_AGENT) );  WriteLn( 'BROOK_SRV_ENV_REMOTE_ADDR: ' + GetEnvironmentVariable(BROOK_SRV_ENV_REMOTE_ADDR) );  WriteLn( 'BROOK_SRV_ENV_X_FORWARDED_FOR: ' + GetEnvironmentVariable(BROOK_SRV_ENV_X_FORWARDED_FOR) ); end;      Solved ... rgds Ralph ---------------------------------------------------------------------- RalphMuc · 2014-05-08 21:04 Hi, the FastCGI sample source code is here : https://github.com/rbmuc/EasyBBS Ralph ---------------------------------------------------------------------- RalphMuc · 2014-05-10 16:28 Hi, i build a installable version of Apache 2.4.9 x86  ( with openssl 1.0.1g and FastCGI ) support. download https://bergertime.eu/download/httpd-2.4.9-win32-x86-openssl- 1.0.1g.exe docu_en https://bergertime.eu/download/apache_fcgi_en.pdf docu_de https://bergertime.eu/download/apache_fcgi.pdf Ralph ---------------------------------------------------------------------- cahaya · 2014-12-12 17:41 Hi all, I just check mod_proxy_fcgi, it work for lazarus with minor bugs(don't know bug or not). Here my steps : 1. install apache 2.4 (just copy in windows) 2. enable LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so and     LoadModule proxy_module modules/mod_proxy.so in httpd.conf.     You can find file httpd.conf in directory conf. 3. add a new line ProxyPass /fcgi-bin/ fcgi://localhost:9000/ in httpd.conf     /fcgi-bin/ is a directory I put under htdocs, you can put anywhere as long     as you set permission. 4. add ScriptAlias /fcgi-bin/ "${SRVROOT}/htdocs/fcgi-bin/" under     alias_module section. 5. add permission         AllowOverride None     Options None     Require all granted     run your program fcgi. Don't forget to add Application.port := 9000; uses   fpFCGI, Main; begin   Application.Title:='FpCgi';   Application.Initialize;   Application.port := 9000;   Application.Run; end. Please notice unit fpFCGI not fpCGI. You can not use actio, something like /fcgi- bin/helloword.exe/testaction. In cgi action testaction is recognized by both. But in proxy, it seem apache do not compatible with that feature. ---------------------------------------------------------------------- chain_reaction · 2015-11-06 04:22 Hello everyone, just to confirm that fp-cgi also works perfectly on lighttpd. ---------------------------------------------------------------------- DarkEagle · 2016-05-28 17:34 i'm finding on many web , so this thread can help me. thank you very much ---------------------------------------------------------------------- egsuh · 2017-03-24 05:47 Hello, I've done a few web projects successfully with Delphi on IIS, both CGI and ISAPI. I found Lazarus recently, and checking feasibility of web application with Lazarus. I found that fpweb in Lazarus supports CGI and FastCGI. I've tested them on IIS, and found CGI works fine, but FastCGI doesn't work as expected. I tried to run it on IIS7 (Windows 7) like as follows:    http://localhost/fcgi/fcgiproject2.exe but the response is:     The application encountered the following error:     Error: Could not determine HTTP module for request "fcgi"     Stack trace:     $0000000100036793     $000000010003685B     $0000000100036522     $0000000100036D8F     $0000000100035FEF     $000000010003715F     $0000000100038365     $0000000100001847 line 16 of fcgiproject2.lpr      ...................... Interesting point is that "fcgi" is just a folder name, as the executable file path is  c:\inetpub\wwwroot\fcgi\fcgiproject2.exe.  As "c:\inetpub\wwwroot" is the home directory for localhost, specifying "localhost/fcgi" works fine for other types of files.  I have tested Apache server too, but the results are the same. I think there are some issue related with pathinfo of other thinks as fcgi is not read as a folder name. If any of you have solved the same problem, please let me know. I would really appreciate. Regards, ---------------------------------------------------------------------- mobilevil · 2017-08-16 03:17 hello, edit: I thought 1.8 RC4 was broken but I was wrong. with 1.8 RC4, the FPC version is updated to 3.0.4 RC1, in which the fp-web package is updated to support routing. to enable legacy routing to fix my stupid hello world FCGI applicaiton,  add this to the main program. Application.LegacyRouting:=true; just to report that mod_fastcgi from mod_fastcgi-2.4.7.1-2.4.x-x64-vc14.zip, https://www.apachehaus.net/modules/mod_fastcgi/ is working for me, default and custom actions are fine. both FastCgiServer and FastCgiExternalServer are working, but note that only the external config need the port setup line in the FCGI project   Application.Port:=9999; // For example    The FastCgiExternalServer mode is really good for debugging, and under windows the FCGI exe won't be locked by anyone so you can kill it anytime you like. You can debug, modify the code and build as you wish. I am using apache 2.4 64 bit, httpd-2.4.27-x64-vc14.zip this is my apache config LoadModule fastcgi_module "modules/mod_fastcgi.so"       #this two is for 2.2 only     #Order allow,deny     #Allow from all     #this line is needed for 2.4     Require all granted     #regular fast cgi, CGI exe itself is always loaded by apache, non- debuggable, for production #  FastCgiServer "C:/checkout/testfcgi/lib/x86_64-win64/testfcgi.exe" -idle-timeout 30   #external server fast cgi, debuggable, have to start the cgi program manually   FastCgiExternalServer "C:/checkout/testfcgi/lib/x86_64-win64/testfcgi.exe" -host 127.0.0.1:9999 -idle-timeout 30 -flush   ScriptAlias /myfcgi "C:/checkout/testfcgi/lib/x86_64-win64/testfcgi.exe" -------------------------------------- and here's my FCGI prrogram's main begin   Application.Title:='fcgiproject1';   { Uncomment the port setting here if you want to run the     FastCGI application stand-alone (e.g. for NGINX) }   Application.Port:=9999; // For example   Application.Initialize;   Application.Run; end.      procedure TFPWebModule1.helloRequest(Sender: TObject; ARequest: TRequest;   AResponse: TResponse; Var Handled: Boolean); var   i:integer; begin   AResponse.contents.Add(datetimetostr(now)+' Hello world fast cgi with external server!
');   AResponse.contents.Add(ARequest.URL+'
');   for i:=0 to ARequest.FieldCount-1 do   begin     AResponse.contents.Add(ARequest.FieldNames + ' ' + ARequest.FieldValues+sLineBreak);   end;   Handled:=true; end; procedure TFPWebModule1.TFPWebAction0Request(Sender: TObject;   ARequest: TRequest; AResponse: TResponse; Var Handled: Boolean); var   i:integer; begin   AResponse.contents.Add(datetimetostr(now)+' default action with external server!
');   AResponse.contents.Add(ARequest.URL+'
');   for i:=0 to ARequest.FieldCount-1 do   begin     AResponse.contents.Add(ARequest.FieldNames + ' ' + ARequest.FieldValues+sLineBreak);   end;   Handled:=true; end; initialization   RegisterHTTPModule('TFPWebModule1', TFPWebModule1); end. ---------------------------------------------------------------------- sfischer · 2018-08-05 14:28 Hi, I try to get example program fcl-web/examples/helloworld running with lighttpd as webserver on raspian. an excerpt of my /etc/lighttpd/lighttpd.conf looks like: fastcgi.debug = 1 fastcgi.server = (     "/helloworld" => (       "helloworld.fcgi.handler" => (         "socket" => "/tmp/helloworld.socket",         "check-local" => "disable", # "host" => "127.0.0.1", # "port" => "2015",         "bin-path" => "/var/lib/BASIS/pump/www/cgi-bin/helloworld",         "max-procs" => 1       )     ) ) can someone supply a small manual, ho to get this working thanks, SF ---------------------------------------------------------------------- matthius · 2020-12-10 15:35 I want to test my fast CGI. I have created a VirtualHost to use 2015 port. I get always this error for a long time, on Debian Buster and Ubuntu 20 : Exception at 000000000048DE0B: EFPWebError: Failed to bind to port 2015. Socket Error: 98. The config is here : Options All AllowOverride All Require all granted Options +ExecCGI +FollowSymLinks    SetHandler fcgid-script    AddHandler fcgid-script .cgi     Order allow,deny     Allow from all Listen 2015 ScriptAlias /cgi-bin "/var/www/cgi-bin" ---------------------------------------------------------------------- mobilevil · 2020-12-11 15:44 quick google and a random guess https://stackoverflow.com/questions/17780291/python-socket-error- errno-98-address-already-in-use address is already in use, since it is not a well known port, I guess it is not something else that is using it. I GUESS you have a looping config file so that apache is trying to bind the same port twice. or something similar ---------------------------------------------------------------------- matthius · 2020-12-11 16:29 After and only after starting a fcgi script i get this : netstat | grep 9000 tcp6       0      0 127.0.0.1:9000          127.0.0.1:58058         TIME_WAIT ---------------------------------------------------------------------- mobilevil · 2020-12-12 14:06 OK I was wrong. The bind error came from FCGI, not APACHE. Apache should use it's own port, and FCGI use another. e.g. Apache should use the default port 80, and FCGI's port is defined in both FCGI's main, and apache's config see my post a few post above dated 2017. look for FastCgiExternalServer and application.port ---------------------------------------------------------------------- matthius · 2020-12-12 14:08 FastCgiExternalServer command is not recognized. I use 2015 port in my Fast CGI. I use Listen command. ---------------------------------------------------------------------- mobilevil · 2020-12-12 14:37 are you using apache? which OS? focus on port HTTP port 80 first. the listen directive define the port apache use to talk to browser. not FCGI. usually the listen directive is already defined in the default config. both 80 for HTTP and 443 for HTTPS. the FastCgiExternalServer directive is inside block, which is only valid if LoadModule fastcgi_module "modules/mod_fastcgi.so" is also defined. FastCgiExternalServer tell apache to use the specified port number to talk to the FCGI program. browser -> port 80 -> apache -> port 2015 -> FCGI so apache "listen" to port 80, and FCGI "listen" to port 2015. ---------------------------------------------------------------------- matthius · 2020-12-12 15:59 I use this config : LoadModule fastcgi_module "/usr/lib/apache2/modules/mod_fastcgi.so" Options All AllowOverride All Require all granted Options +ExecCGI +FollowSymLinks    SetHandler fcgid-script    AddHandler fcgid-script .cgi     Order allow,deny     Allow from all FastCgiExternalServer "/var/www/cgi-bin/cgi2015.cgi" -host 127.0.0.1:2015 -idle-timeout 30 -flush ScriptAlias /cgi-bin "/var/www/cgi-bin" I got that it is unable to connect. After it has no header. But i have created a response. ---------------------------------------------------------------------- matthius · 2020-12-12 16:01 Here is the main unit : program fcgihellow; {$mode objfpc}{$H+} uses   fpFCGI, u_hellofcgi; begin   Application.Title:='fcgihellow';   { Uncomment the port setting here if you want to run the     FastCGI application stand-alone (e.g. for NGINX) }   Application.Port:=2015; // For example   Application.Initialize;   Application.Run; end.                 Here is the web module : unit u_hellofcgi; {$mode objfpc}{$H+} interface uses   SysUtils, Classes, httpdefs, fpHTTP, fpWeb; type   { TFPWebModule1 }   TFPWebModule1 = class(TFPWebModule)     procedure DataModuleRequest(Sender: TObject; ARequest: TRequest;       AResponse: TResponse; var Handled: Boolean);   private   public   end; var   FPWebModule1: TFPWebModule1; implementation {$R *.lfm} { TFPWebModule1 } procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;   AResponse: TResponse; var Handled: Boolean); var   LName: String; begin   LName := ARequest.QueryFields.Values['Name'];   if LName = EmptyStr then     with AResponse.Contents do     begin       Add('
Please tell me your name:');       Add('');       Add('');       Add('
');     end   else     AResponse.Content := 'Hello World, ' + LName + '!';   Handled := true; end; initialization   RegisterHTTPModule('TFPWebModule1', TFPWebModule1); end. ---------------------------------------------------------------------- matthius · 2020-12-13 14:19 iptables and ufw accept entering sockets. ---------------------------------------------------------------------- matthius · 2020-12-13 17:24 I reinstalled apache2. There is always the error. ---------------------------------------------------------------------- zamronypj · 2020-12-13 22:46 you should inspect error from apache error log to find what is wrong. if you use Apache mod_proxy_fcgi, you can setup virtual host using ProxyPassMatch as follows Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      ServerName www.example.com     DocumentRoot /path/to/app/public               Options +ExecCGI         AllowOverride FileInfo         Require all granted          ProxyRequests Off    ProxyPassMatch /(css|images|js).* !    ProxyPassMatch ^/(.*)$ fcgi://127.0.0.1:2015  mod_proxy_fcgi is built-in module since Apache 2.4, you just need to enable it. For example on Debian Linux Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ sudo a2enmod proxy_fcgi  Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     ProxyRequests Off    ProxyPassMatch /(css|images|js).* !    ProxyPassMatch ^/(.*)$ fcgi://127.0.0.1:2015  First line turn off forward proxy. Second line tells Apache to serve any files from css, images or js directory in document root directly. Third line tells Apache to pass everything else to application listen on 127.0.0.1 port 2015. "fcgi:" tells mod_proxy to use mod_proxy_fcgi. ---------------------------------------------------------------------- matthius · 2020-12-14 18:42 Thanks for reply ! I got always the same error : [Mon Dec 14 19:40:33.894880 2020] [proxy:error] [pid 10109:tid 140072876771072] (111)Connection refused: AH00957: FCGI: attempt to connect to 127.0.0.1:2015 (127.0.0.1) failed [Mon Dec 14 19:40:33.894975 2020] [proxy_fcgi:error] [pid 10109:tid 140072876771072] [client 127.0.0.1:52044] AH01079: failed to make connection to backend: 127.0.0.1 I use Lazarus 2.0.10 ---------------------------------------------------------------------- zamronypj · 2020-12-14 23:34 You need to check if Apache and application is on same serverapplication does run and listen on specified port To check if application listen on correct port Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ netstat -tunlp | grep 127.0.0.1:2015 ---------------------------------------------------------------------- matthius · 2020-12-15 12:05 No result. Must i keep FastCgiExternalServer ? ---------------------------------------------------------------------- cappe · 2020-12-15 15:25 but if you write like this something comes back     $ netstat -tunlp | grep 2015 ---------------------------------------------------------------------- matthius · 2020-12-15 16:16 No result too. ---------------------------------------------------------------------- zamronypj · 2020-12-16 01:34 If netstat command above shows no result, it means your application is not running. That is why Apache cannot connect to your application. You need to run application first. ---------------------------------------------------------------------- egsuh · 2020-12-16 02:52 I have no experience with Apache, and I can run FCGI modules on IIS - Windows 10 successfully. Logically thinking, IIS runs FCGI program so I do not assign separate port -- I think the same applies to Apache. Separate port is necessary to run it as HTTP server, which responses directly, not via IIS or Apache.  First, test with following main program. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin   Application.Title:='fcgihellow';  { Uncomment the port setting here if you want to run the    FastCGI application stand-alone (e.g. for NGINX) }  // Application.Port:=2015; // For example    LegacyRouting:= True;    Application.Initialize;   Application.Run;end.  In IIS, I have to set Handler Mappings --- I have to add mapping for the application program. Don't know how to do this in Apache. Important thing is that allowPathInfo should be set TRUE. Again, no idea whether this is applicable to Apache. But as I posted in other posts, Free Pascal fCGI model is not perfect --- I believe it has some problem in memory management. (the author acknowledges this).  Anyway please let me know your result. I'd like to have as many developers of Lazarus webserver as possible.^^ ---------------------------------------------------------------------- matthius · 2020-12-16 11:57 I have deleted link to fastcgi module. It does always no result. ---------------------------------------------------------------------- matthius · 2021-08-25 14:06 I install alibaba TENGINE for NGINX. It seems well done. There is FastCGI module but to let Fast CGI working i need to use fcgi wrapper. How can i do ? I have 502 bad gateway error. My config : server {         listen       80;         server_name  localhost;         access_log  /var/log/nginx/fastcgi.access.log;         index index.html index.php index.html;         root /var/www;         location /cgi         {         }         location ~ \.fcgi$         {             include fastcgi_params; #           fastcgi_pass unix:/var/run/fcgi.sock;             fastcgi_pass 127.0.0.1:9000;             fastcgi_index  hellow.fcgi;             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;             fastcgi_param AUTH_USER $remote_user;             fastcgi_param REMOTE_USER $remote_user;         } } ---------------------------------------------------------------------- nicanor · 2021-10-04 10:50 Hi I found this page showing information about to configure nginx and fastcgi: https://difyel.com/nginx/how-to-setup-nginx-with-cgi-fastcgi- and-c-bash-lisp-or-any-other-programming-language-on-debian-and- freebsd/index.html In the first example the autor shows how to set nginx whit a fastcgi wrapper to CGI. And second he shows how to use a fastcgi server with nginx .... (Well, at least, it was i understood... Sorry, I'm completely newbie in this area, and just learning) Unfortunately I have no success applying the second way in my local machine whit ubuntu. Hope this may be useful for you! Cheers ---------------------------------------------------------------------- nicanor · 2021-10-04 11:06 By the way, if you were to start developing a new web application today... And wanted to use lazarus/fpc... As a matter of fact, you could tell me if the way is still to create fastcgi application, or is there some other way. It's just because I'm new even in this web area, sorry ---------------------------------------------------------------------- egsuh · 2021-10-04 11:18 I have successfully tested NGINX with free pascal FastCGI.  Please refer to following thread. https://forum.lazarus.freepascal.org/index.php/topic,56583.msg420471.h tml#msg420471 I experienced some problems with FastCGI with MS IIS, but this problem does not happen with NGINX. @nicanor, I do not have a wide experience but Lazarus/FPC is quite useful tool to develop web server application, based on my experience. I have set up a survey platform, and doing surveys with it. ---------------------------------------------------------------------- fblu · 2021-11-19 12:14 Quote from: nicanor on October 04, 2021, 01:06:55 pmBy the way, if you were to start developing a new web application today... And wanted to use lazarus/fpc... As a matter of fact, you could tell me if the way is still to create fastcgi application, or is there some other way. It's just because I'm new even in this web area, sorry I'm not much of a webdev either, but if  you want to create a REST API and want to use fpc, you can use Horse ( https://github.com/HashLoad/horse ). ---------------------------------------------------------------------- matthius · 2022-04-27 10:16 On 2021, there is a Fast CGI development kit called fcgi2 : http://fastcgi-archives.github.io/fcgi2/doc/fcgi-devel-kit.htm But they are very poor in examples which not exists as in documentation. I do not know how to use it on 2022-04. For example, we don't see any link to a fastcgi in html pages.