February 2008 Archives

php-fastcgi.sh

|

Source : http://topfunky.net/svn/shovel/nginx/php-fastcgi.sh

 

#!/bin/bash

# Description: PHP-FastCgi start script from http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/
#
# Author: Alexey Kovyrin http://blog.kovyrin.net
# Comments by: Geoffrey Grosenbach http://topfunky.com
#
# This script is started once and receives PHP requests from Nginx for 
# all apps. The Nginx config passes the full path to the script being requested, so
# only one fastcgi runner is needed for all apps, virtual hosts, etc.
#
# See also the init.d script for starting this on boot.
#
# To install PHP, I had to also compile the following:
#
#   * http://www.gnu.org/software/m4/
#   * http://flex.sourceforge.net/
#   * http://php.net/ with './configure --prefix=/usr/local --enable-fastcgi'

## ABSOLUTE path to the PHP binary
PHPFCGI="/usr/local/bin/php"

## tcp-port to bind on
FCGIPORT="9000"

## IP to bind on
FCGIADDR="127.0.0.1"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=5

## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

## if this script is run as root switch to the following user
USERID=deploy

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"

if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi

echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &

nginx.conf

|

Source : http://topfunky.net/svn/shovel/nginx/conf/nginx-php.conf

##
# Basic config modified only slightly from http://brainspl.at/articles/2007/01/03/new-nginx-conf-with-optimizations
#
# Turns SSI on and uses locations as defined in install-nginx.sh script.
#
# See also http://topfunky.net/svn/shovel/nginx
#
# USE AT YOUR OWN RISK!

# user and group to run as
user  deploy deploy;

# number of nginx workers
worker_processes  6;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
  worker_connections 1024;
}

# start the http module where we config http access.
http {
  # pull in mime-types. You can break out your config 
  # into as many include's as you want to make it cleaner
  include /usr/local/nginx/conf/mime.types;

  # set a default type for the rare situation that
  # nothing matches from the mimie-type include
  default_type  application/octet-stream;

  # configure log format
  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  # main access log
  access_log  /var/log/nginx_access.log  main;

  # main error log
  error_log  /var/log/nginx_error.log debug;

  # no sendfile on OSX
  sendfile on;

  # These are good default values.
  tcp_nopush        on;
  tcp_nodelay       off;
  # output compression saves bandwidth 
  gzip            on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  # this is where you define your mongrel clusters. 
  # you need one of these blocks for each cluster
  # and each one needs its own name to refer to it later.
  #
  # Rename to mongrel_site1, mongrel_site2, etc if using
  # virtual hosts.
  upstream mongrel {
    server 127.0.0.1:5000;
    # server 127.0.0.1:5001;
    # server 127.0.0.1:5002;
  }


  # Copy this section on down and put into a separate file 
  # if you want to organize your virtual hosts in files.
  #
  # Then include here with
  #
  #   include /usr/local/nginx/conf/vhosts/my_subdomain.conf
  #
  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT.
    listen 80;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /var/www/apps/mysite.com/current/public;

    # vhost specific access log
    access_log  /var/www/apps/mysite.com/shared/log/nginx.vhost.access.log  main;

    # NOTE Uncomment and edit to redirect all subdomains back to domain.com
    #      Useful for sending .net and .org variants back to your site.
    # if ($host !~ ^domain\.com$) {
    #   rewrite ^.+ http://domain.com$uri permanent;
    #   break;
    # }

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # Uncomment to allow server side includes so nginx can 
      # post-process Rails content
      ## ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        # Use other cluster name here if you are running multiple
        # virtual hosts.
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/mysite.com/current/public;
    }
  }

  # This server is setup for ssl. Uncomment if 
  # you are using ssl as well as port 80.
  # server {
  #   # port to listen on. Can also be set to an IP:PORT
  #   listen 443;
  #   
  #   # Set the max size for file uploads to 50Mb
  #   client_max_body_size 50M;
  # 
  #   # sets the domain[s] that this vhost server requests for
  #   # server_name www.[engineyard].com [engineyard].com;
  #
  #   # doc root
  #   root /var/www/apps/mysite.com/current/public;
  # 
  #   # vhost specific access log
  #   access_log  /var/www/apps/mysite.com/shared/log/nginx.vhost.access.log  main;
  # 
  #   # NOTE See also http://blog.imperialdune.com/2007/3/31/setting-up-godaddy-turbo-ssl-on-nginx
  #   #      if you are buying a GoDaddy SSL cert.
  #   ssl                  on;
  #   ssl_certificate      /var/keys/domain.com.crt;
  #   ssl_certificate_key  /var/keys/domain.com.key;
  #
  #   # NOTE Uncomment and edit to redirect all subdomains back to domain.com
  #   #      Useful for sending .net and .org variants back to your site.
  #   if ($host !~ ^domain\.com$) {
  #     rewrite ^.+ https://domain.com$uri permanent;
  #     break;
  #   }
  #
  #   # this rewrites all the requests to the maintenance.html
  #   # page if it exists in the doc root. This is for capistrano's
  #   # disable web task
  #   if (-f $document_root/system/maintenance.html) {
  #     rewrite  ^(.*)$  /system/maintenance.html last;
  #     break;
  #   }
  # 
  #   location / {
  #     # needed to forward user's IP address to rails
  #     proxy_set_header  X-Real-IP  $remote_addr;
  # 
  #     # needed for HTTPS
  #     proxy_set_header X_FORWARDED_PROTO https;
  # 
  #     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  #     proxy_set_header Host $http_host;
  #     proxy_redirect false;
  #     proxy_max_temp_file_size 0;
  #     
  #     # If the file exists as a static file serve it directly without
  #     # running all the other rewite tests on it
  #     if (-f $request_filename) { 
  #       break; 
  #     }
  # 
  #     # check for index.html for directory index
  #     # if its there on the filesystem then rewite 
  #     # the url to add /index.html to the end of it
  #     # and then break to send it to the next config rules.
  #     if (-f $request_filename/index.html) {
  #       rewrite (.*) $1/index.html break;
  #     }
  # 
  #     # this is the meat of the rails page caching config
  #     # it adds .html to the end of the url and then checks
  #     # the filesystem for that file. If it exists, then we
  #     # rewite the url to have explicit .html on the end 
  #     # and then send it on its way to the next config rule.
  #     # if there is no file on the fs then it sets all the 
  #     # necessary headers and proxies to our upstream mongrels
  #     if (-f $request_filename.html) {
  #       rewrite (.*) $1.html break;
  #     }
  # 
  #     if (!-f $request_filename) {
  #       proxy_pass http://mongrel;
  #       break;
  #     }
  #   }
  # 
  #   error_page   500 502 503 504  /500.html;
  #   location = /500.html {
  #     root   /var/www/apps/mysite.com/current/public;
  #   }
  # }


}

