nginx+fastcgi 交叉编译移植编译
解压包
tar zxvf nginx-1.8.1.tar.gz
tar zxvf pcre-8.35.tar.gz
tar zxvf zlib-1.2.8.tar.gz
tar zxvf openssl-1.0.1i.tar.gz
添加用户
groupadd -r nginx
useradd -r -g nginx nginx
编译安装
cd /opt/software/nginx-1.8.1
./configure \
--prefix=/opt/nginx \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--pid-path=/var/run/nginx.pid \
--with-pcre=/opt/software/pcre-8.35 \
--with-zlib=/opt/software/zlib-1.2.8 \
--with-openssl=/opt/software/openssl-1.0.1i
make
make install && echo OK
启动nginx
正确性检查
#每次修改nginx配置文件后都要进行检查
/opt/nginx/sbin/nginx -t
启动nginx
/opt/nginx/sbin/nginx
reload nginx
/opt/nginx/sbin/nginx -s reload
一键安装脚本
将以上步骤整合到一个脚本中来编译安装nginx
安装spawn-fcgi:
l 获取spawn-fcgi编译安装包,在http://redmine.lighttpd.net/projects/spawn-fcgi/wiki上可以获取当前最新的版本。
l 解压缩spawn-fcgi-x.x.x.tar.gz包。
l 进入解压缩目录,执行./configure。
l make & make install
l 获取fcgi编译安装包,在http://www.fastcgi.com/drupal/node/5上可以获取当前最新的版本。
l 解压缩fcgi-x.x.x.tar.gz包。
l 进入解压缩目录,执行./configure。
l make & make install
在/include/fcgio.h文件中加上 #include <cstdio>,然后再编译安装就通过了
如果提示找不到动态库,请在LD_LIBRARY_PATH或/etc/ld.so.conf中添加fcgi的安装路径,如/usr/local/lib,并执行ldconfig更新一下。
#include "fcgi_stdio.h"
#include <stdlib.h>
int main(void)
{
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++ main.cpp -o demo –lfcgi,并将demo部署到/opt/nginx-1.7.7/cgi-bin/目录
通过spawn-fcgi启动c/c++编写好的fastcgi程序:/opt/nginx-1.7.7/sbin/spawn-fcgi -a 127.0.0.1 -p 8081 -f /opt/nginx-1.7.7/cgi-bin/demo
3.1.3. nginx fastcgi配置
关于nginx的几个配置文件解析,可以参阅《Nginx安装与使用》http://www.cnblogs.com/skynet/p/4146083.html,在上篇的nginx.conf基础上增加下面的fastcgi配置。
这样nginx收到http://localhost/demo.cgi请求时,会匹配到location = /demo.cgi块,将请求传到后端的fastcgi应用程序处理。如下如所示:(注意其中number为80,是因为我请求了80次)