设为首页 收藏本站
查看: 743|回复: 0

[经验分享] nginx学习总结四(nginx+php-cgi)

[复制链接]

尚未签到

发表于 2018-11-8 09:43:09 | 显示全部楼层 |阅读模式
  
一、
Nginx环境

  前面说了nginx的优点:
  服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数才500M。

二、安装前准备

1、安装GD库
  安装freeytpe
  # tar zxvf freetype-2.4.3.tar.gz -C  /usr/local/src/freetype-2.4.3
  # cd  /usr/local/src/freetype-2.4.3
  # ./configure --prefix=/usr/local/freetype-2.4.3
  # make
  # make install
  安装libpng
   #tar -zxvf libpng-1.4.4.tar.gz -C /usr/local/src/
  # cd /usr/local/src/libpng-1.4.4
  #./configure --prefix=/usr/local/libpng-1.4.4
  # make
  # make install
  

  安装jpeg
  # tar -zxvf jpegsrc.v8b.tar.gz -C /usr/local/src/
  # cd /usr/local/src/jpeg-8b/
  # ./configure --prefix=/usr/local/jpeg-8b
  # make
  # make install
  # ./configure --prefix=/usr/local/jpeg --enable-shared --enable-static
  注:在RHEL5系统64 位下编译jpeg-6b时候报以下错误
  make: ./libtool: Command not found make: *** [jcapimin.lo] Error 127
  错误分析:由于libtool版本过低导致的,进入jpeg-6b的源码目录,复制相关文件覆盖,然后重新configure。
  cp /usr/share/libtool/config.sub .
  cp /usr/share/libtool/config.guess .
  安装GD
  最好重新加载下lib  #ldcongfig
  查询下 #ldcongfig -p | grep jpeg
        #ldcongfig -p | grep free
        #ldcongfig -p | grep libpng
  # tar zxvf gd-2.0.35.tar.gz -C /usr/local/src
  # cd /usr/local/src/gd-2.0.35
  # ./configure --prefix=/usr/local/gd-2.0.33 --with-jpeg=/usr/local/jpeg-8b --with-freetype=/usr/local/freetype-2.4.3 --with-png=/usr/local/libpng-1.4.4/ --with-zlib --enable-m4_pattern_allow
  #make  &&  make install
  make[2]: *** [gd_png.lo] 错误 1
  
make[2]: Leaving directory `/usr/local/src/gd-2.0.33'
  
make[1]: *** [all-recursive] 错误 1
  
make[1]: Leaving directory `/usr/local/src/gd-2.0.33'
  
make: *** [all] 错误 2
  
解决办法
  
vi gd_png.c
  
