ubuntu10.04 nginx php-fastcgi 配置

设置主机名

在安装之前先设置主机名hostname

hostname -f

安装需要的软件包

apt-get update
apt-get upgrade
apt-get install openssh-server nginx mysql-client mysql-server imagemagick php5 php5-cgi php5-cli php5-common spawn-fcgi php5-gd php5-imagick php5-imap php5-mysql
apt-get install php-auth php-pear php5-curl php5-idn php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl mcrypt

设置虚拟主机

创建文件夹和修改属性

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 Sockets 配置

File:/etc/nginx/sites-available/www.example.com

<span style="color: #008000; font-weight: bold;">server</span> {
    <span style="color: #008000; font-weight: bold;">server_name</span> <span style="color: #ba2121;">www.example.com</span> <span style="color: #ba2121;">example.com</span>;
    <span style="color: #008000; font-weight: bold;">access_log</span> <span style="color: #ba2121;">/srv/www/www.example.com/logs/access.log</span>;
    <span style="color: #008000; font-weight: bold;">error_log</span> <span style="color: #ba2121;">/srv/www/www.example.com/logs/error.log</span>;
    <span style="color: #008000; font-weight: bold;">root</span> <span style="color: #ba2121;">/srv/www/www.example.com/public_html</span>;

    <span style="color: #008000; font-weight: bold;">location</span> <span style="color: #ba2121;">/</span> {
        <span style="color: #008000; font-weight: bold;">index</span>  <span style="color: #ba2121;">index.html</span> <span style="color: #ba2121;">index.htm</span>;
    }

    <span style="color: #008000; font-weight: bold;">location</span> ~ <span style="color: #bb6688;">\.php$</span> {
        <span style="color: #008000; font-weight: bold;">include</span> <span style="color: #ba2121;">/etc/nginx/fastcgi_params</span>;
        <span style="color: #008000; font-weight: bold;">fastcgi_pass</span> <span style="color: #ba2121;">unix:/var/run/php-fastcgi/php-fastcgi.socket</span>;
        <span style="color: #008000; font-weight: bold;">fastcgi_index</span> <span style="color: #ba2121;">index.php</span>;
        <span style="color: #008000; font-weight: bold;">fastcgi_param</span> <span style="color: #ba2121;">SCRIPT_FILENAME</span> <span style="color: #ba2121;">/srv/www/www.example.com/public_html</span><span style="color: #19177c;">$fastcgi_script_name</span>;
    }
}

创建文件/usr/bin/php-fastcgi

File:/usr/bin/php-fastcgi

<span style="color: #408080; font-style: italic;">#!/bin/bash</span>

<span style="color: #19177c;">FASTCGI_USER</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">FASTCGI_GROUP</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">SOCKET</span><span style="color: #666666;">=</span>/var/run/php-fastcgi/php-fastcgi.socket
<span style="color: #19177c;">PIDFILE</span><span style="color: #666666;">=</span>/var/run/php-fastcgi/php-fastcgi.pid
<span style="color: #19177c;">CHILDREN</span><span style="color: #666666;">=</span>6
<span style="color: #19177c;">PHP5</span><span style="color: #666666;">=</span>/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -s <span style="color: #19177c;">$SOCKET</span> -P <span style="color: #19177c;">$PIDFILE</span> -C <span style="color: #19177c;">$CHILDREN</span> -u <span style="color: #19177c;">$FASTCGI_USER</span> -g <span style="color: #19177c;">$FASTCGI_GROUP</span> -f <span style="color: #19177c;">$PHP5</span>

修改文件为可执行属性

chmod +x /usr/bin/php-fastcgi

TCP Sockets 配置

File:/etc/nginx/sites-available/www.example.com

<span style="color: #008000; font-weight: bold;">server</span> {
    <span style="color: #008000; font-weight: bold;">server_name</span> <span style="color: #ba2121;">www.example.com</span> <span style="color: #ba2121;">example.com</span>;
    <span style="color: #008000; font-weight: bold;">access_log</span> <span style="color: #ba2121;">/srv/www/www.example.com/logs/access.log</span>;
    <span style="color: #008000; font-weight: bold;">error_log</span> <span style="color: #ba2121;">/srv/www/www.example.com/logs/error.log</span>;
    <span style="color: #008000; font-weight: bold;">root</span> <span style="color: #ba2121;">/srv/www/www.example.com/public_html</span>;




    index  index.html index.htm index.php;




    location / {
    try_files $uri $uri/ /index.php?$args;
    }

    # Add trailing slash to */wp-admin requests.
   rewrite /wp-admin$ $scheme://$host$uri/ permanent;

   # Directives to send expires headers and turn off 404 error logging.
   location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 24h;
    log_not_found off;
    }




   # Pass all .php files onto a php-fpm/php-fcgi server.
   location ~ \.php$ {
    # Zero-day exploit defense.
    # http://forum.nginx.org/read.php?2,88845,page=3
    # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
    # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#   fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9000;
}




}



