nginx + fastcgi + c/c++

管理员 Nginx来源:NGINX中文网1 16,8419字数 4056阅读13分31秒阅读模式

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

本文采用nginx,spawn,fastcgi++来构建一个基于cgi的web程序。
由于fastcgi++依赖于boost库,我们先来装boost库文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

Linux下编译boost

1.编译前的准备工作文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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

2.下载安装包并解压文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

#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编译完成后运行程序报错,文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

/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重新编译通过。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

redhat6.3升级gcc4.8.0编译安装方法:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

1.下载源码并解压文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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.下载编译所需的依赖项文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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

3.建立编译输出目录文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

mkdir gcc-build-4.8.0

4.进入此目录,执行以下命令,生成makefile文件文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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

5.执行以下命令进行编译,编译时间比较长文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

make -j4

6.安装文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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的编译,编写一个例子测试是否成功文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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

编译运行文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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程序文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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

如果启动报错,可以加参数-n来看具体原因。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

spawn-fcgi: child exited with:1

我的报错如下,因为升级了gcc4.8,没有升级libstdc++.so.6导致。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

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文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

连接到libstdc++.so.6新的库文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-fastcgi-cc/

# 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

打开浏览器,访问http://192.168.18.11/utf8-helloworld.fcgi,你会看到。

English: Hello World
Russian: Привет мир
Greek: Γεια σας κόσμο
Chinese: 世界您好
Japanese: 今日は世界
Runic English?: ᚺᛖᛚᛟ ᚹᛟᛉᛚᛞ

参考

http://www.cnblogs.com/skynet/p/4173450.html
http://www.cnblogs.com/wanghetao/p/3934350.html
http://www.nongnu.org/fastcgipp/doc/2.1/index.html

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
管理员
  • 版权声明 本文源自 NGINX中文网 整理 发表于 07/08/2016 10:06:55
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-fastcgi-cc/
评论  1  访客  1
    • 匿名
      匿名 9

      丰富内容

    评论已关闭!