聚會時間公告: 因應COSCUP 2011, Kalug 8月份休會一次

六月 30, 2012

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» [筆記] 解決 apache mod_fcgid: HTTP request length 136000 (so far) exceeds MaxRequestLen (131072)

logo-Ubuntu

上次寫了一篇 Ubuntu 10.10 (Maverick) 架設 Nginx + PHP FastCGI,不過在 PHP 上傳檔案部份發現了問題,打開 /var/log/apache2/error.log 發現底下錯誤訊息:

mod_fcgid: HTTP request length 136000 (so far) exceeds MaxRequestLen (131072)

上網找了一下是 fcgid.conf 設定錯誤,解決方式非常簡單,只要打開 /etc/apache2/mods-available/fcgid.conf,將底下內容

<IfModule mod_fcgid.c>
  AddHandler    fcgid-script .fcgi
  FcgidConnectTimeout 20
</IfModule>

取代成

<IfModule mod_fcgid.c>
  AddHandler    fcgid-script .fcgi
  FcgidConnectTimeout 20
  # to get around upload errors when uploading images increase the MaxRequestLen size to 15MB
  MaxRequestLen 15728640
</IfModule>

如果檔案上傳時間不夠,又會出現底下錯誤

mod_fcgid: read data timeout in 40 seconds
Premature end of script headers: index.php

所以我們將設定檔再改成底下就可以了

<IfModule mod_fcgid.c>
  AddHandler    fcgid-script .fcgi

  # fix for:   mod_fcgid: read data timeout in 40 seconds
  IdleTimeout 3600
  DefaultMinClassProcessCount 100
  FcgidConnectTimeout 120
  IPCCommTimeout 400

  # to get around upload errors when uploading images increase the MaxRequestLen size to 15MB
  MaxRequestLen 15728640
</IfModule>

最後附上個人 fcgid.conf 設定檔給大家參考

<IfModule mod_fcgid.c>
  AddHandler    fcgid-script .fcgi .php
  FcgidConnectTimeout 20
  FcgidIPCDir /var/lib/apache2/fcgid/sock
  IdleTimeout 3600
  ProcessLifeTime 7200
  MaxProcessCount 1000
  DefaultMinClassProcessCount 3
  DefaultMaxClassProcessCount 100
  IPCConnectTimeout 8
  IPCCommTimeout 360
  BusyTimeout 300
  FcgidWrapper /usr/bin/php5-cgi .php
  MaxRequestLen 15728640
</IfModule>

參考網站:
[fix] mod_fcgid: HTTP request length xyz (so far) exceeds MaxRequestLen (131072)

Related View

五月 25, 2012

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» 在 Ubuntu 10.10 (Maverick) 架設 Nginx + PHP FastCGI

logo-Ubuntu

今天來筆記如何在 Ubuntu 底下完整安裝 Nginx + PHP FastCGI,以及了解 Nginx 基本設定。我想大家都知道 Apache 是一個很好的 Web Server 伺服器,也常常用在個人網站,或者一般小型專案,網路上也有一堆懶人包,如 Appserv, Xampp,對於新手入門來說 Apache 是一個很好的選擇,但是您會發現用了 Apache 後,系統記憶體常常飆高 XD,載入太多額外不必要的模組,所以非常肥大,那這次就來嘗試另外一套 Web 伺服器 Nginx 吧。

安裝套件

Ubuntu 可以透過 apt-get 方式安裝

$ apt-get update
$ apt-get upgrade
$ apt-get install nginx php5-cli php5-cgi spawn-fcgi psmisc

PHP CGI binary 能自行執行在 Fast CGI Interface 上面,並不需要額外安裝任何程式。只需要安裝 php-cgi 套件 aptitude install php5-cgi,接著執行 php-cgi -b 127.0.0.1:9000 就完成了。你會發現 local 端用 PHP 跑起一個 FastCGI interface 服務,透過 Nginx 的 fastcgi_pass 127.0.0.1:9000; 就可以跟 FastCGI 做溝通。

開機自動執行 PHP FastCGI

建立 PHP FastCGI 開機啟動 Script,首先打開 /etc/init.d/php-fcgi (如果無此檔案,請用 touch 方式產生),將底下 shell script 程式碼放入

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php5-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

存檔後,請修改此檔案權限,讓其他使用者可以執行程式。

chmod +x /etc/init.d/php-fcgi

透過底下方式,可以隨時啟動或關閉 PHP FastCGI

