apache添加fastcgi支持

A,安装apache服务器和fastcgi模块支持(ubuntu测试)

sudo apt-get install apache2
sudo apt-get install libapache2-mod-fastcgi

apache2的配置文件目录如下:

ciaos@ubuntu:/etc/apache2$ ll
total 80
drwxr-xr-x  7 root root  4096 May 28 21:33 ./
drwxr-xr-x 93 root root  4096 May 28 21:44 ../
-rw-r--r--  1 root root  8346 Feb  7  2012 apache2.conf
drwxr-xr-x  2 root root  4096 May 28 21:32 conf.d/
-rw-r--r--  1 root root  1322 Feb  7  2012 envvars
-rw-r--r--  1 root root     0 May 28 21:32 httpd.conf
-rw-r--r--  1 root root 31063 Feb  7  2012 magic
drwxr-xr-x  2 root root  4096 May 28 21:36 mods-available/
drwxr-xr-x  2 root root  4096 May 28 21:36 mods-enabled/
-rw-r--r--  1 root root   750 Feb  7  2012 ports.conf
drwxr-xr-x  2 root root  4096 May 28 21:58 sites-available/
drwxr-xr-x  2 root root  4096 May 28 21:58 sites-enabled/

配置fastcgi目录,只需要修改sites-enabled/000-default文件添加fastcgi的目录结构

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ScriptAlias /fcgi-bin/ /var/lib/apache2/fastcgi/
<Directory "/var/lib/apache2/fastcgi">
    AllowOverride None
    Options FollowSymLinks
    Order allow,deny
    Allow from all
    SetHandler fastcgi-script
</Directory>

重启apache服务器如下:

sudo apachectl -k restart

B,接下来写fastcgi程序测试(用C来测试)

1,安装fastcgi库
make的时候如果出现“error: 'EOF' was not declared in this scope”错误,则在出错文件加上#include <stdio.h>

2,编辑个简单的fastcgi程序

#include <fcgi_stdio.h>

int main()
{
        int count = 0;
        while(FCGI_Accept() >= 0) {
                printf("Content-type: text/html\r\n");
                printf("\r\n");
                printf("Hello world!<br>\r\n");
                printf("Request number %d.", count++);
        }
        return 0;
}

3,编译并放到apache的cgi和fcgi目录下测试

gcc sim.c -lfcgi -o test.cgi
cp test.cgi /usr/lib/cgi-bin/
访问 http://192.168.1.108/cgi-bin/test.cgi , 每次打印Request number都是0

gcc sim.c -lfcgi -o test.fcgi
cp test.fcgi /var/lib/apache2/fastcgi/
访问 http://192.168.1.108/cgi-bin/test.fcgi , 每次打印Request number都会递增

:如果ldd找不到依赖库,三种方法如下
1,export LD_LIBRARY_PATH=/usr/local/lib/
2,直接把so文件拷贝到/lib/目录下,这个目录一般都是
3,/etc/ld.so.conf添加响应目录, /sbin/ldconfig –v更新

posted @ 2014-05-28 22:17  ciaos  阅读(835)  评论(0编辑  收藏  举报