spawn-fcgi 编译与问题

 

搭建环境

注意发现碰钉可以看看相应程序内的README

安装:nginx、spawn-fcgi、fastcgi、fcgiwrap

nginx

sudo apt-get install nginx-full

 

fastcgi

安装fastcgi的时候报EOF错误可以在include/fcgio.h中包含头文件cstdio

wget http://www.fastcgi.com/dist/fcgi.tar.gz
tar -zxvf fcgi.tar.gz
./configure
make
sudo make install

无法下载的话,可以去百度云下载:

链接:https://pan.baidu.com/s/1nEzOkFC0-rfVMDy_BygLWg
提取码:ty0e

 

spawn-fcgi

复制代码

git clone https://github.com/lighttpd/spawn-fcgi.git
sudo apt-get install autoconf
autoreconf -i
./autogen.sh
./configure
make
sudo make install

复制代码

 

fcgiwrap

git clone https://github.com/gnosek/fcgiwrap.git
autoreconf -i
./configure
make
sudo make install

 

 

配置环境

配置nginx

vim /etc/nginx/sites-enabled/default

整个文件改成这样

复制代码

server {

    listen 80 ;
    server_name nextbin.com;

    location / {
        root /home/zebin/nginx/htdoc/;
        index index.html index.htm;
        try_files $uri $uri/ =404;
    }

    location ~ ^/cgi-bin/.*$ {
        #cgi path: /home/zebin/nginx/cgi-bin/
        root /home/zebin/nginx/;
        fastcgi_pass 127.0.0.1:9000;
        #configure path: /etc/nginx/fastcgi.conf
        #include fastcgi.conf;
        include fastcgi_params;
    } 

}

复制代码

 

配置hosts

vim /etc/hosts

追加

127.0.0.1 nextbin.com

 

配置库文件链接

sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/local/lib/libfcgi.so.0
sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/lib/libfcgi.so.0
sudo mkdir /usr/lib64
sudo ln -s /usr/local/lib/libfcgi.so.0.0.0 /usr/lib64/libfcgi.so.0

 

 

测试cgi

编写cgi程序编译后将可执行程序放在相应目录(如/home/zebin/nginx/cgi-bin/demo)

 demo.cpp

#include "fcgi_stdio.h"
#include

int main(int argc, char* argv[]){
    int count = 0;
    while (FCGI_Accept() >= 0){
        printf("Content-type: text/html\r\n"
        "\r\n"
        "FastCGI Hello!"
        "

FastCGI Hello!

"
        "Request number %d running on host %s\n",
        ++count, getenv("SERVER_NAME"));
    }
    return 0;
}

编译:g++ demo.cpp -lfcgi -o demo

加载fcgi-application

spawn-fcgi -p 9000 -C 10  -f  /usr/local/sbin/fcgiwrap

浏览器访问 nextbin.com/cgi-bin/demo 都成功

 

 

=========小结=========

一些用得上的命令

ps -aux | grep cgi
nginx -t
ln --help

 

因为用spawn-fcgi 启动后不能哟 -u root 启动FastCGI进程。所以修改了源代码:

下载代码

wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.3.tar.gz

解压后进入目录,在文件src/spawn-fcgi.c中注释掉一段代码:
                        /*                                                                                                                                                       
                        if (my_uid == 0) {                                                                                                                                       
                                fprintf(stderr, "spawn-fcgi: I will not set uid to 0\n");                                                                                        
                                return -1;                                                                                                                                       
                        }                                                                                                                                                        
                        */

类似的还有两处要注释。
运行

./configure

然后编译

make 

修改链接:

rm /etc/alternatives/spawn-fcgi
ln -s /usr/src/spawn-fcgi-1.6.3/src/spawn-fcgi /etc/alternatives/spawn-fcgi

这样就解决了困扰我的redmine在root下运行经常crash的问题。

运行:
./spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u root -f /usr/share/nginx/tmp/fastcgi/

出错的时候请加-n参数前台显示

如
./spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u root -f /usr/share/nginx/tmp/fastcgi/ -n

提示:sh: exec: line 1: /usr/share/nginx/tmp/fastcgi/: Permission denied

 

你可能感兴趣的:(Linux库的编译)