nginx + fastcgi + c/c++
使用php写后端程序的例子很多,用c/c++的比较少。
本文采用nginx,spawn,fastcgi++来构建一个基于cgi的web程序。
由于fastcgi++依赖于boost库,我们先来装boost库
Linux下编译boost
1.编译前的准备工作
1
|
sudo
yum
install
bzip2
bzip2
-
devel
bzip2
-
libs
python
-
devel
-
y
|
2.下载安装包并解压
1
2
|
#wget http://netcologne.dl.sourceforge.net/project/boost/boost/1.61.0/boost_1_61_0.zip
unzip
boost_1_61_0
.
zip
|
编译安装
1
2
3
|
#cd boost_1_61_0
.
/
bootstrap
.
sh
sudo
.
/
b2
install
|
测试boost库是否可以使用,boost编译完成后运行程序报错,
1
2
3
4
5
6
7
8
|
/
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.下载源码并解压
1
2
|
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.下载编译所需的依赖项
1
2
3
|
cd
gcc
-
4.8.0
.
/
contrib
/
download
_prerequisites
cd
.
.
|
3.建立编译输出目录
1
|
mkdir
gcc
-
build
-
4.8.0
|
4.进入此目录,执行以下命令,生成makefile文件
1
2
|
cd
gcc
-
build
-
4.8.0
.
.
/
gcc
-
4.8.0
/
configure
--
enable
-
checking
=
release
--
enable
-
languages
=
c
,
c
++
--
disable
-
multilib
|
5.执行以下命令进行编译,编译时间比较长
1
|
make
-
j4
|
6.安装
1
|
sudo
make
install
|
安装完成后查看版本
1
2
3
4
5
6
7
8
|
#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的编译,编写一个例子测试是否成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#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
;
}
|
编译运行
1
2
|
g
++
-
o
main
testboost
.
cpp
-
L
/
usr
/
local
/
lib
-
lboost
_serialization
.
/
main
|
spawn-fcgi安装
1
2
3
4
|
wget
http
:
//download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz
.
/
configure
make
make
install
|
fastcgi安装
1
2
3
4
5
6
7
8
|
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程序
1
|
spawn
-
fcgi
-
a
172.27.18.181
-
p
8081
-
f
/
home
/
test
/
fastcgi
++
-
2.1
/
examples
/
utf8
-
helloworld
.
fcgi
|
如果启动报错,可以加参数-n来看具体原因。
1
|
spawn
-
fcgi
:
child
exited
with
:
1
|
我的报错如下,因为升级了gcc4.8,没有升级libstdc++.so.6导致。
/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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新的库
1
2
3
4
|
# 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程序
1
|
spawn
-
fcgi
-
a
192.168.18.11
-
p
8081
-
f
/
home
/
test
/
fastcgi
++
-
2.1
/
examples
/
utf8
-
helloworld
.
fcgi
-
n
|
程序会在前台运行
检查程序是否正常运行
1
2
|
# netstat -anp |grep 8081
tcp
0
0
192.168.18.11
:
8081
0.0.0.0
:
*
LISTEN
4704
/
utf8
-
helloworl
|
配置nginx,增加
1
2
3
4
5
6
|
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的配置
1
|
#nginx -s reload
|
打开浏览器,访问http://192.168.18.11/utf8-helloworld.fcgi,你会看到。
1
2
3
4
5
6
|
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
成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!