Download - 0501n sytem 1 google otros productos o
Node.JS Website
DeploymentpH200
Software Developer @ Cytisan Software
今天我們可以學到
• 如何把做好的node.js website放到自己的server上跑
• 而且是用git deploy
me
• 第一台筆電是iBook G4
• 雖輾轉學過各種語言但樣樣不精,最後覺得C#還不賴,一年前開始研究JS
• 開了間小公司弄些Windows的App
• 把公司網站擺在Heroku (node)的免費quota下白吃白喝
• 因為用node擺網站也就順理成章開始學習node環境
Cloud Computing
PaaS
• 這些服務很好• 但不是今天的重點
IaaS, VPS, etc.
Deploy node.js app 到自己的機器
• 今天的重點
啊不是複製貼上就好了嗎?
啊不是複製貼上就好了嗎?
• 其實我覺得也可以
• 整合 version control (push deploy, rollback)
• Start, stop and monitor
• 自動化
Let’s 複製貼上
• rsync
• rsync -arzve ssh username@host:path source
• 更新:ssh進去,stop,start node
• 當然可以寫成shell script
• 人有失手馬有亂蹄
Forever
• https://github.com/nodejitsu/forever
• Nodejitsu出品
• 已經有hosting platform實際使用
• npm install -g forever
• 讓你的node application永不停止
• 記住你的env variables
Git Deployment
• 手動Heroku式Deploy
• 設定有點煩,但一勞永逸
1. ssh進server
2. git init --bare
3. vim hooks/post-receive
4. chmod +x hooks/post-receive
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
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)
設定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
設定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
設定Git(遠端)
7. chmod +x post-receive
8. mkdir ~/nodeweb
9. 遠端設定完成,回到本機
設定Git
(在自己的工作用git repository)
1. git remote add cloud-deploy
[email protected]:/home/webadmin/nodeweb.git
2. 設定完成
Git Deploy(第一次)
1. git push cloud-deploy master
2. ssh [email protected] "forever start ~/nodeweb/app.js"
3. 網站上線
Git Deploy(之後每一次)
1. git push cloud-deploy master
2. 網站會自動restart update
How about Windows?
Windows (正經)
• IIS
• iisnode (強力推薦)https://github.com/tjanczuk/iisnode
• Windows Azure
• AppHarbor
• Hyper-V + Linux (CentOS, Ubuntu >=12)
延伸閱讀
• 管理你的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
Questions?
twitter: pH_minamo (可叫我200 or pH)
Node.js 台灣 Facebook Group:http://www.facebook.com/groups/node.js.tw/