ubuntu12.04+ngnix+php-fastcgi+mysql+memcached网站开发测试环境搭建

1. 安装必要的软件

sudo apt-get install nginx php5-cli php5-cgi spawn-fcgi psmisc mysql-server \
mysql-client php5-mysql memcached php5-memcache php5-curl php5-gd

2. 打开浏览器访问: 127.0.0.1。就能看到表示nginx安装成功的页面。但这时访问php文件页面还不行。 (注:ngnix的配置文件目录为/etc/ngnix/,默认网站根目录为/usr/share/ngnix/www/;php配置文件目录为/etc/php5/)

3. 编辑文件/etc/ngnix/sites-available/default,内容如下(可以先把原文件备份一下):

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    server_name localhost;

    location / {

        index   index.html index.htm;
    }

    location ~ \.php$ {

        fastcgi_pass    localhost:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
        try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;
}

4. 修改文件/etc/php5/cgi/php.ini。去除

#cgi.fix_pathinfo=1

一行前面的#。具体信息请看php.ini的相关注释。

5. 创建文件/usr/bin/php-fastcgi :

#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data

ADDRESS=127.0.0.1
PORT=9000

PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6

PHP5=/usr/bin/php5-cgi
/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

6. 设置/usr/bin/php-fastcgi为可执行:

sudo chmod +x /usr/bin/php-fastcgi

7. 创建文件/etc/init.d/php-fastcgi:

#!/bin/bash

PHP_SCRIPT=/usr/bin/php-fastcgi

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data

PID_DIR=/var/run/php-fastcgi
PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
RET_VAL=0

case "$1" in
    start)
        if [[ ! -d $PID_DIR ]]
        then
            mkdir $PID_DIR
            chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
            chmod 0770 $PID_DIR
        fi
        if [[ -r $PID_FILE ]]
        then
            echo "php-fastcgi already running with PID `cat $PID_FILE`"
            RET_VAL=1
        else
            $PHP_SCRIPT
            RET_VAL=$?
        fi
    ;;
    stop)
        if [[ -r $PID_FILE ]]
        then
            kill `cat $PID_FILE`
            rm $PID_FILE
            RET_VAL=$?
        else
            echo "Could not find PID file $PID_FILE"
            RET_VAL=1
        fi
    ;;
    restart)
        if [[ -r $PID_FILE ]]
        then
            kill `cat $PID_FILE`
            rm $PID_FILE
            RET_VAL=$?
        else
            echo "Could not find PID file $PID_FILE"
        fi
        $PHP_SCRIPT
        RET_VAL=$?
    ;;
    status)
        if [[ -r $PID_FILE ]]
        then
            echo "php-fastcgi running with PID `cat $PID_FILE`"
            RET_VAL=$?
        else
            echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
        fi
    ;;
    *)
        echo "Usage: php-fastcgi {start|stop|restart|status}"
        RET_VAL=1
    ;;
esac
exit $RET_VAL

8. 添加可执行权限,以及设置为开机启动

sudo chmod +x /etc/init.d/php-fastcgi
sudo update-rc.d php-fastcgi defaults
sudo /etc/init.d/php-fastcgi start
sudo /etc/init.d/nginx start

9. 创建文件/usr/share/nginx/www/test.php, 内容如下:

<?php
    phpinfo();
?>

10. 打开浏览器访问127.0.0.1/test.php,就能看到关于php信息的表格页面了。

参考资料