[nginx 配置]nginx + fastcgi + c/c++环境的配置详解

更新时间:2020-10-20    来源:php安装    手机版     字体:

【www.bbyears.com--php安装】

使用php写后端程序的例子很多,用c/c++的比较少。

本文采用nginx,spawn,fastcgi++来构建一个基于cgi的web程序。
由于fastcgi++依赖于boost库,我们先来装boost库

Linux下编译boost

1.编译前的准备工作


sudo yum install  bzip2 bzip2-devel bzip2-libs python-devel -y

2.下载安装包并解压


#wget http://netcologne.dl.sourceforge.net/project/boost/boost/1.61.0/boost_1_61_0.zip
unzip boost_1_61_0.zip

编译安装


#cd boost_1_61_0
./bootstrap.sh
sudo ./b2 install

测试boost库是否可以使用,boost编译完成后运行程序报错,


/usr/bin/ld: warning: libboost_bzip2.so.1.61.0, needed by /usr/local/lib/libboost_iostreams.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzCompressInit"
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzCompress"
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzDecompress"
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzDecompressEnd"
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzDecompressInit"
/usr/local/lib/libboost_iostreams.so: undefined reference to `BZ2_bzCompressEnd"
collect2: ld returned 1 exit status

最开始以为是bzip2没装上,折腾了许久也没搞定,最后我发现boost的官方文档写着gcc4.4.7,而我本地的编译器是4.4.6,之后我把gcc升级到4.8重新编译通过。

redhat6.3升级gcc4.8.0编译安装方法:

1.下载源码并解压

wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.0/gcc-4.8.0.tar.bz2
tar -jxvf  gcc-4.8.0.tar.bz2

2.下载编译所需的依赖项

cd gcc-4.8.0 
./contrib/download_prerequisites 
cd ..


3.建立编译输出目录

mkdir gcc-build-4.8.0

4.进入此目录,执行以下命令,生成makefile文件


cd  gcc-build-4.8.0
../gcc-4.8.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

5.执行以下命令进行编译,编译时间比较长


make -j4


6.安装

sudo make install

安装完成后查看版本


#gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.8.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 4.8.0 (GCC)

重新执行boost的编译,编写一个例子测试是否成功


#include
#include
#include
#include
#include
#include
#include
#include
 
using namespace std;
 
using namespace boost::serialization;
using namespace boost::archive;
 
int main()
{
    vector v;
    for(int i=0;i!=12;i++)
    {  
        v.push_back(i);
    }  
 
    ofstream os("file",ios::out);
 
    text_oarchive oa(os);
    oa<     os.close();   
 
    ifstream is("file",ios::in);
    text_iarchive ia(is);
 
    vector vr;
    ia>>vr;
 
    is.close();
 
    for(size_t i=0;i!=vr.size();++i)                                                                          
    {
        cout<     }                                                                                                                                                                             
 
    return 0;                                                                                                 
 
}

编译运行


g++ -o main testboost.cpp -L/usr/local/lib -lboost_serialization
./main

spawn-fcgi安装

wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz
./configure
make
make install

fastcgi安装


wget http://ftp.twaren.net/Unix/NonGNU//fastcgipp/fastcgi++-2.1.tar.bz2
tar -xvjf fastcgi++-2.1.tar.bz2
cd fastcgi++-2.1
./configure
make
make install
cd example
make examples

启动fastcgi程序


spawn-fcgi -a 172.27.18.181 -p 8081 -f /home/test/fastcgi++-2.1/examples/utf8-helloworld.fcgi

如果启动报错,可以加参数-n来看具体原因。

spawn-fcgi: child exited with:1

我的报错如下,因为升级了gcc4.8,没有升级libstdc++.so.6导致。

/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15" not found错误的解决


strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_FORCE_NEW
GLIBCXX_DEBUG_MESSAGE_LENGTH

没有3.4.15

连接到libstdc++.so.6新的库


# cp /usr/local/lib64/libstdc++.so.6.0.18 /usr/lib64/
cd /usr/lib64/
rm -f libstdc++.so.6
ln -s libstdc++.so.6.0.18 libstdc++.so.6

再次启动fastcgi程序


spawn-fcgi -a 192.168.18.11 -p 8081 -f /home/test/fastcgi++-2.1/examples/utf8-helloworld.fcgi -n

程序会在前台运行
检查程序是否正常运行


# netstat -anp |grep 8081
tcp        0      0 192.168.18.11:8081          0.0.0.0:*                   LISTEN      4704/utf8-helloworl


配置nginx,增加


        location ~ .fcgi$ {
            fastcgi_pass   192.168.18.11:8081;
            fastcgi_index  index.cgi;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

重新加载ngnx的配置


#nginx -s reload
1
#nginx -s reload
打开浏览器,访问http://192.168.18.11/utf8-helloworld.fcgi,你会看到。
English: Hello World

本文来源:http://www.bbyears.com/jiaocheng/105789.html

热门标签

更多>>

本类排行