how to config apache2 and fastCGI to run my c++ application

Sasan Golchin picture Sasan Golchin · Sep 23, 2010 · Viewed 12.3k times · Source

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 ???

Answer

Messa picture Messa · Sep 23, 2010

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:

  1. Install libfcgi-dev

  2. 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>
    
  3. 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.

  4. 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.

  5. Go to http://localhost:8080/tiny-cgi, where you will see output from your FastCGI program. If not, see error.log.

  6. 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
    
  7. Visit http://localhost:8080/tiny-cgi again.