创建文件/usr/bin/php-fastcgi

File:/usr/bin/php-fastcgi

<span style="color: #408080; font-style: italic;">#!/bin/bash</span>

<span style="color: #19177c;">FASTCGI_USER</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">FASTCGI_GROUP</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">ADDRESS</span><span style="color: #666666;">=</span>127.0.0.1
<span style="color: #19177c;">PORT</span><span style="color: #666666;">=</span>9000
<span style="color: #19177c;">PIDFILE</span><span style="color: #666666;">=</span>/var/run/php-fastcgi/php-fastcgi.pid
<span style="color: #19177c;">CHILDREN</span><span style="color: #666666;">=</span>6
<span style="color: #19177c;">PHP5</span><span style="color: #666666;">=</span>/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -a <span style="color: #19177c;">$ADDRESS</span> -p <span style="color: #19177c;">$PORT</span> -P <span style="color: #19177c;">$PIDFILE</span> -C <span style="color: #19177c;">$CHILDREN</span> -u <span style="color: #19177c;">$FASTCGI_USER</span> -g <span style="color: #19177c;">$FASTCGI_GROUP</span> -f <span style="color: #19177c;">$PHP5</span>

修改文件为可执行属性

chmod +x /usr/bin/php-fastcgi

重要的安全考虑

<span style="color: #008000; font-weight: bold;">location</span> ~ <span style="color: #bb6688;">\.php$</span> {
    <span style="color: #008000; font-weight: bold;">try_files</span> <span style="color: #19177c;">$uri</span> =<span style="color: #666666;">404</span>;
    <span style="color: #008000; font-weight: bold;">include</span> <span style="color: #ba2121;">/etc/nginx/fastcgi_params</span>;
    <span style="color: #008000; font-weight: bold;">fastcgi_pass</span> <span style="color: #ba2121;">unix:/var/run/php-fastcgi/php-fastcgi.socket</span>;
    <span style="color: #008000; font-weight: bold;">fastcgi_index</span> <span style="color: #ba2121;">index.php</span>;
    <span style="color: #008000; font-weight: bold;">fastcgi_param</span> <span style="color: #ba2121;">SCRIPT_FILENAME</span> <span style="color: #ba2121;">/srv/www/www.example.com/public_html</span><span style="color: #19177c;">$fastcgi_script_name</span>;
}

另外一种类型

<span style="color: #008000; font-weight: bold;">location</span> ~ <span style="color: #bb6688;">\.php$</span> {
    <span style="color: #008000; font-weight: bold;">include</span> <span style="color: #ba2121;">/etc/nginx/fastcgi_params</span>;
    <span style="color: #008000; font-weight: bold;">if</span> <span style="color: #ba2121;">(</span><span style="color: #19177c;">$uri</span> <span style="color: #ba2121;">!~</span> <span style="color: #ba2121;">"^/images/")</span> {
        <span style="color: #008000; font-weight: bold;">fastcgi_pass</span> <span style="color: #ba2121;">unix:/var/run/php-fastcgi/php-fastcgi.socket</span>;
    }
    <span style="color: #008000; font-weight: bold;">fastcgi_index</span> <span style="color: #ba2121;">index.php</span>;
    <span style="color: #008000; font-weight: bold;">fastcgi_param</span> <span style="color: #ba2121;">SCRIPT_FILENAME</span> <span style="color: #ba2121;">/srv/www/www.example.com/public_html</span><span style="color: #19177c;">$fastcgi_script_name</span>;
}

启用网站服务

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

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

File:/etc/init.d/php-fastcgi

<span style="color: #408080; font-style: italic;">#!/bin/bash</span>

<span style="color: #19177c;">PHP_SCRIPT</span><span style="color: #666666;">=</span>/usr/bin/php-fastcgi
<span style="color: #19177c;">FASTCGI_USER</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">FASTCGI_GROUP</span><span style="color: #666666;">=</span>www-data
<span style="color: #19177c;">PID_DIR</span><span style="color: #666666;">=</span>/var/run/php-fastcgi
<span style="color: #19177c;">PID_FILE</span><span style="color: #666666;">=</span>/var/run/php-fastcgi/php-fastcgi.pid
<span style="color: #19177c;">RET_VAL</span><span style="color: #666666;">=</span>0

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

启动 php-fastcgi and 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

测试 PHP with FastCGI

File:/srv/www/example.com/www/publichtml/test.php_

<span style="color: #bc7a00;"><?php</span> <span style="color: #008000;">phpinfo</span>(); <span style="color: #bc7a00;">?></span>

其他参考网址