nginx-php.conf [zz]

|

Source : http://topfunky.net/svn/shovel/nginx/conf/nginx-php.conf

##
# Basic config modified only slightly from http://brainspl.at/articles/2007/01/03/new-nginx-conf-with-optimizations
#
# Allows PHP to run alongside Ruby on Rails. Also 
# turns SSI on and uses locations as defined in install-nginx.sh script.
#
# See also http://topfunky.net/svn/shovel/nginx

# user and group to run as
user  deploy deploy;

# number of nginx workers
worker_processes  6;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
  worker_connections 1024;
}

# start the http module where we config http access.
http {
  # pull in mime-types. You can break out your config 
  # into as many include's as you want to make it cleaner
  include /usr/local/nginx/conf/mime.types;

  # set a default type for the rare situation that
  # nothing matches from the mimie-type include
  default_type  application/octet-stream;

  # configure log format
  log_format main '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

  # main access log
  access_log  /var/log/nginx_access.log  main;

  # main error log
  error_log  /var/log/nginx_error.log debug;

  # no sendfile on OSX
  sendfile on;

  # These are good default values.
  tcp_nopush        on;
  tcp_nodelay       off;
  # output compression saves bandwidth 
  gzip            on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;


  # this is where you define your mongrel clusters. 
  # you need one of these blocks for each cluster
  # and each one needs its own name to refer to it later.
  upstream mongrel {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    # server 127.0.0.1:8002;
  }


  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for.
    # None means listen to all.
    #server_name www.rubyonrailsworkshops.com rubyonrailsworkshops.com;

    # doc root
    root /var/www/apps/rubyonrailsworkshops/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # Uncomment to allow server side includes so nginx can 
      # post-process Rails content
      ## ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # Look for existence of PHP index file.
      # Don't break here...just rewrite it.
      if (-f $request_filename/index.php) {
        rewrite (.*) $1/index.php;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/rubyonrailsworkshops/current/public;
    }

    # Pass the PHP scripts to FastCGI server listening on ip:port.
    #
    # Requires you to start one instance of http://topfunky.net/svn/shovel/nginx/php-fastcgi.sh
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /var/www/apps/rubyonrailsworkshops/current/public$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
    }

  }

  # This server is setup for ssl. Uncomment if 
  # you are using ssl as well as port 80.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 443;
    
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /var/www/apps/rubyonrailsworkshops/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      ssi on;

      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header X_FORWARDED_PROTO https;

      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) { 
        break; 
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite 
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end 
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the 
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /var/www/apps/rubyonrailsworkshops/current/public;
    }
  }


}

find

|
我们经常要在日常的工作中查找文件,要说linux下最强大的文件查找工具,非find莫属。find可以从文件名、读取修改时间、大小、类型等等来筛选文件,可以完成日常几乎所有的查找需求。

来看一个简单的例子:

find /tmp/ -name 'galeki*'

意思就是在/tmp目录下找出以"galeki"开头的文件,-name指出是对文件名进行筛选,是不是很简单明了?除了文件名,find还有更强大的筛选条件:
1.时间:
参数     意义
-atime n     上一次存取时间在n*24小时前以内的文件
-ctime n     上一次状态改变在n*24小时前以内的文件
-mtime n     上一次修改时间在n*24小时前以内的文件
-newer file     比file所指的文件还要新的文件

