Deploy Nginx + Mysql + Spawn-Fcgi On Debian/ubuntu

毒药      1004阅读

Install Required Packages

apt-get update & upgrade
apt-get install mysql-server nginx php5-cli php5-cgi spawn-fcgi php5-gd php5-mysql php5-mysqlnd

Create Directories For Your Site

mkdir -p /var/www/iduyao
mkdir /srv/www/logs
chown -R www-data:www-data /var/www/iduyao

Configuration

I will use UNIX Sockets to run php.

vi /etc/nginx/sites-enabled/yourdomain

server {
    server_name www.yourdomain.com yourdomain.com;
    access_log /var/www/logs/induyao_access.log;
    error_log /var/www/logs/induyao_error.log;
    root /var/www/yourdomain;

    location / {
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/yourdomain$fastcgi_script_name;
    }
}

# rewrite
server {
    server_name yourdomain.com;
    rewrite ^/(.*) http://www.$host/$1 permanent;
}

Create a file named /usr/bin/php-fastcgi with the following contents:

vi /usr/bin/php-fastcgi

#!/bin/bash

FASTCGI_USER=www-data
FASTCGI_GROUP=www-data
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=4
PHP5=/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

Make it executable by issuing the following command:

chmod +x /usr/bin/php-fastcgi

Create a file named /etc/init.d/php-fastcgi with the following contents:

vi /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

   1004阅读

Start php-fastcgi and nginx by issuing the following commands:

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

Test PHP With FastCGI

vi /srv/www/yourdomain/public_html/info.php

<?php 
phpinfo(); 
?>

毒药   2015-06-29 13:09:52
1
登录 后发表评论