2013年8月1日 星期四

[Linux] 使用 C/C++ 撰寫 FastCGI 與 Nginx 設定筆記 @ Ubuntu 12.04

大概有半年了,跟 fastcgi 與 nginx 的淵源還不斷地繼續著,不過,開始更有機會真的用 C/C++ 寫 Fastcgi 啦,在此簡易筆記用法

/etc/nginx/sites-enabled/fcgidev:

server {
  listen 55688;

  root /tmp/fcgidev;
  client_max_body_size 0;

  location /fcgi {
    fastcgi_pass 127.0.0.1:55699;
    include /path/etc/fastcgi_params;
  }

  location / {
  }
}

用 C 撰寫 (https://github.com/changyy/fastcgi-study/blob/master/fcgi/main.c):

#include <stdlib.h>
#include <fcgi_stdio.h>

#define CGI_HEAD_BODY_SEPARATOR "\r\n\r\n"
#define CGI_CONTENT_TYPE_TEXT_HTML "Content-type: text/html"
#define CGI_CONTENT_TYPE_APPLICATION_JSON "Content-type: application/json"
#define CGI_WELCOME_HEAD CGI_CONTENT_TYPE_TEXT_HTML
#define CGI_WELCOME_BODY \
"<html>\
<head>\
<title>Hello FastCGI</title>\
</head>\
<body>\
<h1>Hello FastCGI</h1>\
</body>\
</html>"

int main(int argc, char** argv)
{
int count = 0;
while(FCGI_Accept() >= 0)
{
printf(
CGI_WELCOME_HEAD
CGI_HEAD_BODY_SEPARATOR
CGI_WELCOME_BODY
);
}
return 0;
}

編譯:

$ gcc -o fcgi-out main.c -lfcgi 

用 C++ 撰寫(https://github.com/changyy/fastcgi-study/blob/master/fcgicc/main.cpp):

#include <cstdlib>
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPResponseHeader.h"
#include "cgicc/HTMLClasses.h"
#include "FCgiIO.hpp"

#define CGI_WELCOME_BODY \
"<html>\
<head>\
<title>Hello FastCGI</title>\
</head>\
<body>\
<h1>Hello FastCGI</h1>\
</body>\
</html>"

int main(int argc, char **argv)
{
if (argc < 2) exit(1);

FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0,0);

while(FCGX_Accept_r(&request) == 0)
{
try
{
cgicc::FCgiIO io(request);
cgicc::Cgicc cgi(&io);

cgicc::HTTPResponseHeader resp("Status:", 200, "OK");
resp.addHeader("Content-Type", "text/html");
io << CGI_WELCOME_BODY;
}
catch (std::exception const &e)
{
}
FCGX_Finish_r(&request);
}
return 0;
}


編譯:

$ g++ -o fcgi-out main.cpp FCgiIO.cpp -lfcgi -lcgicc -lfcgi++

執行:

$ spawn-fcgi -a 127.0.0.1 -n -p 55699 -F 1 -- fcgi-out /tmp

2013年7月29日 星期一

nginx worker process exited on signal 7 @ ARM & LOA

嵌入式就是這麼奇妙!nginx 在 x86 可以穩穩跑、在 ARM-based 的 linux 也能穩穩跑,但是在 LOA 上頭就是會收到 signal 7 訊號而掛掉!

最後則是想要編 debug 訊息來用,意外發現 -O0 時就一切正常了!

故,最佳化這種事,還是小心為妙。

Scripting Nginx with Lua (lua-nginx-module) 筆記 @ Linux

最近常用 nginx 開發服務,接著又碰碰 lua 的部分,十分彈性。接下來,移植到 ARM 裝置後,就開始進入 debug 模式 XD 需要追蹤一些 requests 的流向,又想到 lua 啦

nginx-test.conf

  location /lua {
    default_type 'text/plain';
    content_by_lua '
        local currtime = ngx.localtime()
        local file = io.open("/tmp/nginx_lua.log", "a");
        file:write(currtime, "\\t", ngx.var.uri, "\\n");
        file:write("\\t", ngx.var.args, "\\n");
        file:close();

        ngx.print(currtime, "\\n");
        ngx.print(ngx.var.uri,"\\n");
        ngx.print(ngx.var.args,"\\n");
        --ngx.print(ngx.var.request_body, "\\n");
    ';
  }


/tmp/nginx_lua.log:
2013-07-29 04:19:07     /lua
        a=1&b=c&d


Web:
2013-07-29 04:19:07
/lua
a=1&b=c&d


如此一來,當 request 進來時,可以查看 /tmp/nginx_lua.log 看看到底走了幾個 requests,留意項目:

  • 在 content_by_lua 中,要印出 newline 必須用 ngx.print( "\\n" ) ,但如果是在 access_by_lua_file 中,只需 ngx.print( "\n" ) 即可。
  • 註解是用 "--" 而非用 "#" 或 "//" 開頭

2013年7月27日 星期六

使用微軟 Windows Live 管理中心免費郵件託管服務

livedomain01

想說前陣子買的 domain 該好好把玩一下,就想到 Mail server 服務啦。若曾架過站,應該不難體會維護一台機器、甚至一台 Mail server 是多重的任務了 XD 更別說中毒、被駭之後的處理流程等。所以,就先看看市面上有什麼好用的資源,除非不得已不然就不自己架站 :P

前陣子用過 Google Apps for Business 了,這次就改用微軟 Windows Live 管理中心吧!其實我沒特別討厭微軟,至從我用 MBP 被我操到常常出現多國語言的招呼聲或是瞬間登出等奇異現象後,我覺得微軟其實沒那麼糟 XDD

首先進入 https://domains.live.com 網頁後,點選"馬上開始":


livedomain02

livedomain03

輸入一些資料後,即可進入郵件託管設定頁面,首要之事就是(必須欄位)的設定,其實也真的只要設定這欄就好。

livedomain04

接著就是要去設定 DNS MX record 的部分,由於我是使用 Namecheap 服務,所以可以直接在 Namecheap 網站上設定完,十分便利(可參考 Namecheap - How can I set up MX records for my domain?):

livedomain05

如此一來,在切回 Windows Live 管理中心,點選"立刻更新"後,就會看到畫面更新為以下狀態,可以從 live.com 用自己的 email address 登入囉!

livedomain06

最後一提的,建立好的信箱可以用 POP3 協定收信囉:

POP3: pop3.live.com:995 (是否加密:Yes)
帳號:完整的 Email address (如:user@yourdomain.com)

2013年7月22日 星期一

[LOA] Linux on Android @ Android 4.x


圖片來源:Complete Linux Installer

最近把玩 Linux on Android (LOA),這的確是個還不錯的架構:
Complete Linux Installer - https://play.google.com/store/apps/details?id=com.zpwebsites.linuxonandroid
簡單流程:

  • Android 裝置要先 root 過
  • 在 Android 裝置上跑 busybox 建制相關環境
  • 採用 chroot 切換到 linux image 運行
更多細節:
# cat /data/data/com.zpwebsites.linuxonandroid/files/bootscript.sh
其中運行最關鍵的是 busybox 這隻程式,安裝 Complete Linux Installer 有機會碰到 busybox 不能跑,需要自行把它編譯出來喔。看到 LOA 這等應用我才發現 busybox 還真的不是普通威啊! !