Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418108
  • 博文数量: 48
  • 博客积分: 1032
  • 博客等级: 上士
  • 技术积分: 1256
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-19 13:24
文章分类

全部博文(48)

文章存档

2014年(3)

2013年(23)

2012年(22)

分类: 系统运维

2013-02-28 18:20:09

前面整理了下nginx和php之间两种不同的通讯方式。《nginx和php之间通讯方式之unix socket》

这次主要记录下挂载在/dev/shm下的socket方式

1./dev/shm介绍

<1>tmpfs是一个虚拟文件系统,它的存储空间都在内存中,系统默认挂载点/dev/shm

      认大小是RAM(内存)的一半。

<2>我们可以直接在/dev/shm下使用创建文件,而不用再mkfs格式化文件系统

      为都是在内存中操作。所以/dev/shm下的文件I/O速度相对很快

利用这点。我们可以把php.socket放在/dev/shm下加速工作效率

下面是nginx和php的编译安装步骤:

一。编译安装nginx

<1>安装nginx所需的pcre库  //服务器中如果已经安装,忽略此步骤

    tar zxvf pcre-8.10.tar.gz

    cd pcre-8.10/
    ./configure
    make && make install

<2>安装nginx

    tar zxvf nginx-0.8.46.tar.gz

    cd nginx-0.8.46/

    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

    make && make install

<3>修改nginx.conf

点击(此处)折叠或打开

  1. user www www;
  2. worker_processes 8;
  3. error_log /usr/local/nginx/logs/nginx_error.log crit;
  4. pid /usr/local/nginx/nginx.pid;
  5. #Specifies the value for maximum file descriptors that can be opened by this process.
  6. worker_rlimit_nofile 65535;

  7. events
  8. {
  9.   use epoll;
  10.   worker_connections 65535;
  11. }

  12. http
  13. {
  14.   include mime.types;
  15.   default_type application/octet-stream;

  16.   charset utf-8;
  17.       
  18.   server_names_hash_bucket_size 128;
  19.   client_header_buffer_size 32k;
  20.   large_client_header_buffers 4 32k;
  21.   client_max_body_size 8m;
  22.       
  23.   sendfile on;
  24.   tcp_nopush on;

  25.   keepalive_timeout 60;

  26.   tcp_nodelay on;

  27.   fastcgi_connect_timeout 300;
  28.   fastcgi_send_timeout 300;
  29.   fastcgi_read_timeout 300;
  30.   fastcgi_buffer_size 64k;
  31.   fastcgi_buffers 4 64k;
  32.   fastcgi_busy_buffers_size 128k;
  33.   fastcgi_temp_file_write_size 128k;

  34.   gzip on;
  35.   gzip_min_length 1k;
  36.   gzip_buffers 4 16k;
  37.   gzip_http_version 1.0;
  38.   gzip_comp_level 2;
  39.   gzip_types text/plain application/x-javascript text/css application/xml;
  40.   gzip_vary on;

  41.   #limit_zone crawler $binary_remote_addr 10m;

  42.   server
  43.   {
  44.     listen 80;
  45.     server_name zs.center.test.com
  46.     index index.html index.htm index.php;
  47.     root /data/nginx;

  48.     location ~ .*.(php|php5)?$
  49.     {
  50.       fastcgi_pass 127.0.0.1:9000;
  51.       fastcgi_index index.php;
  52.       include fcgi.conf;
  53.     }
  54.     log_format access_gm '$remote_addr - $remote_user [$time_local] "$request" '
  55.               '$status $body_bytes_sent "$http_referer" '
  56.               '"$http_user_agent" $http_x_forwarded_for';
  57.     access_log /usr/local/nginx/logs/gm.log access_gm;
  58.    }
  59. }
