2010年12月25日 星期六

快速安裝Nginx+PHP5

因我是用Fedora13 linux, 故一開始裝好系統, 我會先update更新系統!
1. 更新系統
#yum -y update 

2. 安裝nginx 
#yum install nginx

 nginx預設設定檔

Default config file: /etc/nginx/nginx.conf
Default SSL config file: /etc/nginx/conf.d/ssl.conf
Default virtual hosting config file: /etc/nginx/conf.d/virtual.conf
Default documentroot: /usr/share/nginx/html

3. 安裝PHP5
 # yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

# yum install spawn-fcgi

4. 新增php_cgi啟用script
# vi /etc/init.d/php_cgi
內容如下
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via  spawn-fcgi
# chkconfig:   - 85 15
# description:  Run php-cgi as app server
# processname: php-cgi
# config:      /etc/sysconfig/phpfastcgi (defaults RH style)
# 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
spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
server_childs=5
pidfile="/var/run/php_cgi.pid"

# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi

start() {
    [ -x $php_cgi ] || exit 1
    [ -x $spawnfcgi ] || exit 2
    echo -n $"Starting $prog: "
    daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
    retval=$?
    echo
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} $prog -QUIT
    retval=$?
    echo
    [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
    return $retval
}

restart(){
        stop
        sleep 2
        start
}

rh_status(){
        status -p ${pidfile} $prog
}

case "$1" in
    start)
        start;;
    stop)
        stop;;
    restart)
        restart;;
    status)
        rh_status;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 3
esac

#chmod +x /etc/init.d/php_cgi

5. 啟用php-cgi
# /etc/init.d/php_cgi start
Starting php-cgi: spawn-fcgi: child spawned successfully: PID: 24094
                                                           [  OK  ]

[root@www src]# netstat -na |grep -w 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN



6. 編輯nginx.conf檔案
 # vi /etc/nginx/nginx.conf
內容如下
 server {
        listen       80;
        server_name  _;
...........                 //在server{  裡加入php,  以下省略
............
..............                   
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

7. 啟用nginx
#service nginx start

8. 測試
 # vi /usr/share/nginx/html/test.php
<?php
phpinfo();
?>
http://192.168.11.200/test.php

沒有留言:

張貼留言