2.大小:
参数     意义
-size n[bkMG]     大小为n(b为比特,k为KB,M为MB,G为GB)的文件

3.所有者:
参数     意义
-user name     所有者用户名称是name的文件
-group name     所有者用户组群名称是name的文件

例子:
find /tmp/ -name 'sess*' -mtime 1   #在/tmp目录下找到所有以'sess'开头、并且在一个小时以内修改过的文件
find ./ -name '*.zip' -size 546k       #在当前目录下找到所有546KB的zip压缩包文件
find /tmp/ -user 'galeki'               #在/tmp目录下找到所有所有者为galeki的文件

另外,还可以用"+"、"-"来进一步修饰时间和大小的条件:
find src/ -mtime +2            #找出src目录下上一次修改时间在2天以前的文件
find download/ -size +50M    #在download目录下找出大于50M的文件

光找出文件还不够,我们还要对找出的文件进行处理才有意义,一般来讲可以把find的查找结果传递给其他的命令做进一步处理。find本身也提供了许多处理动作的选项。
常用的处理动作:
参数     意义
-print
    打印出找出文件的完整地址,一个文件一行
-delete
    删除找出的文件
-exec command {} \;
    对每一个找到的文件执行command命令,并用文件替换"{}","\;"是固定的结束符。

例子:
find /tmp/ -ctime +7 -delete            #删除/tmp目录下一周以前创建的文件
find ./ -name '*.unk' -exec file {} \;   #找出当前文件夹下后缀是unk的文件,并用file命令显示每个文件的文件类型

以上只是find的一些简单的用法.

xcache ZendOptimizer

|
之前一直是eaccelerator + ZendOptimizer的组合
现在换成了xcache+ZendOptimizer, 按照其安装方法,没有什么难度,尤其在BSD上,还是通过ports来安装
不过在重启php就出现问题了,总是报错.......
最终的解决方法是,把xcache.ini里
[xcache-common]
;; install as zend extension (recommended, but not working yet)
;; zend_extension = /usr/local/lib/php/20060613/xcache.so
; zend_extension_ts = /usr/local/lib/php/20060613/xcache.so
;; or install as extension
;;extension = xcache.so
关于用zend 扩展模式的两个加入到php.ini里在ZendOptimizer之前即可

freebsd php-cgi script

|

%cat /usr/local/etc/rc.d/fastcgi-php.sh
#!/bin/sh
#  FreeBSD rc.d script for fastcgi+php
#  in rc.conf
# fcgiphp_enable (bool):        Set it to "YES" to enable fastcgi+php
#                               Default is "NO".
# other options see below
#

. /etc/rc.subr

name="fcgiphp"
rcvar=`set_rcvar`

load_rc_config $name

: ${fcgiphp_enable="NO"}
: ${fcgiphp_bin_path="/usr/local/bin/php-cgi"}
: ${fcgiphp_user="www"}
: ${fcgiphp_group="www"}
: ${fcgiphp_children="16"}
: ${fcgiphp_port="9999"}
: ${fcgiphp_socket="/tmp/php-fastcgi.sock"}
: ${fcgiphp_env="SHELL PATH USER"}
: ${fcgiphp_max_requests="500"}
: ${fcgiphp_addr="localhost"}


pidfile=/var/run/fcgiphp/fcgiphp.pid
procname="${fcgiphp_bin_path}"
command_args="/usr/local/bin/spawn-fcgi -a ${fcgiphp_addr} -f ${fcgiphp_bin_path} -u ${fcgiphp_user} -g ${fcgiphp_group} -C ${fcgiphp_children} -P ${pidfile}"

start_precmd=start_precmd
stop_postcmd=stop_postcmd

start_precmd()
{
        PHP_FCGI_MAX_REQUESTS="${fcgiphp_max_requests}"
        FCGI_WEB_SERVER_ADDRS=$fcgiphp_addr
        export PHP_FCGI_MAX_REQUESTS
        export FCGI_WEB_SERVER_ADDRS
        allowed_env="${fcgiphp_env} PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
# copy the allowed environment variables
        E=""
        for i in $allowed_env; do
                eval "x=\$$i"
                E="$E $i=$x"
        done
        command="env - $E"

        if [ -n "${fcgiphp_socket}" ]; then
                command_args="${command_args} -s ${fcgiphp_socket}"
        elif [ -n "${fcgiphp_port}" ]; then
                command_args="${command_args} -p ${fcgiphp_port}"
        else
                echo "socket or port must be specified!"
                exit
        fi
}

stop_postcmd()
{
        rm -f ${pidfile}
#       eval "ipcs | awk '{ if (\$5 == \"${fcgiphp_user}\") print \"ipcrm -s \"\$2}' | /bin/sh"
}

run_rc_command "$1"

About this Archive

This page is an archive of entries from February 2008 listed from newest to oldest.

January 2008 is the previous archive.

March 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.1