wshq 发表于 2018-11-13 08:02:59

在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)

算是原创。转载请注明此地址,随着对NGINX认知的深入,此文会不定期更新或是修正。  以下兵分两路来说明:一是直接利用php-cgi的FastCGI运行方式;二是利用Lighttpd的spawn-fcgi来控制进程的运行方法。
  先说说利用php-cgi的FASTCGI运行方式:
  7、创建php-cgi启动脚本,
  # vi /etc/init.d/phpcgi
  #!/bin/sh
  #
  # php-cgi - this script starts and stops the php-cgi daemin
  #
  # chkconfig: - 85 15
  # description: Fast CGI php
  # processname: php-cgi
  # config: /etc/php.ini
  # pidfile: /var/run/php-cgi.pid
  # Source function library.
  . /etc/rc.d/init.d/functions
  # Source networking configuration.
  . /etc/sysconfig/network
  # Check that networking is up.
  [ "$NETWORKING" = "no" ] && exit 0
  phpcgi="/usr/bin/php-cgi"
  prog=$(basename ${phpcgi})
  FCGIPORT="8888"
  FCGIADDR="127.0.0.1"
  FCGIUSER="apache"
  FCGIGROUP="apache"
  PHP_FCGI_CHILDREN=5
  PHP_FCGI_MAX_REQUESTS=1000
  export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
  [ -e /etc/sysconfig/php-cgi ] && . /etc/sysconfig/php-cgi
  lockfile=/var/lock/subsys/php-cgi
  start() {
  echo -n $"Starting $prog: "
  /usr/bin/spawn-fcgi-a $FCGIADDR-p $FCGIPORT -C $PHP_FCGI_CHILDREN -u $FCGIUSER -g$FCGIGROUP -P /var/run/php-cgi.pid -f "${phpcgi}" >> /
  dev/null 2>&1
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $lockfile
  return $retval
  }
  stop() {
  echo -n $"Stopping $prog: "
  killproc $prog -QUIT
  retval=$?
  echo
  [ $retval -eq 0 ] && rm -f $lockfile
  return $retval
  }
  restart() {
  stop
  start
  }
  force_reload() {
  restart
  }
  fdr_status() {
  status $prog
  }
  case "$1" in
  start|stop|restart)
  $1
  ;;
  status)
  fdr_status
  ;;
  condrestart|try-restart)
  [ ! -f $lockfile ] || restart
  ;;
  *)
  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
  exit 2
  esac
  然后,开机自动运行:
  #/sbin/chmod +x /etc/init.d/phpcgi
  #/sbin/chkconfig --add phpcgi
  #/sbin/chkconfig --level 35 phpcgi on
  #/sbin/chkconfig --level 35 nginx on
  但从网上说会遇到两个问题,这里摘录一位的解决方案。(我没有遇到。也没有机会测试下面的解决方式是否正确)
  # cat /var/log/audit/audit.log| audit2allow -M local
  #/usr/sbin/semodule -i local.pp
  下面说说利用Lighttpd的spawn-fcgi来控制进程的运行的方法:
  8、开启nginx及利用Lighttpd的spawn-fcgi来控制进程的运行
  # spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f /usr/bin/php-cgi
  spawn-fcgi.c.187: child spawned successfully: PID: 2513
  参数含义如下
  -f指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
  -a绑定到地址addr
  -p绑定到端口port
  -s绑定到unix socket的路径path
  -C指定产生的FastCGI的进程数,默认为5(仅用于PHP)
  -P指定产生的进程的PID文件路径
  -u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等
  因为,安装rpm 安装nginx时。会创建nginx用户和组。
  # service nginx start
  Starting nginx:
  9、在IE栏里输入http://124.207.102.22/index.htm这时NGINX已在正常运行。如下图:
http://blog.51cto.com/rickyfang/../attachment/200901/200901111231645003740.jpg
  在/usr/share/nginx/html下面新建index.php
  
  在IE栏里输入http://124.207.102.22/index.php这时NGINX已在正常运行。如下图:
http://blog.51cto.com/rickyfang/../attachment/200901/200901111231645011438.jpg
  10、那如何实现php的运行呢。在第7或第8步骤中,已开启了PHP的进程:
  # ps -aux |grep php
  Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
  nginx   25130.01.9177204964 ?      Ss   20:45   0:00 /usr/bin/php-cgi
  nginx   25140.00.6177201656 ?      S    20:45   0:00 /usr/bin/php-cgi
  nginx   25150.00.6177201656 ?      S    20:45   0:00 /usr/bin/php-cgi
  nginx   25160.00.6177201656 ?      S    20:45   0:00 /usr/bin/php-cgi
  nginx   25170.00.6177201656 ?      S    20:45   0:00 /usr/bin/php-cgi
  nginx   25180.00.6177201656 ?      S    20:45   0:00 /usr/bin/php-cgi
  root      25420.00.2   3892   676 pts/0    R+   20:48   0:00 grep php
  可以看到,有五个进程正在运行。
  默认情况下。NGINX是可以开启静态页面,但如何开启PHP。还是要在/etc/nginx/nginx.php设置的。
  各位可以参考我的配置前后的截图(呵呵,研究下,有些参数是可以改变的。要举一返三吧):
  修改前:
http://blog.51cto.com/rickyfang/../attachment/200901/200901111231645027422.jpg
  修改后:
http://blog.51cto.com/rickyfang/../attachment/200901/200901111231645033901.jpg
  保存更改。
  然后service nginx restart便可了。
  11、配置虚拟主机
  在APACHE上配置虚拟主机。想来各位都有一定的体验。那如何在NGINX中实现呢?
  # vi /etc/nginx/nginx.conf
  参考下图(开启https的样例也在内)。最后几行:
  server
  {
  listen       8000;    ####监听端口
  server_name124.207.102.22aliasanother.alias;####域名
  root   /usr/share/nginx/html;                     ####路径
  indexindex.php index.html index.htm;    ####index
  location ~ \.php$
  {
  include   fastcgi.conf;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_indexindex.php;
  }
http://blog.51cto.com/rickyfang/../attachment/200901/200901111231645041835.jpg
  想多加虚机吗。呵呵,多来几个吧(日志选项请自位参考CONF文件自行研究)。
  基本上完成了。有些功能还需要参考官方文档深入研究学习下
  接下来,研究下rpm安装的情况下实现php连ms sql server.(tar包的已成功且在用啦)

页: [1]
查看完整版本: 在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)