Privacy Terms

Who We Are At Walk In The Cloud, we are committed to maintaining the trust and confidence of all visitors to our web site. In particular, we want you to know that Walk In The Cloud is not in the business of selling, renting or trading email lists with other companies and businesses for marketing purposes.  In this Privacy Policy, we’ve provided detailed information on when and why we collect personal information, how we use it, the limited conditions under which we may disclose it to others, and how we keep it secure.  We take your privacy seriously and take measures to provide all visitors and users of Walk In The Cloud with a safe and secure environment. Cookies  Walk In The Cloud may set and access Walk In The Cloud cookies on your computer.  Cookies are used to provide our system with the basic information to provide the services you are requesting.  Cookies can be cleared at any time from your internet browser settings.  Google Analytics When someone visits Walk In The Cloud we us

NodeJS學習筆記(一): 環境安裝

大致了解NodeJS運作概念,便開始安裝主機!

參考NodeJS入門資料: http://www.nodebeginner.org/index-zh-tw.html#hello-world

目前的開發環境OS是Ubuntu14.04,搭配MongoDB 與 Express。

安裝流程參考Digital Ocean的神文,只要剪下貼上按部就班就可以使用了。

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-
ubuntu-14-04-server

安裝MongoDB:

https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-14-04

===============================

安裝完後,重開mongo db竟然發現開不開?!(service mongod start 跑了但去status看卻是stop的)

出現下方的錯誤log,發現應該是資料夾權限問題。


E NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted

前往/tmp資料夾修改權限,就可以正常跑起來了!

sudo chown root:root /tmp
sudo chmod 777 /tmp


首次使用mongodb著實不知道怎麼下手,

sudo service mongod start 跑起來後,下指令 mongo 即可進入。

要記得確保資料夾在本機存在且權限正確,不然mongodb跑不起來。

ls -ld /data/db/
sudo chmod 0755 /data/db

跑起來後為了安全,新增一下系統使用者。(預設mongodb是沒有做使用者權限的,有點危險)

進入mongo後,用以下語法切換到admin資料表,再新增系統使用者:
use admin
db.createUser(
  {
    user: "xxx",
    pwd: "xxx",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

新增一個想要連接的db:(在mongo db中,use會直接判斷存在否,不存在直接新增一個)
use log

以上大致完成mongodb的安裝。

===============================

因為要使用MongoDB,安裝了一套Mongo Express,方便網頁檢視資料庫,有點類似phpmyadmin的作用。

下安裝指令


npm install mongo-express

卻回傳了


npm WARN engine mongo-express@0.31.0: wanted: {"node":">=4.0.0","npm":">=3.0.0"} (current: {"node":"0.10.46","npm":"2.15.1"})

內心OS:不是有安裝6.4版本了嗎?orz

原來是因為搞混node指令與nodejs指令。

這兩個是不同的東西~

node -v 看到的是 nvm (nodejs server) 的版本。

nodejs -v 看到的是nodejs的版本。


所以,再用以下指令更新nodejs版本就可以順利安裝了。

curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs

安裝好後,需設定mongo express的設定檔。

cd node_modules/mongo-express
vim config.default.js

裡面有兩個部分要改:

1. mongo db user與資料庫設定

mongo = {
    db:       'log',
    host:     'localhost',
    password: 'xxx',
    port:     27017,
    ssl:      false,
    url:      'mongodb://localhost:27017/log',
    username: 'xxx',

  };

2. 連接網頁位置 (Web網址或IP),預設是localhost,如果不在localhost連接請務必修改這部分,不然會access不到~


host:   process.env.VCAP_APP_HOST    || '192.168.0.209'  

設定完成後,直接在該資料夾下

node app.js

就會出現成功字樣,恭喜您跑起來啦!(淚奔...)


Welcome to mongo-express
------------------------


Mongo Express server listening at http://192.168.0.209:8081
Database connected
Connecting to log...
Database log connected

網頁端會長成這樣:




最後,為了讓機器重開會自動跑起來。

安裝pm2讓node js服務能自動化跑起來。

sudo npm install pm2 -g


pm2 start app.js

以上指令等同於 node app.js 可執行起mongo express。

備註:如果想多執行序 (Multi-Core可參考:PM2 多核心Cluster實作方式分享)

sudo env PATH=$PATH:/usr/local/bin pm2 startup -u someuser

設定自動開機檔案,讓該執行緒在重啟的時候會自動重啟。

(參考自:https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps)



接著將mongodb 啟動指令放到rc.local中,讓mongodb也會自動重啟。

sudo vim /etc/rc.local

service mongod start


留言