免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2014年11月26日 星期三

nodejs + heroku

0. launch git bash from the working folder of nodejs project, say mynjs1
  $ heroku login

1.  initialize git repository
   $ git init
   $ git add .
   $ git commit -m "initialize mynjs1"

2.  create the corresponding app on heroku
   $ heroku create --http-git
   [   $ heroku apps:create --http-git  ]
   https://infinite-lake-5143.herokuapp.com/
   https://git.heroku.com/infinite-lake-5143.git
...
   $ git push heroku master


3. Ensure that at least one instance of the app is running by :
   $ heroku ps:scale web=1

4.  visit the app at the URL generated by its app name.
   $ heroku open  [ or open it by browser]

5. view log
   $ heroku logs --tail
   Press Control+C to stop streaming the logs


6.  create package.json by
   $ npm init

7.  Procfile
   web: node httpsvr1.njs

8. rename app
   $ heroku apps:rename mynjs2

9.  run bash on heroku
   $ heroku run bash

10.
$  heroku config:set TIMES=4

11. revise files, git add, git commit, git push
httpsvr.listen(8080)  --> httpsvr.listen(process.env.PORT)



[ref]
https://devcenter.heroku.com/articles/getting-started-with-nodejs


[err]
ssh: connect to host  port 22: Bad file number
fatal: Could not read from remote repository.
-->

//=== http://stackoverflow.com/questions/16436284/failing-to-push-git-to-heroku-port-22-bad-file-number-could-not-read-from-remot


""" ...
if unable to access Heroku on port 22, then HTTP Git should solve it (it works on port 443). ...

To create a new app and have it be configured with a HTTP Git remote...
$ heroku apps:create --http-git

To change an existing app from SSH to HTTP Git ...
$ heroku git:remote --http-git

... """

2014年11月25日 星期二

install nodejs on ubuntu


//=== https://github.com/joyent/node/wiki/Installation
"""...
You can install a pre-built version of node.js via the downloads page available in a .tar.gz.
..."""

$ sudo apt-get remove nodejs

$ wget http://nodejs.org/dist/v0.12.0/node-v0.12.0-linux-x64.tar.gz
$ sudo tar -C /usr/local/ --strip-components=1 -zxvf node-v0.12.0-linux-x64.tar.gz
$ sudo ln -s /usr/local/bin/node /usr/bin/node
$ node --version
$ npm --version [newer nodejs should already include npm in its package]






//===
1.  quick way to install nodejs on ubuntu
$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo apt-get install npm

$ mkdir ~/nodejs
$ cd ~/nodejs
$ npm install node-static
-->  ~/nodejs/node_modules/node-static
...


2.  build from source

//=== http://iconof.com/blog/how-to-install-setup-node-js-on-amazon-aws-ec2-complete-guide/#installNode

$ git clone git://github.com/joyent/node.git
$ cd node

$ git tag -l
[ view available Node tags (versions)  ]

$ git checkout v0.6.8
$ ./configure
$ make      [需要30分鐘以上?]
$ sudo make install
[ how to change name to nodejs to avoid conflict ?]


[ install NPM(Node package manager) ]
$ git clone https://github.com/isaacs/npm.git
$ cd npm
???
$ sudo make install





如何讓 nodejs 與 apache 共用 port 80 ?

[Q] nodejs 無法在port 80 執行?
[Q] why does nodejs listening to port 80 cause error  :listen EACCES?

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1020:19)
    at listen (net.js:1061:10)


""" ...
The error code EACCES means you don't have proper permissions to run applications on that port.
On Linux systems, any port below 1024 requires root access.

... """

[ref]
http://stackoverflow.com/questions/18947356/node-js-app-cant-run-on-port-80-even-though-theres-no-other-process-blocking-t


[try]
0. run nodejs with sudo,  --> 恐有資安風險?
  $ sudo nodejs httpsvr.njs

1. use apache as reverse proxy  --> 效能較弱?
[ need to install/enable apache proxy, proxy_http module ]
 $ sudo a2enmod proxy
 $ sudo a2enmod proxy_http

<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyRequests Off

#ProxyPreserveHost on
ProxyPass / http://localhost:3000/
</VirtualHost>

""" ...
Every request that comes in through Apache will cause an Apache thread to
wait/block until the response is returned from your Node.js process.

... """

