nginx+fastcgi+c/cpp
参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/
跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多交流。。。
搭建环境
注意发现碰钉可以看看相应程序内的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
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)
#include "fcgi_stdio.h" #include <stdlib.h> int main(int argc, char* argv[]){ int count = 0; while (FCGI_Accept() >= 0){ printf("Content-type: text/html\r\n" "\r\n" "<title>FastCGI Hello!</title>" "<h1>FastCGI Hello!</h1>" "Request number %d running on host <i>%s</i>\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
折腾了一天终于搭建好了。之前只试过LNMP。
1. 还没用上数据库,有待补充
2. 对spawn-fcgi、fcgiwrap、fastcgi的关系不清晰,有待理解
3. 对信息传输交互没接触,有待构建