<4>添加修改/etc/init.d/下的nginx启动

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: - 85 15
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse
  7. # proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config: /etc/nginx/nginx.conf
  10. # config: /etc/sysconfig/nginx
  11. # pidfile: /var/run/nginx.pid

  12. # Source function library.
  13. . /etc/rc.d/init.d/functions

  14. # Source networking configuration.
  15. . /etc/sysconfig/network

  16. # Check that networking is up.
  17. [ "$NETWORKING" = "no" ] && exit 0

  18. #nginx="/usr/sbin/nginx"
  19. nginx="/usr/local/nginx-0.7/sbin/nginx"
  20. prog=$(basename $nginx)

  21. sysconfig="/etc/sysconfig/$prog"
  22. lockfile="/var/lock/subsys/nginx"
  23. #pidfile="/var/run/${prog}.pid"
  24. pidfile="/usr/local/nginx-0.7/logs/${prog}.pid"

  25. NGINX_CONF_FILE="/usr/local/nginx-0.7/conf/nginx.conf"

  26. [ -f $sysconfig ] && . $sysconfig


  27. start() {
  28.     [ -x $nginx ] || exit 5
  29.     [ -f $NGINX_CONF_FILE ] || exit 6
  30.     echo -n $"Starting $prog: "
  31.     daemon $nginx -c $NGINX_CONF_FILE
  32.     retval=$?
  33.     echo
  34.     [ $retval -eq 0 ] && touch $lockfile
  35.     return $retval
  36. }

  37. stop() {
  38.     echo -n $"Stopping $prog: "
  39.     killproc -p $pidfile $prog
  40.     retval=$?
  41.     echo
  42.     [ $retval -eq 0 ] && rm -f $lockfile
  43.     return $retval
  44. }

  45. restart() {
  46.     #configtest_q || return 6
  47.     stop
  48.         sleep 2
  49.     start
  50. }

  51. reload() {
  52.     configtest_q || return 6
  53.     echo -n $"Reloading $prog: "
  54.     killproc -p $pidfile $prog -HUP
  55.     echo
  56. }

  57. configtest() {
  58.     $nginx -t -c $NGINX_CONF_FILE
  59. }

  60. configtest_q() {
  61.     $nginx -t -q -c $NGINX_CONF_FILE
  62. }

  63. rh_status() {
  64.     status $prog
  65. }

  66. rh_status_q() {
  67.     rh_status >/dev/null 2>&1
  68. }

  69. # Upgrade the binary with no downtime.
  70. upgrade() {
  71.     local oldbin_pidfile="${pidfile}.oldbin"

  72.     configtest_q || return 6
  73.     echo -n $"Upgrading $prog: "
  74.     killproc -p $pidfile $prog -USR2
  75.     retval=$?
  76.     sleep 1
  77.     if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
  78.         killproc -p $oldbin_pidfile $prog -QUIT
  79.         success $"$prog online upgrade"
  80.         echo
  81.         return 0
  82.     else
  83.         failure $"$prog online upgrade"
  84.         echo
  85.         return 1
  86.     fi
  87. }

  88. # Tell nginx to reopen logs
  89. reopen_logs() {
  90.     configtest_q || return 6
  91.     echo -n $"Reopening $prog logs: "
  92.     killproc -p $pidfile $prog -USR1
  93.     retval=$?
  94.     echo
  95.     return $retval
  96. }

  97. case "$1" in
  98.     start)
  99.         rh_status_q && exit 0
  100.         $1
  101.         ;;
  102.     stop)
  103.         rh_status_q || exit 0
  104.         $1
  105.         ;;
  106.     restart|configtest|reopen_logs)
  107.         $1
  108.         ;;
  109.     force-reload|upgrade)
  110.         rh_status_q || exit 7
  111.         upgrade
  112.         ;;
  113.     reload)
  114.         rh_status_q || exit 7
  115.         $1
  116.         ;;
  117.     status|status_q)
  118.         rh_$1
  119.         ;;
  120.     condrestart|try-restart)
  121.         rh_status_q || exit 7
  122.         restart
  123.             ;;
  124.     *)
  125.         echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
  126.         exit 2
  127. esac

<5>启动 nginx

    /etc/init.d/nginx start   

    Starting nginx:                                            [  OK  ]

二。编译安装php(fastcgi,php-fpm) 

<1>检查安装所需要的库

     libiconv libmcrypt mhash mcrypt

<2>编译php  

    tar zxvf php-5.2.14.tar.gz

    gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1

    cd php-5.2.14/

    //根据自己的需求添加不同的功能插件

    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql /bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap

    make ZEND_EXTRA_LIBS='-liconv'
    make install
    cp php.ini-dist /usr/local/webserver/php/etc/php.ini

<3>安装php扩展模块,php加速器  //根据自己的需求

    tar jxvf eaccelerator-0.9.6.1.tar.bz2
    cd eaccelerator-0.9.6.1/
    /usr/local/webserver/php/bin/phpize
    ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
    make&&make install

<4>修改php.ini php-fpm.conf

    php-fpm.conf中主要修改:

    5

    www

    www

<5>启动php

    /usr/local/php/sbin/php-fpm restart

   Shutting down php_fpm . done
   Starting php_fpm  done

三。修改配置文件 使用/dev/shm/php.socket方式通讯

<1>修改nginx.conf

点击(此处)折叠或打开

  1. server
  2.     {
  3.     listen 8081;
  4.     server_name 61.147.69.165;
  5.     index index.php index.html;
  6.     root /home/lostpz/tools/html;
  7.     location ~ .*.(php|php5)?$
  8.     {
  9.     #fastcgi_pass 127.0.0.1:9000;
  10.     fastcgi_pass unix:/dev/shm/php.socket;
  11.     fastcgi_index index.php;
  12.     include fastcgi_params;
  13.     }
  14.     }
<2>修改php-fpm.conf

点击(此处)折叠或打开

  1. Address to accept fastcgi requests on.
  2. Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
  3. <value name="listen_address">/dev/shm/php.socket</valu>
<3>重启nginx 、php-fpm

   /etc/init.d/nginx reload     [  OK  ]

   /usr/local/php/etc/php-fpm restart    [  OK  ]
<4>此时可看到/dev/shm/目录下多了个php.socket文件

   修改此文件的属主,和nginx、php-cgi一致

   chown www. /dev/shm/php.socket

四。通过压力测试对比nginx和php-cgi的三种通讯方式:http、放在普通目录下的php.socket、放在/dev/shm/下的php.socket

    测试结果可通过下面的链接查看-->nginx和php之间通讯方式(压力测试性能比较)




阅读(2645) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~