Ubuntu 12.04 (Precise Pangolin)安装Nginx PHP FastCGI

nginx web服务器是一个快速,轻量级的服务器,旨在有效地处理低流量和高流量网站的需求。 虽然通常用于提供静态内容,但它也能够处理动态页面。 本文说明如何在Ubuntu 12.04 LTS(Precise Pangolin)Linux服务器上安装 Nginx PHP FastCGI。

安装所需软件包



执行以下命令更新系统并安装nginx Web服务器,PHP和编译器工具:

  1. apt-get update
  2. apt-get upgrade
  3. apt-get install nginx php5-cli php5-cgi spawn-fcgi psmisc

配置虚拟主机


创建目录

在本指南中,域“example.com”用作示例网站。 您应该在后续的配置步骤中替换您自己的域名。 首先,创建保存内容和日志文件的目录:

  1. mkdir -p /srv/www/www.example.com/public_html
  2. mkdir /srv/www/www.example.com/logs
  3. chown -R www-data:www-data /srv/www/www.example.com

UNIX套接字配置示例

接下来,您需要定义网站的虚拟主机文件。 此示例使用UNIX套接字连接到fcgiwrap。 请务必将“example.com”的所有实例更改为您的域名。
/etc/nginx/sites-available/www.example.com

  1. server {
  2.     server_name www.example.com example.com;
  3.     access_log /srv/www/www.example.com/logs/access.log;
  4.     error_log /srv/www/www.example.com/logs/error.log;
  5.     root /srv/www/www.example.com/public_html;
  6.  
  7.     location / {
  8.         index  index.html index.htm;
  9.     }
  10.  
  11.     location ~ \.php$ {
  12.         include /etc/nginx/fastcgi_params;
  13.         fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
  14.         fastcgi_index index.php;
  15.         fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
  16.     }
  17. }

创建一个名为/usr/bin/php-fastcgi的文件,包含以下内容:

/usr/bin/php-fastcgi

  1. #!/bin/bash
  2.  
  3. FASTCGI_USER=www-data
  4. FASTCGI_GROUP=www-data
  5. SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
  6. PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
  7. CHILDREN=6
  8. PHP5=/usr/bin/php5-cgi
  9.  
  10. /usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

增加执行权限

  1. chmod +x /usr/bin/php-fastcgi

TCP套接字配置示例

或者,您可能希望使用TCP套接字。 如果是这样,请按如下示例修改nginx虚拟主机配置文件。 同样,请确保将“example.com”的所有实例替换为您的域名。
/etc/nginx/sites-available/www.example.com

  1. server {
  2.     server_name www.example.com example.com;
  3.     access_log /srv/www/www.example.com/logs/access.log;
  4.     error_log /srv/www/www.example.com/logs/error.log;
  5.     root /srv/www/www.example.com/public_html;
  6.  
  7.     location / {
  8.         index  index.html index.htm;
  9.     }
  10.  
  11.     location ~ \.php$ {
  12.         include /etc/nginx/fastcgi_params;
  13.         fastcgi_pass 127.0.0.1:9000;
  14.         fastcgi_index index.php;
  15.         fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
  16.     }
  17. }

创建一个名为/usr/bin/php-fastcgi的文件,包含以下内容:

  1. #!/bin/bash
  2.  
  3. FASTCGI_USER=www-data
  4. FASTCGI_GROUP=www-data
  5. ADDRESS=127.0.0.1
  6. PORT=9000
  7. PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
  8. CHILDREN=6
  9. PHP5=/usr/bin/php5-cgi
  10.  
  11. /usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

增加执行权限:

  1. chmod +x /usr/bin/php-fastcgi

激活启动服务

执行如下命令激活网站

  1. cd /etc/nginx/sites-enabled/
  2. ln -s /etc/nginx/sites-available/www.example.com

创建一个名为/etc/init.d/php-fastcgi的文件,包含以下内容:
/etc/init.d/php-fastcgi:

  1. #!/bin/bash
  2.  
  3. PHP_SCRIPT=/usr/bin/php-fastcgi
  4. FASTCGI_USER=www-data
  5. FASTCGI_GROUP=www-data
  6. PID_DIR=/var/run/php-fastcgi
  7. PID_FILE=/var/run/php-fastcgi/php-fastcgi.pid
  8. RET_VAL=0
  9.  
  10. case "$1" in
  11.     start)
  12.       if [[ ! -d $PID_DIR ]]
  13.       then
  14.         mkdir $PID_DIR
  15.         chown $FASTCGI_USER:$FASTCGI_GROUP $PID_DIR
  16.         chmod 0770 $PID_DIR
  17.       fi
  18.       if [[ -r $PID_FILE ]]
  19.       then
  20.         echo "php-fastcgi already running with PID `cat $PID_FILE`"
  21.         RET_VAL=1
  22.       else
  23.         $PHP_SCRIPT
  24.         RET_VAL=$?
  25.       fi
  26.   ;;
  27.     stop)
  28.       if [[ -r $PID_FILE ]]
  29.       then
  30.         kill `cat $PID_FILE`
  31.         rm $PID_FILE
  32.         RET_VAL=$?
  33.       else
  34.         echo "Could not find PID file $PID_FILE"
  35.         RET_VAL=1
  36.       fi
  37.   ;;
  38.     restart)
  39.       if [[ -r $PID_FILE ]]
  40.       then
  41.         kill `cat $PID_FILE`
  42.         rm $PID_FILE
  43.         RET_VAL=$?
  44.       else
  45.         echo "Could not find PID file $PID_FILE"
  46.       fi
  47.       $PHP_SCRIPT
  48.       RET_VAL=$?
  49.   ;;
  50.     status)
  51.       if [[ -r $PID_FILE ]]
  52.       then
  53.         echo "php-fastcgi running with PID `cat $PID_FILE`"
  54.         RET_VAL=$?
  55.       else
  56.         echo "Could not find PID file $PID_FILE, php-fastcgi does not appear to be running"
  57.       fi
  58.   ;;
  59.     *)
  60.       echo "Usage: php-fastcgi {start|stop|restart|status}"
  61.       RET_VAL=1
  62.   ;;
  63. esac
  64. exit $RET_VAL

启动php-fastcgi和nginx:

  1. chmod +x /etc/init.d/php-fastcgi
  2. update-rc.d php-fastcgi defaults
  3. /etc/init.d/php-fastcgi start
  4. /etc/init.d/nginx start
标签:UbuntuPHPNginx 发布于:2019-11-21 14:06:56