且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-24 21:23:48


   以下兵分两路来说明:一是直接利用php-cgi的FastCGI运行方式;二是利用Lighttpd的spawn-fcgi来控制进程的运行方法。

先说说利用php-cgi的FASTCGI运行方式:
7、创建php-cgi启动脚本,
[root@nginx-freetds ~]# 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

然后,开机自动运行:
[root@nginx-freetds ~]#/sbin/chmod +x /etc/init.d/phpcgi
[root@nginx-freetds ~]#/sbin/chkconfig --add phpcgi
[root@nginx-freetds ~]#/sbin/chkconfig --level 35 phpcgi on
[root@nginx-freetds ~]#/sbin/chkconfig --level 35 nginx on

但从网上说会遇到两个问题,这里摘录一位的解决方案。(我没有遇到。也没有机会测试下面的解决方式是否正确)
[root@nginx-freetds ~]# cat /var/log/audit/audit.log| audit2allow -M local
[root@nginx-freetds ~]#/usr/sbin/semodule -i local.pp

下面说说利用Lighttpd的spawn-fcgi来控制进程的运行的方法:
8、开启nginx及利用Lighttpd的spawn-fcgi来控制进程的运行
[root@nginx-freetds ~]# 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 <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置 
-a <addr> 绑定到地址addr 
-p <port> 绑定到端口port 
-s <path> 绑定到unix socket的路径path 
-C <childs> 指定产生的FastCGI的进程数,默认为5(仅用于PHP) 
-P <path> 指定产生的进程的PID文件路径 
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等


因为,安装rpm 安装nginx时。会创建nginx用户和组。

[root@nginx-freetds ~]# service nginx start
Starting nginx: [  OK  ]

9、在IE栏里输入[url]http://124.207.102.22/index.htm[/url]这时NGINX已在正常运行。如下图:
在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)

在/usr/share/nginx/html下面新建index.php
<?php
phpinfo();
?>
       
在IE栏里输入[url]http://124.207.102.22/index.php[/url]这时NGINX已在正常运行。如下图:
在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)

10、那如何实现php的运行呢。在第7或第8步骤中,已开启了PHP的进程:
[root@nginx-freetds ~]# ps -aux |grep php
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
nginx     2513  0.0  1.9  17720  4964 ?        Ss   20:45   0:00 /usr/bin/php-cgi
nginx     2514  0.0  0.6  17720  1656 ?        S    20:45   0:00 /usr/bin/php-cgi
nginx     2515  0.0  0.6  17720  1656 ?        S    20:45   0:00 /usr/bin/php-cgi
nginx     2516  0.0  0.6  17720  1656 ?        S    20:45   0:00 /usr/bin/php-cgi
nginx     2517  0.0  0.6  17720  1656 ?        S    20:45   0:00 /usr/bin/php-cgi
nginx     2518  0.0  0.6  17720  1656 ?        S    20:45   0:00 /usr/bin/php-cgi
root      2542  0.0  0.2   3892   676 pts/0    R+   20:48   0:00 grep php

可以看到,有五个进程正在运行。

    默认情况下。NGINX是可以开启静态页面,但如何开启PHP。还是要在/etc/nginx/nginx.php设置的。
    各位可以参考我的配置前后的截图(呵呵,研究下,有些参数是可以改变的。要举一返三吧):

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

修改后:
在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)
保存更改。

然后service nginx restart便可了。

11、配置虚拟主机
在APACHE上配置虚拟主机。想来各位都有一定的体验。那如何在NGINX中实现呢?

[root@nginx-freetds html]# vi /etc/nginx/nginx.conf
参考下图(开启https的样例也在内)。最后几行:
  server 
      {
        listen       8000;    ####监听端口
        server_name  124.207.102.22  alias  another.alias;####域名
        root   /usr/share/nginx/html;                     ####路径
        index  index.php index.html index.htm;    ####index

        location ~ \.php$
        {
             include   fastcgi.conf;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
         }

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

想多加虚机吗。呵呵,多来几个吧(日志选项请自位参考CONF文件自行研究)。

基本上完成了。有些功能还需要参考官方文档深入研究学习下

接下来,研究下rpm安装的情况下实现php连ms sql server.(tar包的已成功且在用啦)



 本文转自 rickyfang 51CTO博客,原文链接:http://blog.51cto.com/rickyfang/125841,如需转载请自行联系原作者