node.js website deployment

of 25 /25
Node.JS Website Deployment pH200 Software Developer @ Cytisan Software

Upload: minamo-ph

Post on 26-Aug-2014

2.499 views

Category:

Self Improvement


3 download

DESCRIPTION

用 git 做 Node.JS 網站佈署

TRANSCRIPT

Page 1: Node.JS Website Deployment

Node.JS Website

DeploymentpH200

Software Developer @ Cytisan Software

Page 2: Node.JS Website Deployment

今天我們可以學到

• 如何把做好的node.js website放到自己的server上跑

• 而且是用git deploy

Page 3: Node.JS Website Deployment

me

• 第一台筆電是iBook G4

• 雖輾轉學過各種語言但樣樣不精,最後覺得C#還不賴,一年前開始研究JS

• 開了間小公司弄些Windows的App

• 把公司網站擺在Heroku (node)的免費quota下白吃白喝

• 因為用node擺網站也就順理成章開始學習node環境

Page 4: Node.JS Website Deployment

Cloud Computing

Page 5: Node.JS Website Deployment

PaaS

• 這些服務很好• 但不是今天的重點

Page 6: Node.JS Website Deployment

IaaS, VPS, etc.

Page 7: Node.JS Website Deployment

Deploy node.js app 到自己的機器

• 今天的重點

Page 8: Node.JS Website Deployment

啊不是複製貼上就好了嗎?

Page 9: Node.JS Website Deployment

啊不是複製貼上就好了嗎?

• 其實我覺得也可以

• 整合 version control (push deploy, rollback)

• Start, stop and monitor

• 自動化

Page 10: Node.JS Website Deployment

Let’s 複製貼上

• rsync

• rsync -arzve ssh username@host:path source

• 更新:ssh進去,stop,start node

• 當然可以寫成shell script

• 人有失手馬有亂蹄

Page 11: Node.JS Website Deployment

Forever

• https://github.com/nodejitsu/forever

• Nodejitsu出品

• 已經有hosting platform實際使用

• npm install -g forever

• 讓你的node application永不停止

• 記住你的env variables

Page 12: Node.JS Website Deployment

Git Deployment

• 手動Heroku式Deploy

• 設定有點煩,但一勞永逸

1. ssh進server

2. git init --bare

3. vim hooks/post-receive

4. chmod +x hooks/post-receive

Page 13: Node.JS Website Deployment

post-receive example

#!/bin/bash

APP=~/repos/app/app.js

export GIT_WORK_TREE=~/repos/app

git checkout -f

pushd $GIT_WORK_TREE

npm install

forever restart $APP

popd

Page 14: Node.JS Website Deployment

Step by Step

• 範例前置設定:• Local Repository名稱為nodeweb

• Remote IP為5.5.6.6

• Remote User為webadmin

• 把Remote Repository放在/home/webadmin/nodeweb.git

• Remote端已安裝forever (npm install -g forever)

Page 15: Node.JS Website Deployment

設定Git

1. 在local端做好git repository (git init)

2. ssh [email protected]

3. git init --bare nodeweb.git

(註:新增在/home/webadmin/nodeweb.git)

4. cd nodeweb.git/hooks

5. vim post-receive

Page 16: Node.JS Website Deployment

設定Git(post-receive內容)

6. #!/bin/bash

APP=~/nodeweb/app.js

export GIT_WORK_TREE=~/nodeweb

git checkout -f

pushd $GIT_WORK_TREE

npm install --production

forever restart $APP

popd

Page 17: Node.JS Website Deployment

設定Git(遠端)

7. chmod +x post-receive

8. mkdir ~/nodeweb

9. 遠端設定完成,回到本機

Page 18: Node.JS Website Deployment

設定Git

(在自己的工作用git repository)

1. git remote add cloud-deploy

[email protected]:/home/webadmin/nodeweb.git

2. 設定完成

Page 19: Node.JS Website Deployment

Git Deploy(第一次)

1. git push cloud-deploy master

2. ssh [email protected] "forever start ~/nodeweb/app.js"

3. 網站上線

Page 20: Node.JS Website Deployment

Git Deploy(之後每一次)

1. git push cloud-deploy master

2. 網站會自動restart update

Page 21: Node.JS Website Deployment

How about Windows?

Page 22: Node.JS Website Deployment

Windows (正經)

• IIS

• iisnode (強力推薦)https://github.com/tjanczuk/iisnode

• Windows Azure

• AppHarbor

• Hyper-V + Linux (CentOS, Ubuntu >=12)

Page 23: Node.JS Website Deployment

Uptime 監控

• https://github.com/fzaninotto/uptime

Page 24: Node.JS Website Deployment

延伸閱讀

• 管理你的processhttp://upstart.ubuntu.com/

• 網路上的文章談git deployhttp://sebduggan.com/blog/deploy-your-website-changes-using-git/

• Nodejistu Blog介紹Foreverhttp://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever

• 設定ssh key loginhttp://www.linuxproblem.org/art_9.html

• 設定git serverhttp://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server

Page 25: Node.JS Website Deployment

Questions?

twitter: pH_minamo (可叫我200 or pH)

Node.js 台灣 Facebook Group:http://www.facebook.com/groups/node.js.tw/