zoukankan      html  css  js  c++  java
  • 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.9.tar.gz
    [root@slave1 nginx-1.2.9]# pwd                             //configure
    /root/nginx-1.2.9
    [root@slave1 nginx-1.2.9]# ll
    total 572
    drwxr-xr-x 6 1001 1001   4096 Jun  9 13:05 auto
    -rw-r--r-- 1 1001 1001 216470 May 13  2013 CHANGES
    -rw-r--r-- 1 1001 1001 330113 May 13  2013 CHANGES.ru
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 conf
    -rwxr-xr-x 1 1001 1001   2369 May 13  2013 configure
    drwxr-xr-x 3 1001 1001   4096 Jun  9 13:05 contrib
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 html
    -rw-r--r-- 1 1001 1001   1397 May 13  2013 LICENSE
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 man
    -rw-r--r-- 1 1001 1001     49 May 13  2013 README
    drwxr-xr-x 8 1001 1001   4096 Jun  9 13:05 src             
    [root@slave1 nginx-1.2.9]# cd src                        //源代码目录
    [root@slave1 src]# ll
    total 24
    drwxr-xr-x 2 1001 1001 4096 Jun  9 13:05 core
    drwxr-xr-x 3 1001 1001 4096 Jun  9 13:05 event
    drwxr-xr-x 3 1001 1001 4096 Jun  9 13:05 http
    drwxr-xr-x 2 1001 1001 4096 Jun  9 13:05 mail
    drwxr-xr-x 2 1001 1001 4096 Jun  9 13:05 misc
    drwxr-xr-x 3 1001 1001 4096 May 13  2013 os
    [root@slave1 src]# pwd
    /root/nginx-1.2.9/src

    Nginx的configure

    1 ./configure -with-debug --with-cc-opt='-g -O0 -finstrument-functions'

    [root@slave1 nginx-1.2.9]# ll              //产生objs目录   在objs产生真正Makefile文件
    total 580
    drwxr-xr-x 6 1001 1001   4096 Jun  9 13:05 auto
    -rw-r--r-- 1 1001 1001 216470 May 13  2013 CHANGES
    -rw-r--r-- 1 1001 1001 330113 May 13  2013 CHANGES.ru
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 conf
    -rwxr-xr-x 1 1001 1001   2369 May 13  2013 configure
    drwxr-xr-x 3 1001 1001   4096 Jun  9 13:05 contrib
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 html
    -rw-r--r-- 1 1001 1001   1397 May 13  2013 LICENSE
    -rw-r--r-- 1 root root     46 Jun  9 13:11 Makefile
    drwxr-xr-x 2 1001 1001   4096 Jun  9 13:05 man
    drwxr-xr-x 2 root root   4096 Jun  9 13:11 objs
    -rw-r--r-- 1 1001 1001     49 May 13  2013 README
    drwxr-xr-x 8 1001 1001   4096 Jun  9 13:05 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 20 -rwxr-xr-x 1 root root 3183 Jun 9 13:16 etrace -rwxr-xr-x 1 root root 4134 Jun 9 13:16 etrace.pl -rw-r--r-- 1 root root 5054 Jun 9 13:16 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  8;
    
    root     16125     1  0 14:06 ?        00:00:00 nginx: master process ./nginx
    
    nobody   16126 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16127 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16128 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16129 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16130 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16131 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16132 16125  0 14:06 ?        00:00:00 nginx: worker process
    nobody   16133 16125  0 14:06 ?        00:00:00 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:50: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)': fcgio.cpp:70: error: 'EOF' was not declared in this scope fcgio.cpp:75: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()': fcgio.cpp:86: error: 'EOF' was not declared in this scope fcgio.cpp:87: error: 'EOF' was not declared in this scope fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()': fcgio.cpp:113: error: 'EOF' was not declared in this scope make[2]: *** [fcgio.lo] Error 1 make[2]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249/libfcgi' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249' make: *** [all] Error 2 解决办法: 在/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 "
    " "
    ""
    "FastCGI Hello!"
    "Request number %d running on host%s "
    "Process ID: %d ", ++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












  • 相关阅读:
    Linux系统编程——特殊进程之僵尸进程
    怎样让你的安卓手机瞬间变Firefox os 畅玩firefox os 应用
    新手学JavaScript都要学什么?
    《解读window核心编程》 之 注冊表
    指针知识梳理10-指向数组的指针
    简易迷宫游戏
    CC++控制台颜色设置类
    实用的开放平台整理
    java 集合交并补
    PHP 标准库 SPL 之数据结构栈(SplStack)简单实践
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5572199.html
Copyright © 2011-2022 走看看