2014年3月17日 星期一

2014年3月16日 星期日

chroot: failed to run command : No such file or directory

   If you use chroot and it said "chroot: failed to run command  : No such file or directory",  how to slove it? Ok, follow me.


   For example, if you type "chroot /tmp/root /bin/bash" and get error. First of all, check whether the file "/tmp/root/bin/bash" exists. If not, create it.

   But if the file exists and got "No such file or directory", use "file /tmp/root /bin/bash" to check the file. If the file is "statically linked", I don't know what happen to your system. If the file is "dynamically linked (uses shared libs)", you have two way to go. The one is that you can compile the file to "statically linked". The another is that copy the shared libs which your program use to the right path(here is /tmp/root/lib).
 
    You can use "ldd to find out all shared libs your prcess use. The output is like below.

    linux-vdso.so.1 =>  (0x00007fff747fe000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fd6544bc000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd6542b8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd653eef000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd6546ff000)

2014年3月9日 星期日

golang 的 revel web framework 安裝與使用



要在 linux 下安裝 revel,要先設定環境變數,讓 go get 能夠將檔案下載到你指定的路徑

export GOPATH="/home/user/go"
export PATH="$PATH:$GOPATH/bin"

下載 revel
go get github.com/robfig/revel

編譯 revel

go get github.com/robfig/revel/revel

你可以用這個指令確定是否安裝成功

$ revel help

建立新應用程式

revel new myapp

注意:他是建立在 $GOPATH 的路徑上,而非當前目錄。輸出訊息會告訴你專案建在哪。

執行你的第一個程式

revel run myapp


有關 route 的資訊放在 conf/routes。內容依序為 method, path, function

GET     /                                       App.Index

App.Index 的程式碼大概長這樣。每個 Action 都會去找對應檔名的 view 來當 template。如下面程式碼會找 view 目錄下的 app/index.html 來當 template

package controllers

import "github.com/robfig/revel"

type App struct {
 *revel.Controller
}

func (c App) Index() revel.Result {
 return c.Render()
}

template 大概長這樣,這個 template 從 action 接受兩個參數,一個 name,一個 sum。

Hello, {{ .name }} {{ .sum }}

我們要傳資料到 template ,只要在 c.Render 裡放我們要的參數即可

func (c App) Index(name string, a int, b int) revel.Result {    sum := a + b return c.Render(name, sum)}

get/post method 的參數會被放在 action 相對應的參數裡。如 name=world&a=1&b=2,到 action 的話,變數 name是字串 world ,a 是整數1,b 是 整數2


如果你程式都寫完了,想要 deploy。用 

revel build import/path/to/app /tmp/app

之後你就可以到 /tmp/app ,用 ./run.sh 來跑 server 了。






2014年3月8日 星期六

web load balance

Nginx 要作到 load balance 很簡單,只要幾個設定就好了。以下面的 Nginx 設定檔為例(來源)。


worker_processes 2;
 
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
 
events {
    worker_connections 1024;
    use epoll;
}
 
http {
    charset utf-8;
 
    # Enumerate all the Tornado servers here
    upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }
 
    include /path/to/nginx.mime.types;
    default_type application/octet-stream;
 
    access_log /var/log/nginx/access.log;
 
    keepalive_timeout 65;
    proxy_read_timeout 200;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;
 
    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;
 
    server {
        listen 80;
 
        location ^~ /static/ {
            root /path/to/app;
            if ($query_string) {
                expires max;
            }
        }
 
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}
只要簡單的設定 upstream frontends(frontends名稱可自訂) 裡的 server ,即可作到 loadbalance。

下面連結有 lighttpd 的 load balance 例子。
http://www.playframework.com/documentation/2.2.x/HTTPServer
設定檔大概就長這樣



server.modules = (
      "mod_access",
      "mod_proxy",
      "mod_accesslog"
)

$HTTP["host"] =~ "www.myapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" =>
        ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}

$HTTP["host"] =~ "www.loadbalancedapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" => (
          ( "host" => "127.0.0.1", "port" => 9001 ),
          ( "host" => "127.0.0.1", "port" => 9002 ) )
    )
}



apache 的 load balancer 的資料在這
http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html