Fastcgi.com
Jump to navigation
Jump to search
Depends
apt-get install libfcgi-dev libapache2-mod-fastcgi spawn-fcgi a2enmod fastcgi
Apache config
from http://whocares.de/fastcgiexternalserver-demystified/all/1/
<IfModule mod_fastcgi.c> FastCGIExternalServer /tmp/my_app.fastcgi -host 127.0.0.1:9000 Alias /my_app /tmp/my_app.fastcgi </IfModule>
First fastcgi.com app
#include <stdlib.h> #include <string.h> #include <syslog.h> #include <alloca.h> #include <fcgiapp.h> #define LISTENSOCK_FILENO 0 #define LISTENSOCK_FLAGS 0 int main(int argc, char** argv) { openlog("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER); int err = FCGX_Init(); /* call before Accept in multithreaded apps */ if (err) { syslog (LOG_INFO, "FCGX_Init failed: %d", err); return 1; } FCGX_Request cgi; err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS); if (err) { syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err); return 2; } while (1) { err = FCGX_Accept_r(&cgi); if (err) { syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err); break; } char** envp; int size = 200; for (envp = cgi.envp; *envp; ++envp) size += strlen(*envp) + 11; char* result = (char*) alloca(size); strcpy(result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n"); strcat(result, "<html><head><title>testcgi</title></head><body><ul>\r\n"); for (envp = cgi.envp; *envp; ++envp) { strcat(result, "<li>"); strcat(result, *envp); strcat(result, "</li>\r\n"); } strcat(result, "</ul></body></html>\r\n"); FCGX_PutStr(result, strlen(result), cgi.out); } return 0; }
- A better C++ example is available on http://www.fastcgi.com/devkit/examples/echo-cpp.cpp
Compile
g++ -lfcgi my_app.cpp -o myapp
Running spawned process
spawn-fcgi -n -p 9000 ./myapp
See the result on http://localhost/my_app/fooxverg3?aaa&bbb
Debug a running process
from http://www.fastcgi.com/om_archive/words/FAQ.htm on
- 2. How do I debug my FastCGI app?
As usual, you must compile the app for debugging (e.g. -g switch to gcc) in order to get the best use of the debugger. Also, your debugger must be able to attach to a running process. Some platforms don't allow attaching to a running process, but most do. A typical command-line syntax for attaching to a running process is gdb prog_name pid where prog_name is the name of the executable and pid is the process number. Consult your debugger's documentation for full information.
and
Sometimes you will want to attach the debugger to your app before it has performed its initialization. One way to enable this is to make your main program look something\ like this: void main(void) { int stopSleep = 0; while (getenv("SLEEP") != NULL && stopSleep == 0) { sleep(2); } /* the application proper starts here */ . . . } Now if you need to debug the application, start it with AppClass ... -initial-env SLEEP=TRUE. This will place the application into the sleep loop as soon as it starts. Then use ps to find the application process, attach your debugger to the process, set whatever breakpoints you like, set stopSleep to a non-zero value, and continue the application. Here's a transcript, using dbx: % dbx fcgi_debug 9488 ... Reading symbolic information for libmp.so.1 Reading symbolic information for libw.so.1 Attached to process 9488 >>>Press ^C here to force a signal<<< stopped in _sigsuspend at 0xdf678004 _sigsuspend+0x4: ta 0x8 Current function is main 9 sleep(2); (dbx) assign stopSleep=1 (dbx) next stopped in main at line 12 in file "fcgi_debug.c" 12 while(FCGI_Accept() >= 0) { (dbx)quit detaching from process 9488 %
Set-up an Eclipse CDT project
Create an Eclipse project, maybe like there
Configure to Run and Debug
- Run -> Run Configurations
- Name: "TestFastcgi Spawned"
- C/C++ Application: "/usr/bin/spawn-fcgi"
- switch to tab Arguments
- Program arguments: "-n -p 9000 build/debug/TestFastcgi"
- Working directory: "${workspace_loc:TestFastcgi/TestFastcgi}"
- click on Close
- Now can Run or Debug the 'TestFastcgi Spawned' configuration
Now you can see the debug working, and run the page on http://localhost/my_app/fooxverg3?aaa&bbb