nginx web服务器是一个快速,轻量级的服务器,旨在有效地处理低流量和高流量网站的需求。 虽然通常用于提供静态内容,但它也能够处理动态页面。 本文说明如何在Ubuntu 12.04 LTS(Precise Pangolin)Linux服务器上安装 Nginx PHP FastCGI。
安装所需软件包
执行以下命令更新系统并安装nginx Web服务器,PHP和编译器工具:
- apt-get update
- apt-get upgrade
- apt-get install nginx php5-cli php5-cgi spawn-fcgi psmisc
配置虚拟主机
创建目录
在本指南中,域“example.com”用作示例网站。 您应该在后续的配置步骤中替换您自己的域名。 首先,创建保存内容和日志文件的目录:
- mkdir -p /srv/www/www.example.com/public_html
- mkdir /srv/www/www.example.com/logs
- chown -R www-data:www-data /srv/www/www.example.com
UNIX套接字配置示例
接下来,您需要定义网站的虚拟主机文件。 此示例使用UNIX套接字连接到fcgiwrap。 请务必将“example.com”的所有实例更改为您的域名。
/etc/nginx/sites-available/www.example.com
- server {
- server_name www.example.com example.com;
- access_log /srv/www/www.example.com/logs/access.log;
- error_log /srv/www/www.example.com/logs/error.log;
- root /srv/www/www.example.com/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/www.example.com/public_html$fastcgi_script_name;
- }
- }
创建一个名为/usr/bin/php-fastcgi的文件,包含以下内容:
/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=6
- PHP5=/usr/bin/php5-cgi
- /usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5
增加执行权限
- chmod +x /usr/bin/php-fastcgi
TCP套接字配置示例
或者,您可能希望使用TCP套接字。 如果是这样,请按如下示例修改nginx虚拟主机配置文件。 同样,请确保将“example.com”的所有实例替换为您的域名。
/etc/nginx/sites-available/www.example.com
- server {
- server_name www.example.com example.com;
- access_log /srv/www/www.example.com/logs/access.log;
- error_log /srv/www/www.example.com/logs/error.log;
- root /srv/www/www.example.com/public_html;
- location / {
- index index.html index.htm;
- }
- location ~ .php$ {
- include /etc/nginx/fastcgi_params;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
- }
- }
创建一个名为/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
增加执行权限:
- chmod +x /usr/bin/php-fastcgi
激活启动服务
执行如下命令激活网站
- cd /etc/nginx/sites-enabled/
- ln -s /etc/nginx/sites-available/www.example.com
创建一个名为/etc/init.d/php-fastcgi的文件,包含以下内容:
/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
启动php-fastcgi和nginx:
- chmod +x /etc/init.d/php-fastcgi
- update-rc.d php-fastcgi defaults
- /etc/init.d/php-fastcgi start
- /etc/init.d/nginx start