nginx + fastcgi + c/c++環境的配置詳解

來源:互聯網
上載者:User

使用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 <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;                                                                                                 
 
}

編譯運行


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.