2. use nodejs package http-proxy to redirect port 80 traffic
-->  http-proxy 尚未穩定?
--> still needs sudo to run the proxy listening to port 80?
$ npm install  http-proxy

myproxy.njs

var http = require('http');
httpProxy = require('http-proxy');
proxy = httpProxy.createProxy(options);
var options={
    hostnameOnly: true,
    router: {
        'urdomain.com':       'http://127.0.0.1:1001',
        'nodejs.urdomain.com':     'http://127.0.0.1:1002',
        '127.0.0.1':        'http://127.0.0.1:1003'
    }
};

http.createServer(function(req, resp) {
  proxy.web(req, resp, {
    target: options.router[req.headers.host]
  }).listen(80);

xxx target : '127.0.0.1:1001'
--> target: 'http://127.0.0.1:1001'

--> 'nodejs.urdomain.com':     'http://127.0.0.1:1002',
run the actual httpsvr.njs on localhost at port 1002

3. use Nginx as reverse proxy ...

[ref]
http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://stackoverflow.com/questions/11172351/how-to-put-nodejs-and-apache-in-the-same-port-80

http://blog.nodejitsu.com/http-proxy-intro/



2014年11月24日 星期一

favicon, png, ico

*** ico may contain mutliple resolutions(multiple layers) of image.
*** IE 11 has to operate in compatibilty view mode to show favicon correctly !?

$ convert /?

$ convert srcImg.png -background transparent -colors 256 fav16.ico

$ convert srcImg64.png -bordercolor white -border 0
( -clone 0 -resize 16x16 )   ( -clone 0 -resize 32x32 )
( -clone 0 -resize 48x48 )  ( -clone 0 -resize 64x64 )
-delete 0  -alpha off -colors 256  fav4in1.ico

$ convert favicon-256.png -resize 16x16 favicon-16.png
$ convert favicon-256.png -resize 32x32 favicon-32.png
$ convert favicon-256.png -resize 64x64 favicon-64.png
$ convert favicon-256.png -resize 128x128 favicon-128.png
$ convert favicon-16.png favicon-32.png favicon-64.png favicon-128.png favicon-256.png -colors 256 favicon.ico

$ convert ico64.png -resize 16x16 -transparent white fav16.png
$ convert ico64.png -resize 32x32 -transparent white fav32.png
$ convert fav16.png -background transparent -colors 256 fav16.ico
$ convert fav16.png fav32.png -colors 256 5gen2in1.ico

[ref]
https://gist.github.com/pfig/1808188


//=== http://www.jonathantneal.com/blog/understand-the-favicon/
32×32 icon is used in the address bar on IE10 Metro ?

<!-- IE6-10 -->
<link rel="shortcut icon" href="path/to/favicon.ico">

<!-- Everybody else -->
<link rel="icon" href="path/to/favicon.ico">


PNG favicon files do not include multiple resolutions like ICO favicons
-->
<link rel="icon" href="favicon-16.png" sizes="16x16">
<link rel="icon" href="favicon-32.png" sizes="32x32">
<link rel="icon" href="favicon-48.png" sizes="48x48">
<link rel="icon" href="favicon-64.png" sizes="64x64">
<link rel="icon" href="favicon-128.png" sizes="128x128">




File Shortcut and Symbolic Link in Windows

//===
File shortcut [檔案捷徑/shell link] :  副檔名 .lnk / .LNK
通稱 shortcut / link file

根據wikipedia:
"a small file containing a target URI or GUID to an object, or
the name of a target program file ..."

//===  Symbolic link
> mklink   linkname  targetFileName            [file symbolic link]
> mklink  /D  linkname  targetDirName        [directory symbolic link ]


> mklink /?
MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      Creates a directory symbolic link.
        /H      Creates a hard link instead of a symbolic link.
        /J       Creates a Directory Junction.
        Link    specifies the new symbolic link name.
        Target  specifies the path (relative or absolute) that the new link
                refers to.

//===
IShellLink
IPersistFile
IPersistSystem


[ref]
http://en.wikipedia.org/wiki/File_shortcut

shell link spec:
http://msdn.microsoft.com/en-us/library/dd871305(v=prot.13).aspx
http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/[MS-SHLLINK].pdf



2014年11月13日 星期四

EC2 elastic ip

網路搜尋 elastic ip charge 之後
終於了解 amazon EC2 elastic ip 與 public ip 的差異

將一個elastic ip 指定給某個 EC2 running instance i-123c484c,
(綁在一起, to associate an elastic ip to an EC2 running instance)
停掉 instance i-123c484c 再啟動, 這個 elastic ip 不會改變;
若採用public ip , 在instance重新啟動時會動態取得一個IP , 很有可能與原來的不同.
-->
* elastic ip == fixed external ip
* public ip == dynamic external ip

[pricing]
Amazon 目前提供一個免費的 elastic ip, 前提是它必須與某個running instance 綁在一起(assoicate elastic ip with instance),
也就是透過running instance 的 流量/CPU/Storage/ ...來收費.



*** 向系統要求分配了elastic ip後, 卻未指定給某個running instance,  Amazon 將酌收費用 ...

*** 為了避免用戶的損失與資源的有效利用, Amazon 鼓勵用戶盡快釋出閒置的elastic ip   (release elastic ip)

*** 一個 elastic ip 可以在不同的running instances切換, dis-associate with one then associate with another.


[ref]
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-
eip.html

http://aws.amazon.com/ec2/pricing/

2014年11月10日 星期一

ssh bitbucket.org by GitBash

//=== https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git

0. launch GitBash
1. chk OpenSSH/OpenSSL are installed
$ ssh -v
   OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007 ...

$ cd    //go back user home folder
$ pwd
$ cd .ssh
[ $ cd ~/.ssh ]


$ ls
id_rsa    id_rsa.pub  known_hosts

* id_rsa is the private key
* id_rsa is the public key
* id  --> identifier for short

2. $ ssh-keygen

[ 3.  notepad ~/.ssh/config , 預設的組態檔
加入
Host bitbucket.org
 IdentityFile ~/.ssh/id_rsa

"...
The second line is indented.
That indentation (a single space) is important, so make sure you include it.
..."
IdentityFile 的那一行開頭必須有一個空白, 之後緊接著IdentityFile  path-to-privatekey

]

4.  $ ssh-add -l
Could not open a connection to your authentication agent
-->
need to start ssh-agent before you run the ssh-add command:

$ eval `ssh-agent -s`  # need to use eval instead of just ssh-agent
$ ssh-add path-to-target-privatate-key

path-to-target-privatate-key 代表密鑰檔案路徑
會蓋過預設組態檔的設定
[例] /c/myProjs/sshkeys/privkey-for-bitbucket

5. 將公鑰(.pub)內容複製到 bitbucket.org 帳戶
account settings --> ssh
*** take care not to wrap the lines


6. 更改既存的git config, 將https改為ssh
$ cat .git/config
[core]
  ...
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = https://username@bitbucket.org/teamid/repo1.git
[branch "master"]
  remote = origin
  merge = refs/heads/master

--> url = ssh://git@bitbucket.org/username/repo1.git

https://accountname@bitbucket.org/accountname/reponame.git
-->  ssh://git@bitbucket.org/accountname/reponame.git

https://accountname@bitbucket.org/teamid/reponame.git
-->  ssh://git@bitbucket.org/teamid/reponame.git


//=== 讓 Git Bash 啟動時帶入 ssh-agent
http://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent

1. notepad .bashrc

#!/bin/bash
eval `ssh-agent -s`
ssh-add

ssh-add without paramaters
-->  ~/.ssh/id_rsa

for customized location of the private key
--> ssh-add path-to-private-key


2. Restart GitBash (Msysgit)


//=== ssh-add, ssh-agent
$ man ssh-add
""" ...
-L      Lists public key parameters of all identities currently repre?
             sented by the agent.

-l      Lists fingerprints of all identities currently represented by the
             agent.
... """

$ man ssh-agent
""" ...
     a program to hold private keys used for public key authenti?
     cation (RSA, DSA, ECDSA).  The idea is that ssh-agent is started in the
     beginning of an X-session or a login session, and all other windows or
     programs are started as clients to the ssh-agent program.  Through use of
     environment variables the agent can be located and automatically used for
     authentication when logging in to other machines using ssh(1).
..."""


//===

2014年11月5日 星期三

subdomain alias or subdomain forward?

//=== "subdomain forward" is different from "subdomain alias"
subdomain forward is done by 'A record'
subdomain alias is done by 'CNAME record'

* subdomain alias 會將輸入的域名保留在瀏覽器的網址列
[例]
it.3hd.me 其實是指向 3hitek.blogspot.com
在網址列仍顯示 it.3hd.me

* 典型的subdomain forward (without masking) 網址列則會顯示域名所轉指的url,
[例]  book.3hd.me --> 3hbook.blogspot.com
網址列顯示 3hbook.blogspot.com

* masked subdomain forward (subdomain with masking) 則可達到類似alias的效果, 與CName alias 不同的是即使點選連結到外部的網頁
masked subdomain 的框架(frame/mask) 會一直籠罩著之後的網頁,
網址列始終顯示原來輸入的域名值, 這使得許多搜尋引擎會特意過濾掉
masked subdomain, 因而影響SEO (seach engine optimization).


[例]  tour.3hd.me  --> 3htour.blogspot.com
網址列顯示 tour.3hd.me , 即使點選外部連結, 網址列依然顯示 tour.3hd.me

""" ...
The main disadvantage of this type of forwarding is that is uses Frames.
...
Many search engines, such as Google, ignore framed content, and so your site will not be found easily ...
..."""


* subdomain forward 所增加的 A records
book    A     50.63.202.15  
tour      A    184.168.221.30


* subdomain alias 的設定比較麻煩, cname record並非直接指向目標網址,
而是要知道目標網址的域名伺服器(domain name server) ,
如果要指向既有的blogger(blogspot.com) 或是 google site, 則cname record 的 值要設為 ghs.google.com

[例]
it                  CNAME     ghs.google.com
yyyxxxzzz   CNAME     afjklgjkljajdkturjkj5.dv.googlehosted.com

除了正常的別名設定之外 it --> ghs.google.com ,
google 還會要求增加另一個隨機產生的 cname record 以驗證域名的所有權,
yyyxxxzzz   -->    afjklgjkljajdkturjkj5.dv.googlehosted.com


"""...
For security reasons, your domain must be verified with Google. See the below section, 'Domain is not verified' error ...

..."""



[ref]
http://wiki.gandi.net/en/domains/management/domain-as-website/forwarding

https://support.godaddy.com/help/article/5112/mapping-your-domain-name-to-work-with-blogger
https://support.godaddy.com/help/article/422/manually-forwarding-or-masking-your-domain-name


https://support.google.com/blogger/troubleshooter/1233381
https://support.google.com/sites/answer/99448


//=== https://www.linode.com/docs/networking/dns/introduction-to-dns-records
""" ...
 a CNAME record does not function the same way as a URL redirect.
... """


//=== use subdomain alias binding to backup/spread/aggregate your blogs
運用subdomain alias/forward 將散落各處的部落格集中在自己的網域,
方便存取/管理, 也達到縮址(縮短網址)的效果.


//=== 將自己的 subdomain 指向 github

http://www.openfoundry.org/index.php?option=com_content&task=view&id=9307&Itemid=4;isletter=1

""" ...

用 CNAME 的方式將一組子網域名稱指向你的 Github Pages
...
•將受惠於 Github Pages 站台的 CDN 效益。
•Github Pages 伺服器換 IP 的時候你不必跟著換否則其他人就連不上去。
•網頁載入的速度較快。

... """





//=== https://support.google.com/blogger/troubleshooter/1233381
""" ...
1.Go to your blog and click on Basics under the Settings tab. In the "Publishing" section, click the link to add a custom domain.

Add a custom domain
... """

//=== https://support.godaddy.com/help/article/5112/mapping-your-domain-name-to-work-with-blogger
To Edit Your CNAME Record

"...
1. Log in to your Account Manager.
Next to Domains, click Launch.
2. Click the domain you want to use, and then select the 'DNS Zone File' tab.
3. Click Add Record.
4. Select CNAME (Alias)for the Record type.
....

Click Save, and then click Save Changes.

... """


//===
domain : 網域
domain name : 域名
subdomain: 子網域

registrar : 域名註冊商, 網域代理商,
例如 godaddy.com, name.com, namecheap.com ...