# start the fast cgi
/etc/init.d/php-fcgi start
# stop the fast cgi
/etc/init.d/php-fcgi stop
# restart fast cgi
/etc/init.d/php-fcgi restart

加入開機自動執行

update-rc.d php-fcgi defaults

編輯 Nginx 設定檔

Nginx 設定檔目錄在 /etc/nginx/,我們打開設定檔 nginx.conf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

此檔案不需要更動,我們可以看到最後一行有 include /etc/nginx/sites-enabled/*,這就是 Nginx Virtual Host 設定檔位置,預設會有一個 default 設定,如果需要建立其它 domain,我們就可以複製此檔案即可。底下會帶大家如何設定 Nginx + PHP FastCGI,直接打開 default,加底下設定值寫到檔案即可

location ~ \.php {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;
    fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}

注意看到 fastcgi_pass 就是用 TCP 方式跟 PHP FastCGI 溝通,另外網頁目錄也是需要設定

location / {
    root   /var/www;
    index  index.php index.html index.htm;
}

設定完成後,重新啟動 Nginx

/etc/init.d/nginx restart

打開 /var/www/phpinfo.php 裡面寫入

<?php
phpinfo();
?>

看到 PHP 設定值畫面就代表架設成功。另外可以用 /etc/init.d/nginx configtest 檢查 Nginx 設定檔是否正確,如果要新開虛擬網站,請透過底下方式建立

# 建立目錄
mkdir /var/www/www.example.com
# 建立設定檔
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
# 連結
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
# 重新啟動伺服器
/etc/init.d/php-fcgi start
/etc/init.d/nginx restart

出現 No input file specified.

請注意網站設定檔是否設定錯誤

fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;

上面請務必設定正確,根據不同的根目錄,請自行修改 /var/www 這部份

其他參考網站:
PHP FastCGI with nginx on Ubuntu
架設 Nginx + PHP FastCGI 於 Ubuntu Linux 10.04
Nginx and PHP-FastCGI on Ubuntu 10.10 (Maverick)
官方 Wiki: PHPFcgiExample (必看)
官方 Wiki: Configuration

Related View

十月 30, 2007
» [tips] using flup as trac's default fastcgi backend. (with lighttpd)

I've found trac's fastcgi backend simply out-dated. I thinks it's direct copied from an old flup source, and since then flup really updates a lot, I've already use thread pool techniques everywhere in our web applications/frameworks for a while, so current trac's fastcgi backend implement just so out-dated and looks wierd when I installed it, really.

But thanks there's WSGI to save us ! I could just replace the fastcgi backend to flup-0.5 (latest, and author claims it maybe final version of flup) and SAVE MEMORY !

and it's simple too,
just install the flup package, and edit your old trac.fcgi / lighttpd's settings.

trac.fcgi:


try:
import pkg_resources

from trac import __version__ as VERSION
from trac.web.main import dispatch_request

from flup.server.fcgi import WSGIServer
WSGIServer(dispatch_request,maxSpare=100,minSpare=100,maxThreads=100).run()

except SystemExit:
raise
except Exception, e:
print 'Content-Type: text/plain\r\n\r\n',
print 'Oops...'
print
print 'Trac detected an internal error:'
print
print e
print
import traceback
import StringIO
tb = StringIO.StringIO()
traceback.print_exc(file=tb)
print tb.getvalue()


you can change the line which specify
maxSpare,minSpare,maxThreads,I use 100,
you should use less than 381 if you're using
python 2.4 (you'll hit stack limit at least on i386-linux) however if you're using python 2.5 you can change stack size via thread.stack_size,
though I don't think anyone would need that high concurrency (who knows?!).

below's also a sample setting in lighttpd.conf,

what really matters here is to remember add a
"max-procs" => 1
line in it, since we've already using thread pools technique, forking another process seems redundant (and WASTE memory).


fastcgi.server += ("/sandbox" =>
("sandbox" =>
("socket" => "/tmp/trac-sandbox.sock", #need to change for each
"bin-path" => "/var/trac/cgi-bin/trac.fcgi",
"max-procs" => 1, #default 4
"check-local" => "disable",
"bin-environment" =>
("TRAC_ENV" => "/var/trac/sandbox")
)
)
)


oh, btw I'm using trac-0.11dev, but I think any version below 0.11 which implement WSGI should also works.

support:

biggo.com.tw

A Django site.