Install Required Packages
apt-get update & upgrade
apt-get install mysql-server nginx php5-cli php5-cgi spawn-fcgi nginx php5-cli php5-cgi
spawn-fcgi php5-gd php5-mysql
2 Create Directories For Your Site
mkdir -p /srv/www/yourdomain/public_html
mkdir /srv/www/yourdomain/logs
chown -R www-data:www-data /srv/www/yourdomain
3 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 /srv/www/yourdomain/logs/access.log; error_log /srv/www/yourdomain/logs/error.log; root /srv/www/yourdomain/public_html; location / { index 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 /srv/www/yourdomain/public_html$fastcgi_script_name; } } # rewrite server { server_name yourdomain.com; rewrite ^/(.*) http://www.$host/$1 permanent; }4 Create a file named /usr/bin/php-fastcgi with the following contents:
vi /usr/bin/php-fastcgi
注意:FASTCGI_USER=www-data, 在nginx的配置文件中user www-data;
#!/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
5 Make it executable by issuing the following command:
chmod +x /usr/bin/php-fastcgi
6 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
7 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
8 Test PHP With FastCGI
vi /srv/www/yourdomain/public_html/info.php <?php phpinfo(); ?>