找到“png.h”改成“/usr/local/libpng-1.4.4/include/png.h”
  问题1:
  编译最新的2.0.35版本,make的时候出现以下错误:
  configure.ac:64: error: possibly undefined macro: AM_ICONV
  If this token and others are legitimate, please use m4_pattern_allow.
  See the Autoconf documentation.
  make: *** [configure] Error 1
  解决办法: configure的时候加上--enable-m4_pattern_allow参数;便能顺利编译安装
  问题2:
  aclocal:configure.ac:64: warning: macro `AM_ICONV' not found in library
  解决:没有安装gettext软件包
  gettext-0.14.6-4.el5、gettext-devel-0.14.6-4.el5
  

  2、安装其他组件
  安装 libiconv
  #iconv的功能可算强大,可以对文本进行编码间的转换,它支持世界上很多的语言,当然我们主要用它来处理中文各种编码之间的转换,libiconv是一个开源的开发包,使用它可以非常方便的开发出适合自己用的编码转换工具。在这里http://gnuwin32.sourceforge.net/packages/libiconv.htm可以下载到libiconv的文档和开发包。
  iconv本身linux系统自带,检查是否有/usr/bin/iconv 及 /usr/include/iconv.h 文件
  安装 libmcrypt
  #phpmyadmin基本上都安装完成了之后,出现了一个错误信息无法载入mcrypt 扩展
  #Mcrypt扩展库依赖的包
  # tar zxvf libmcrypt-2.5.8.tar.gz
  # cd libmcrypt-2.5.8/
  # ./configure
  # make && make install
  # /sbin/ldconfig
  # cd libltdl/
  # ./configure --enable-ltdl-install
  # make && make install
  安装mhash
  #Mcrypt扩展库依赖的包
  # tar zxvf mhash-0.9.9.9.tar.gz
  # cd mhash-0.9.9.9/
  # ./configure
  # make
  # make install
  安装mcrypt  
  #其中,Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。PHP加密扩展库Mcrypt的算法和加密模式
  # tar zxvf mcrypt-2.6.8.tar.gz
  # cd mcrypt-2.6.8
  # LD_LIBRARY_PATH=/usr/local/lib ./configure --with-libiconv-prefix
  # make
  # make install
  注:configure时候出现如下错误:
  configure: error: "You need at least libmhash 0.8.15 to compile this program http://mhash.sf.net/"
  前面已经安装过mhash,初步分析为预编译的时候没找到lib文件所在的地址;
  所以在./configure前面加上LD_LIBRARY_PATH=/usr/local/lib
  安装 libevent
  # tar -xzvf libevent-1.4.9-stable.tar.gz
  # cd /usr/local/libevent-1.4.9-stable/
  # ./configure --prefix=/usr/local/libevent
  # make
  # make install
  #libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名分布式缓存软件memcached也是libevent based,,编译库代码,编译脚本会判断OS支持哪种类型的事件机制(select、epoll或kqueue),然后条件编译相应代码,供上层使用的接口仍然是保持统一的
  安装pcre
  #PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。测试了一下,同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题
  
  # tar zxvf pcre-7.9.tar.gz
  # cd pcre-7.9/
  # ./configure
  # make && make install

三、Nginx+PHP安装

1、PHP-CGI安装及配置
  

  
yum install libxml2-devel
  

  # tar -zxvf php-5.2.10.tar.gz -C /usr/local/src
  # cp /home/houzc/php-5.2.10-fpm-0.5.10.diff.gz /usr/local/src
  #cd /usr/local/src
  

  # gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
  注:将php-fpm解压到php-5.2.10
  # cd /usr/local/src/php-5.2.10
  # ./configure
  
  --prefix=/usr/local/php-5.2.10 --with-mysql=/usr/local/mysql/ --with-freetype-dir=/usr/local/freetype-2.4.3  --with-jpeg-dir=/usr/local/jpeg-8b  --with-png-dir=/usr/local/libpng-1.4.4/ --with-zlib --with-libxml-dir --enable-xml --enable-fastcgi --enable-fpm  --enable-force-cgi-redirect --enable-mbstring //
  --with-gd=/usr/local/gd-2.0.33  --with-openssl
  # make && make install
  

  配置php-cgi
  # vi /usr/local/php-5.2.10/etc/php-fpm.conf
  Equivalent to Apache MaxClients directive.
    Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
    Used with any pm_style.
    500
  //修改php进程的最大数为500进程
  Unix user of processes
    www
  Unix group of processes
    www
  //守护进程的用户
  启动php-cgi
  # echo “ulimit -SHn 65535” >> /etc/rc.loal
  # /usr/local/php-5.2.10/sbin/php-fpm start
  注:php-cgi进程,监听127.0.0.1的9000端口,进程数为500
  (如果服务器内存小于3GB,可以只开启64个进程),用户为www(上面的配置)
  php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload
  


2、NGINX安装与完整配置
  注:需要先安装pcrc,见上面的安装过程
  安装nginx
  # tar zxvf nginx-0.7.65 .tar.gz
  # cd nginx-0.7.65
  # ./configure --prefix=/usr/local/nginx-0.7.65 --with-http_stub_status_module --with-http_ssl_module
  //--with-http_stub_status_module:添加nginx监控参数
  # make && make install
    user  www www;
  worker_processes 30;
  worker_cpu_affinity 00000001 00000001 00000001 00000001 00000010 00000010 00000010 00000010 00000100 00000100 00000100 00000100 00001000 00001000 00001000 00001000 00010000 00010000 00010000 00010000 00100000 00100000 00100000 00100000 01000000 01000000 01000000 01000000 10000000 10000000;
  
  error_log logs/error.log;
  pid nginx.pid;
  worker_rlimit_nofile 65535;
  events {
     use epoll;
     worker_connections  65535;
  }
  http {
  include mime.types;
  default_type application/octet-stream;
     include /usr/local/nginx-0.7.65/conf/fastcgi_params;
  keepalive_timeout  
  50;
  

  

  sendfile on;
  tcp_nopush on;
  server_names_hash_bucket_size 128;
  
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 8k;
  gzip_http_version 1.1;
  gzip_types       text/javascript text/plain application/x-javascript text/css application/xml;
  
  server {
         listen  80;
         server_name   img.xywy.com
         root    html;
  
         location / {
             index index.htm index.php index.html;
             root /www/img.xywy.com;
         }  
  
  error_page   404   /404.php;   
         error_page   403   /404.php;
  
         location ~ \.php$ {
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME /www/img.xywy.com$fastcgi_script_name;
             include fastcgi_params;
         }
  
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
         {
             expires    30d; //使用户浏览器缓存图片格式文件30天
          }
         location ~ .*\.(js|css)$
         {
            expries 1h; //使用户浏览器缓存js格式文件1小时
         }
     }
  
  server
     {
         listen  80;
         server_name  status.206.xywy.com;
  
         location / {
             stub_status on;
             access_log   off;
         }
     
  }
  


启动nginx
  # /usr/local/ nginx-0.7.65/sbin/nginx
  
  当修改了nginx.conf配置,想测试一下是否正确,可以用下面的命令
  # /usr/local/ nginx-0.7.65/sbin/nginx -t
  the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  configuration file /usr/local/nginx/conf/nginx.conf test is successful
  
  停止nginx
  # kill -TERM `cat /usr/local/ nginx-0.7.65/logs/nginx.pid`
  
  

  

  Nginx启动脚本
  # cat /usr/local/ nginx-0.7.65/ngxin.sh
  
  #!/bin/sh
  ### BEGIN INIT INFO
  # Provides:          Nginx-php-fpm(fastcgi)
  # Required-Start:    $all
  # Required-Stop:     $all
  # Default-Start:     3 5
  # Default-Stop:      0 1 6
  # Short-Description: Start and stop nginx-fcgi in external FASTCGI mode
  # Description:       Start and stop nginx-fcgi in external FASTCGI mode
  # http://www.linxutone.org msn:cnseek@msn.com
  ### END INIT INFO
  set -e
  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  DESC="nginx daemon"
  NAME=nginx
  DAEMON=/usr/local/nginx-0.7.65/sbin/$NAME
  CONFIGFILE=/usr/local/nginx-0.7.65/conf/nginx.conf
  FPM_PATH=/usr/local/php-5.2.10/sbin/php-fpm
  PIDFILE=/usr/local/nginx-0.7.65/logs/$NAME.pid
  # Gracefully exit if the package has been removed.
  test -x $DAEMON || exit 0
  
  d_start() {
   $FPM_PATH start > /dev/null 2>&1
   $DAEMON -c $CONFIGFILE || echo -n " already running"
  }
  d_stop() {
   $FPM_PATH stop > /dev/null 2>&1
   kill -QUIT `cat $PIDFILE` || echo -n " not running"
  }
  d_reload() {
   $FPM_PATH reload > /dev/null 2>&1
   kill -HUP `cat $PIDFILE` || echo -n " can't reload"
  }
  case "$1" in
   start)
         echo -n "Starting $DESC: $NAME"
         d_start
         echo "."
         ;;
   stop)
         echo -n "Stopping $DESC: $NAME"
         d_stop
         echo "."
         ;;
   reload)
         echo -n "Reloading $DESC configuration ..."
         d_reload
         echo "reloaded."
   ;;
   restart)
         echo -n "Restarting $DESC: $NAME"
         d_stop
         sleep 1
         d_start
         echo "."
         ;;
   *)
           echo "Usage: $NAME {start|stop|restart|reload}" >&2
           exit 3
         ;;
  esac
  exit 0
  

  

  写个
  
  test就可以了。
  

DSC0000.jpg

  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-632251-1-1.html 上篇帖子: 【Nginx】02、nginx常用的配置指令详解 下篇帖子: Nginx实战基础篇二 Nginx主配置文件参数详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表