2013年7月19日 星期五

Heroku On Linux 安裝

Heroku 是提供雲端運算的平台
方便讓不想自行架設伺服器或是管理server的人

國內大多的空間都提供 PHP + MySQL
對於目前想找 Ruby or Python 甚至是當紅的 Node.js
是不太好找到合適的空間來放

老牌的 Heroku 支持 Ruby, Node.js, Clojure, Java, Python, and Scala.
這篇是基礎的安裝教學

要使用 Heroku 一定要預先安裝 Heroku Toolbelt
才可以開始使用他的服務


Linux Mint:

首先連結至 https://toolbelt.heroku.com/debian

wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

這段 script 將會新增 heroku 的 repository 以及 key 至您的 apt 來源,並安裝 heroku 以及 foreman 套件。 heroku client 將會被安裝至 /usr/local/heroku 並將 /usr/local/heroku/bin 新增至您的 PATH 環境變數內

Linux Fedora 17

https://toolbelt.heroku.com/standalone

wget -qO- https://toolbelt.heroku.com/install.sh | sh
echo "$ echo 'PATH=\"/usr/local/heroku/bin:\$PATH\"' >> ~/.bashrc"


第一行的安裝 script 將會下載 heroku client 的 tarball 檔 並將他安裝至 /usr/local/heroku
第二行則是將 heroku 的路徑加入至 PATH 環境變數中


在 Terminal 裡面輸入上面的指令

這是什麼呢?
  • Heroku client - 建立或管理 Heroku apps 的 CLI tool
  • Foreman - 簡易快速在 Local 端運行你的 apps
  • Git -版本控制及更新至 Heroko

開始動手玩

安裝完畢後, 你可以在 Terminal 中輸入 heroku 指令, 登入使用你在 Heroku 建立帳號時所使用的 email 及 密碼.
(中途會問您是否要建立 SSH 的 public key , 這是要)

$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

現在可以開始建立 Heroku app 囉

$ cd ~/myapp
$ heroku create
Creating stark-fog-398... done, stack is cedar
http://stark-fog-398.herokuapp.com/ | git@heroku.com:stark-fog-398.git
Git remote heroku added

2013年7月18日 星期四

Express.js: Session for Production

Express.js Production Mode 啟動後會收到以下訊息
Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and obviously only work within a single process.
避免造成 Memory Leak 方案如下
  1. no sessions - fast (438 req/sec)
  2. cookieSession: requires no external service, minor speed impact (311 req/sec) - fastest, sessions will expire with the cookie (customised by maxAge)
  3. connect-redis: requires redis server, large speed impact (4 req/sec with redis2go and redisgreen) - faster than mongo, sessions will be deleted after a while (customised by ttl)
  4. connect-mongo - requires mongodb server, large speed impact (2 req/sec with mongohq) - slower than redis, also has no way to remove sessions with no maxAge, requires manual clear_interval to be set to cleanup sessions

這邊我選用了 MongoDB 來作為解決方案

使用 connect-mongo 

INSTALL

  
$ npm install connect-mongo
  
mongoStore = require('connect-mongo')(express);
app.configure(function(){
    ...
    app.use(express.session({
        secret: "@#$TYHBVGHJIY^TWEYKJHNBGFDWGHJKUYTWE#$%^&*&^%$#",
        store: new mongoStore({
            host: setting.mongo.host,
            port: setting.mongo.port,
            db: 'examOnlineSessions',
            collection: 'sessions'
        })
    }));
    ...
})
With connect:
var connect = require('connect');
var MongoStore = require('connect-mongo')(connect);

2013年7月17日 星期三

那年不怕死的冒險


離開了服務近三年的公司
要說沒有不捨是騙人的
從 App 開發一路到了系統, 甚至是 server side 開發
以及最後整套考試系統的規劃與開發
兩年多來成長了不少

但攻略迷宮
總有走完的一天
沒走透也許可惜
但新的地圖還是會出現在眼前
展開新的冒險吧


今天也是與小天使第一次約會的紀念日
我會記得承諾
讓妳在天上也看的見的

不管外人如何數落抹黑
作自己就對了

2013年7月3日 星期三

Nginx 設定檔相關說明

Setting

