Apache2 FastCGI C Demo

安裝依賴

sudo apt-get install libapache2-mod-fastcgi
a2enmod fastcgi
sudo apt-get install libfcgi-dev libfcgi0ldbl
 

編寫FCGI代碼

代碼fcgi-hello.chtml

#include "fcgi_stdio.h" /* fcgi library; put it first*/
#include <stdlib.h>

int count;

void initialize(void)
{
  count=0;
}

void main(void)
{
/* Initialization. */  
  initialize();

/* Response loop. */
  while (FCGI_Accept() >= 0)   {
    char *host = getenv("SERVER_HOSTNAME");
    printf("Content-type: text/html\r\n"
           "\r\n"
           "<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
           "<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
           "Request number %d running on host <i>%s</i>\n",
            ++count, (host == NULL)?"unknow":host);
  }
}

編譯apache

gcc fcgi-hello.c -o fcgi-hello.fcgi -lfcgi
 

配置Apache

編輯/etc/apache2/mods-available/fastcig.conf, 在AddHandler處添加.fcgi瀏覽器

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  FastCgiIpcDir /var/lib/apache2/fastcgi
</IfModule>

編輯/etc/apache2/sites-available/fcgi-demo.confcurl

<VirtualHost *:8081>
    ServerName localhost
    ServerAdmin you@local.com
    DocumentRoot /var/www
    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log combined
    ServerSignature Off

    <IfModule mod_fastcgi.c>
        <Directory /var/www>
            Options +ExecCGI
            AllowOverride All
            SetHandler fastcgi-script
            Order allow,deny
            Allow from all
            AuthBasicAuthoritative Off
        </Directory>
    </IfModule>
</VirtualHost>

編輯/etc/apache2/ports.conf,添加8081端口的監聽ide

Listen 80
Listen 8081

啓用fcgi-demo站點oop

a2ensite fcgi-demo.conf

重載apacheurl

sudo service apache2 reload

確保防火牆的8081端口是打開的,而後就能夠經過瀏覽器或curl訪問這個fastcgi程序了。code

相關文章
相關標籤/搜索