etrace跟踪Nginx代码+ FASTCGI
http://blog.csdn.net/jianqiangchen/article/details/29175285
http://blog.csdn.net/jianqiangchen/article/details/29201415
http://blog.csdn.net/allenlinrui/article/details/19419721
nginx安装:
[root@monitor ~]# pwd
/root wget http://nginx.org/download/nginx-1.2.9.tar.gz tar -zxvf nginx-1.2..tar.gz
[root@slave1 nginx-1.2.]# pwd //configure
/root/nginx-1.2.
[root@slave1 nginx-1.2.]# ll
total
drwxr-xr-x Jun : auto
-rw-r--r-- May CHANGES
-rw-r--r-- May CHANGES.ru
drwxr-xr-x Jun : conf
-rwxr-xr-x May configure
drwxr-xr-x Jun : contrib
drwxr-xr-x Jun : html
-rw-r--r-- May LICENSE
drwxr-xr-x Jun : man
-rw-r--r-- May README
drwxr-xr-x Jun : src
[root@slave1 nginx-1.2.]# cd src //源代码目录
[root@slave1 src]# ll
total
drwxr-xr-x Jun : core
drwxr-xr-x Jun : event
drwxr-xr-x Jun : http
drwxr-xr-x Jun : mail
drwxr-xr-x Jun : misc
drwxr-xr-x May os
[root@slave1 src]# pwd
/root/nginx-1.2./src
Nginx的configure
1 ./configure -with-debug --with-cc-opt='-g -O0 -finstrument-functions'
[root@slave1 nginx-1.2.]# ll //产生objs目录 在objs产生真正Makefile文件
total
drwxr-xr-x Jun : auto
-rw-r--r-- May CHANGES
-rw-r--r-- May CHANGES.ru
drwxr-xr-x Jun : conf
-rwxr-xr-x May configure
drwxr-xr-x Jun : contrib
drwxr-xr-x Jun : html
-rw-r--r-- May LICENSE
-rw-r--r-- root root Jun : Makefile
drwxr-xr-x Jun : man
drwxr-xr-x root root Jun : objs
-rw-r--r-- May README
drwxr-xr-x Jun : src
2. 下载etrace
[root@slave1 ~]# pwd
/root
[root@slave1 ~]git clone https://github.com/elcritch/etrace.git
3. 将etrace代码加入Ngnix
将ptrace.c拷贝至Nginx源码core目录下.
core目录:
[root@slave1 core]# pwd
/root/nginx-1.2.9/src/core ptrace.c目录:
[root@slave1 src]# pwd
/root/etrace/src
[root@slave1 src]# ll
total
-rwxr-xr-x root root Jun : etrace
-rwxr-xr-x root root Jun : etrace.pl
-rw-r--r-- root root Jun : ptrace.c cp /root/etrace/src/ptreace.c /root/nginx-1.2.9/src/core/
cp /root/etrace/src/etrace /sbin/
cp /root/etrace/src/ptrace.pl /sbin/ 保证NGINX 与 etrace与etrace.pl在一个目录,目的是读取nginx产生的TRACE文件,然后转换为函数树
修改 /root/nginx-1.2.9/objs/Makefile
objs/nginx: objs/src/core/nginx.o \
objs/src/core/ptrace.o \ $(LINK) -o objs/nginx \
objs/src/core/nginx.o \
objs/src/core/ptrace.o \ //要加入的值 objs/src/core/nginx.o: $(CORE_DEPS) \
src/core/nginx.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/nginx.o \
src/core/nginx.c objs/src/core/ptrace.o: $(CORE_DEPS) \ //要加入值
src/core/ptrace.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/ptrace.o \
src/core/ptrace.c 注意:拷贝后注意每个大项的下面,要重新以TAB健开头,空格不等于TAB
3.make
4.make install
/usr/local/nginx
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx
/usr/local/nginx/html
/usr/local/nginx/html/index.html
/usr/local/nginx/html/50x.html
/usr/local/nginx/conf
/usr/local/nginx/conf/fastcgi_params.default
/usr/local/nginx/conf/mime.types
/usr/local/nginx/conf/scgi_params.default
/usr/local/nginx/conf/scgi_params
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/fastcgi.conf
/usr/local/nginx/conf/koi-win
/usr/local/nginx/conf/koi-utf
/usr/local/nginx/conf/nginx.conf.default
/usr/local/nginx/conf/mime.types.default
/usr/local/nginx/conf/uwsgi_params.default
/usr/local/nginx/conf/fastcgi_params
/usr/local/nginx/conf/uwsgi_params
/usr/local/nginx/conf/win-utf
/usr/local/nginx/conf/fastcgi.conf.default
/usr/local/nginx/logs
默认目录结构:
主目录:/usr/local/nginx/
配置目录:/usr/local/nginx/conf/
root目录:/usr/local/nginx/html/
可执行文件路径:/usr/local/nginx/sbin/
cp /usr/local/nginx/sbin/nginx /sbin/
5.启动 - 默认和特殊
/usr/local/nginx/sbin/nginx (默认启动方式)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (指定配置文件启动)
6.
vi /usr/local/nginx/conf/nginx.conf 加入 daemon off;
master_process off;
7.开3个窗口
窗口1
cd /sbin
touch TRACE
etrace nginx 或者 etrace nginx >log
窗口2
cd /sbin
./nginx
窗口三
curl 127.0.0.1
研究一下CGI: http://www.cnblogs.com/skynet/p/4173450.html root@monitor ~]# cat /usr/local/nginx/conf/nginx.conf worker_processes ; root : ? :: nginx: master process ./nginx nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
spawn_fastcgi的安装、部署与配置
wget https://github.com/lighttpd/spawn-fcgi/archive/v1.6.3.tar.gz
[root@monitor spawn-fcgi-1.6.3]# pwd
/root/spawn-fcgi-1.6.3 [root@monitor spawn-fcgi-1.6.3]# ./autogen.sh
[root@monitor spawn-fcgi-1.6.3]# ./configure
[root@monitor spawn-fcgi-1.6.3]# make
编译好以后,将可执行文件移动到nginx的sbin目录下
cp ./src/spawn-fcgi /usr/local/nginx/sbin/ //(cp到nginx的安装目录下)
安装fastcgi开发包: [root@monitor ~]# wget http://down1.chinaunix.net/distfiles/fcgi-2.4.0.tar.gz
[root@monitor ~]# tar -zxvf fcgi-2.4.0.tar.gz
[root@monitor fcgi-2.4.0]# pwd
/root/fcgi-2.4.0 1./configure 2.make 3.make install //安装了什么
/usr/bin/install -c -m 644 fastcgi.h /usr/local/include/fastcgi.h
/usr/bin/install -c -m 644 fcgi_stdio.h /usr/local/include/fcgi_stdio.h
/usr/bin/install -c -m 644 fcgiapp.h /usr/local/include/fcgiapp.h
/usr/bin/install -c -m 644 fcgimisc.h /usr/local/include/fcgimisc.h
/usr/bin/install -c -m 644 fcgio.h /usr/local/include/fcgio.h
/usr/bin/install -c -m 644 fcgios.h /usr/local/include/fcgios.h
/usr/bin/install -c -m 644 fcgi_config.h /usr/local/include/fcgi_config.h 安装fastcgi开发包,报以下错误: fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
make[]: *** [fcgio.lo] Error
make[]: Leaving directory `/root/downloads/fcgi-2.4.-SNAP-/libfcgi'
make[]: *** [all-recursive] Error
make[]: Leaving directory `/root/downloads/fcgi-2.4.-SNAP-'
make: *** [all] Error 解决办法:
在/include/fcgio.h文件中加上 #include <stdio.h>,然后再编译安装就通过了。 CGI程序:
[root@monitor ~]# vi demo.cc
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int count = 0;
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
""
"FastCGI Hello!"
"Request number %d running on host%s "
"Process ID: %d\n", ++count, getenv("SERVER_NAME"), getpid());
}
return 0;
} [root@monitor ~]# ./demo
./demo: error while loading shared libraries: libfcgi.so.0: cannot open shared object file: No such file or directory 解决:
1.cp /usr/local/lib/libfcgi.so.0 /usr/lib
在/etc/ld.so.conf中加入
include /usr/lib 2./sbin/ldconfig -v [root@monitor ~]# ldd demo
linux-vdso.so.1 => (0x00007fff0a4f1000)
libfcgi.so.0 => /usr/lib/libfcgi.so.0 (0x00007f560e1cf000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f560dec9000)
libm.so.6 => /lib64/libm.so.6 (0x0000003c4fc00000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f560dcb2000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c4ec00000)
libnsl.so.1 => /lib64/libnsl.so.1 (0x0000003c51400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c4e400000) mkdir -p /usr/local/nginx/cgibin/
cp /root/deme /usr/local/nginx/cgibin/demo 指定spawn-fcgi与 demo 这个CGI关联
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088
spawn-fcgi: no FastCGI application given [root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23692 参数含义如下:
-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的情况具体设置。
-a <addr> 绑定到地址addr。
-p <port> 绑定到端口port。
-s <path> 绑定到unix socket的路径path。
-C <childs> 指定产生的FastCGI的进程数,默认为5。(仅用于PHP)
-P <path> 指定产生的进程的PID文件路径。
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,CentOS下可以使用apache用户,其他的根据情况配置,如nobody、www-data等。 浏览器上:
http://120.24.160.93/demo.cgi (.cgi不能省) FastCGI Hello!Request number 4 running on host Process ID: 23692 服务器上:
[root@monitor cgibin]# ps -ef|grep demo
root 23692 1 0 15:46 ? 00:00:00 /usr/local/nginx/cgibin/demo
demo 这个CGI一致在运行 还可以监听其他端口:
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23700
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9001 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23702
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23704
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9003 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23706 ctive Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:9003 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9002 0.0.0.0:* LISTEN [root@monitor cgibin]# ps -ef|grep demo
root 23692 1 0 15:46 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23700 1 0 15:57 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23702 1 0 15:58 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23704 1 0 15:58 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23706 1 0 15:59 ? 00:00:00 /usr/local/nginx/cgibin/demo
重启nginx或者重新加载配置文件
重新加载配置文件
1.kill -HUP [pid]
2.重启nginx
service nginx restart
3.killall nginx ./nginx
kill -HUP 是让进程挂起,睡眠;
kill -9 六亲不认的杀掉
kill -15 正常的退出进程
killall命令用于杀死指定名字的进程(kill processes by name)
1. yum install php
PHP-CGI是PHP自带的FastCGI管理器
php-cgi -b 127.0.0.1:9000
php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启
直接杀死php-cgi进程,php就不能运行了。(PHP-FPM和Spawn-FCGI就没有这个问题,守护进程会平滑从新生成新的子进程。)
2.Spawn-FCGI,PHP-FPM 都是fastcgi管理器
yum install php-fpm
PHP-FPM是一个PHP FastCGI管理器 专门服务PHP
Spawn-FCGI是一个通用的FastCGI管理服务器 通用的
PHP-FPM、Spawn-FCGI都是守护php-cgi的进程管理器
php-fpm,Spawn-FCGI的管理对象是php-cgi
fastcgi是协议
php-cgi只是解释PHP脚本的程序 [root@monitor ~]# /usr/sbin/php-fpm
[09-Jun-2016 17:59:33] NOTICE: fpm is running, pid 23943
[09-Jun-2016 17:59:33] NOTICE: ready to handle connections [root@monitor ~]# ps -ef|grep php-fpm
root 23943 23848 0 17:59 pts/0 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 23944 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23945 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23946 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23947 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23948 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
root 23964 23900 0 18:01 pts/1 00:00:00 grep php-fpm
[root@monitor ~]# kill -9 23943
[root@monitor ~]# ps -ef|grep php-fpm
apache 23944 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23945 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23946 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23947 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23968 1 0 18:02 pts/0 00:00:00 php-fpm: pool www
root 23970 23900 0 18:02 pts/1 00:00:00 ps -ef spawn-fcgi管理php-cgi: [root@monitor ~]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 2000 -C 5 -f /usr/bin/php-cgi [root@monitor ~]# ps -ef|grep php-cgi
root 24007 1 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24008 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24009 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24010 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24011 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24012 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24017 23900 0 18:22 pts/1 00:00:00 grep php-cgi
[root@monitor ~]# kill -9 24008
[root@monitor ~]# ps -ef|grep php-cgi
root 24007 1 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24009 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24010 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24011 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24012 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24018 24007 0 18:23 ? 00:00:00 /usr/bin/php-cgi
root 24020 23900 0 18:23 pts/1 00:00:00 grep php-cgi [root@monitor ~]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -u nobody -g nobody -p 2010 -C 5 -f /usr/bin/php-cgi
ps -ef|grep nobody
nobody 24023 1 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24024 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24025 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24026 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24027 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24028 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
etrace跟踪Nginx代码+ FASTCGI的更多相关文章
- etrace 跟踪 nginx之HTTP请求流程
curl 127.0.0.1 | | | \--ngx_epoll_process_events | | | | \--ngx_time_update | | | | | \--ngx_gmtime ...
- Nginx中FastCGI配置优化
FastCGI: FastCGI是从CGI发展改进而来的.传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给HTTP服务器 ...
- Linux上配置Nginx+PHP5(FastCGI)
原为地址:http://www.laruence.com/2009/07/28/1030.html Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,以事件驱动的方式编写,所以有非常好的性能,同时 ...
- Nginx 中 FastCGI 配置示例
nginx 中 FastCGI 参数:主要是在 http 层 :保证PHP环境的高校运行 主要对PHP用来解析 fastcgi_cache_path /tmp/fastcgi_cache levels ...
- nginx+python+fastcgi环境配置(flup版本)
昨天花了一整天的时间研究搭建了nginx+python+fastcgi环境,并测试没问题,由于是第一次,并且参考了网上很多东西,网上也有很多,但还是把自己的过程记录下. 主要感谢这位兄弟的文章给了我很 ...
- nginx、fastCGI、php-fpm关系梳理(转)
前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装php-fpm扩展并启动php-fpm守护进程,nginx ...
- Nginx + CGI/FastCGI + C/Cpp
接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...
- nginx、fastCGI、php-fpm关系梳理(转载 http://blog.sina.com.cn/s/blog_6df9fbe30102v57y.html)
前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装 php-fpm扩展并启动php-fpm守护进程, ...
- 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
Nginx的好处相信我不必多说了,它作为一个相当轻量级的开源Web 服务器以及反向代理服务器而深受欢迎.越来越多的公司已经对它产生兴趣,包括我们公司的许多部门,利用它进行负载均衡和资源管理,之前写过一 ...
随机推荐
- chmod 命令
来源网址: http://www.2cto.com/os/201205/130236.html http://www.cnblogs.com/younes/archive/2009/11/20/160 ...
- ARM的STRB和LDRB指令分析
一.SDRAM 1.存储结构 SDRAM的内部是一个存储阵列.阵列就如同表格一样,将数据“填”进去.在数据读写时和表格的检索原理一样,先指定一个行(Row),再指定一个列 (Column),我们就可以 ...
- SQL脚本小笔记
--表添加字段.说明--- --脚本 alter table 表名 ADD 字段名 FLOAT(类型) NOT NULL Default 0(默认值) EXECUTE sp_addextendedpr ...
- Linux脚本
放置在/usr/local/sbin下面: 收到一个问题:"-bash XXX 没有权限":需要在此目录下执行chmod +x filename 收到一个问题:"-bas ...
- HTML文档模式与盒模型
HTML文档根据文档顶部的doctype声明来决定渲染模式,有标准模式(Standards Mode)与怪异模式(Quirks mode,或叫做混杂模式)两种模式. IE5及以前默认总是表现为怪异模式 ...
- HNOI2015滚粗记
HNOI2015滚粗记 经过两天的苦战,艰难的HNOI终于结束了.感觉这次HNOI自己还是收获了许多. \(Day1\)打的很是艰难,题目一下就有种晕头转向的感觉.开场\(20min\)自己还在读题时 ...
- Early 80386 CPUs
Assembling a detailed and accurate history of the 80386, including a complete listing of all the &qu ...
- [topcoder]BinarySearchable
http://community.topcoder.com/stat?c=problem_statement&pm=5869&rd=8078 这道题有点意思,思考理解后,就是找数组中的 ...
- duck type鸭子类型
在程序设计中,鸭子类型(英语:duck typing)是动态类型的一种风格.在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由当前方法和属性的集合决定.这个概念的名字来源于 ...
- BZOJ2375: 疯狂的涂色
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2375 小t非常喜爱画画,但是他还是一个初学者.他最近费尽千辛万苦才拜到已仙逝的达 芬奇为师(神 ...