I have written a program with c++ and compiled it with gcc ( like the sample in the fastcgi.com) but i dont know how to run it on localhost.
everywhere i searched , i found the php configuration for mod_fcgi which wont work for c++.
does any body configured apache and mod_fcgi to run a c++ web application ???
FastCGI is a language independent, scalable, open extension to CGI that provides high performance and persistence without the limitations of server specific APIs. FastCGI applications are not limited to a particular development language (the protocol is open).
mod_fcgi? I have found only mod_fastcgi and mod_fcgid. Apache configuration looks pretty simple for both. Lets compile FastCGI example and create a minimalistic Apache instance to serve it:
Install libfcgi-dev
Create temporary directory somewhere and compile the example from https://opensource.apple.com/source/FastCGI/FastCGI-4/fcgi/doc/fcgi-devel-kit.htm#S3.1
When you simply run it, it already has some output:
$ ./tiny-cgi
Content-type: text/html
<title>FastCGI Hello!</title><h1>FastCGI Hello!</h1>Request number 1 running on host <i>(null)</i>
Install apache2 and libapache2-mod-fcgid; create configuration file apache.conf:
User www-data
Listen 8080
PidFile apache.pid
DocumentRoot .
LoadModule fcgid_module /usr/lib/apache2/modules/mod_fcgid.so
SetHandler fcgid-script
Options +ExecCGI
ErrorLog error.log
User www-data is important, because it has access to /var/lib/apache2/fcgid/sock/
, which is pretty important for fcgid (I am running on Debian, maybe somewhere else it will be different). Having DocumentRoot in the same directory with the rest is not very good, but this is just a quick example.
Run sudo /usr/sbin/apache2 -d . -f apache.conf -X
That -X
is for debug mode, when the server does not daemonize (does not detach), which is pretty handy for such playing.
Go to http://localhost:8080/tiny-cgi
, where you will see output from your FastCGI program. If not, see error.log
.
Stop Apache, install libapache2-mod-fastcgi, replace the two lines in configuration with:
LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
SetHandler fastcgi-script
Visit http://localhost:8080/tiny-cgi
again.
Here's an example from my dev PC at home. It's a C++ web service running on 127.0.0.1:90 that I'm testing/debugging. The "FcgidIOTimeout" is set to 3600 so mod_fcgid won't timeout waiting for a response while I step through the fcgi process with gdb (the debugger). If it times out while debugging, the fcgi app will be killed. A little further down there is a ScriptAlias and a Directory telling Apache where the cgi folder is..."/home/dgnorton/prj/dfi/build/src/"...which is the build output folder for my project. You'll also need to check the permissions of that directory.
I only use this on my home system for debugging. Read the Apache and mod_fcgid docs before using any of this in the wild.
Listen 90
NameVirtualHost 127.0.0.1:90
<VirtualHost 127.0.0.1:90>
ServerName www.example1.com
DocumentRoot /var/www/dfi
<IfModule fcgid_module>
FcgidIOTimeout 3600
</IfModule>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi/ /home/dgnorton/prj/dfi/build/src/
<Directory "/home/dgnorton/src/dfi/build/src">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With