主要設定檔位置: /etc/nginx/nginx.conf
引入設定檔路徑: include /etc/nginx/conf.d/*.conf
伺服器設定檔: /etc/nginx/conf.d/default.conf

nginx.conf 設定檔內容

user              nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 1048576;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;

events {
    use   epoll;
    worker_connections  1048576;
}

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

    default_type  application/octet-stream;


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

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

    include /etc/nginx/conf.d/*.conf;

    upstream node_server {
        server 192.168.1.1:3000 weight=2;
        server 192.168.1.1:3001 weight=1;
    }
}

設定說明:

>>> user        nginx;
nginx 為使用者名稱
>>> worker_processes  8;
需要開啟的 worker 數量,盡量以核心數為設定標準
>>> worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
綁定每個 worker process 給 CPU 核心
>>> worker_rlimit_nofile 1048576;
要和系统單一 process 的 open file 數量一致
Note
The Linux kernel has a hard upper limit of 1024*1024
>>> error_log  /var/log/nginx/error.log;
錯誤紀錄檔 參考
>>> pid        /run/nginx.pid;
Pid文件位置

event 區塊

>>> use epoll;
epoll 是 I/O Multiplexing 中的一種方式, 用於linux kernel 2.6以上,可提高nginx的效能
>>> worker_connections  1048576;
每個 worker 的大連接數,理論上每台 nginx 伺服器的最大連接數為 worker_processes * worker_connections

http 區塊

>>> include       /etc/nginx/mime.types;
設定MIME, 由mime.type 檔案來定義
>>> default_type  application/octet-stream;
設定預設的MIME类型
>>> log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
設定紀錄檔的格式
>>> access_log  /var/log/nginx/access.log  main;
設定 access 紀錄檔
>>> sendfile        on;
開啟 sendfile(), 指定Nginx 使用 kernel 的 sendfile 函數來提高 web 傳輸文件的效率,一般設為 on,如果用來進行下載等應用 磁碟IO 重負載的應用,可設為 off,以平衡磁碟與網路 I/O 處理速度,降低系統的負載。 P.S.如果圖片顯示不正常把這個改成off
>>> tcp_nopush      on;
開啟 TcpNopush(TCP_CORK), 採用 Nagle 算法較小的 packet 存入緩衝區直到蓄足一個 frame 後一起發送,這樣可以最小化所發送的 message 的數量來提高應用程序的效率,用以縮減網路傳輸過程中的封包數量,並減輕網絡擁塞問題。 可參考 TcpNopush
>>> keepalive_timeout 65 5;
第一個參數指定 Client 端連接保持活動的限制時間,在這個時間之後,Server 會關掉連接,第二個參數是可選的,它指定了消息頭保持活動的有效時間,即響應中的 timeout=time,它可以告訴某些瀏覽器關閉連接,因此服務器就不必關閉連接了,如果沒有這個參數,Nginx不會發送Keep-Alive頭。
>>> gzip  on;
開啟gzip壓縮 參考資料: GzipModule
>>> include /etc/nginx/conf.d/*.conf;
讀取來自於 /etc/nginx/conf.d 資料夾的 config 檔案, 預設的 Server 設定在 conf.d/default.conf
>>> upstream node_server {
        server 192.168.1.146:3000 weight=2;
        server 192.168.1.146:3001 weight=1;
    }
開啟負載平衡, weight 是用來設定 server 分配權重

Nginx 安裝

Nginx

輕量級的web server, reverse proxy server 與apahce & IIS 角色相同
epoll 非同步non-blocking I/O
下面為Linux下的安裝方式

Install

Fedora 17
yum install nginx
CentOS 6
vim /etc/yum.repos.d/nginx.repo
nginx.repo >
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

NodeJS Production Depoly (1)

最近公司的專案要開始Depoly
從原本的測試環境改為production 

目前用到以下技術:
Nginx: 負責Loadblance
NodeJS: 撰寫後端應用
MongoDB: 儲存Session與服務的資料

NodeJS 用到的Module:
connect-mongo: 存session資料
expressjs: 網站Framework
i18n: 多國語系
moment: 處理日期時間
mongoskin: 連結mongoDB
underscore: 前後端都好用的JS Library (針對Flow Control也有用到)

因應多核心主機使用了 cluster


整體架構圖