天天看點

如何開發一個産品級的Node.js 應用

介紹

node.js是一個開源的javascript運作時環境。非常簡單可以快速開發一個網絡應用。這個平台運作在linux、osx和windows,而且運作在這個平台上的應用都是用javascript寫的。node.js的應用可以使用指令行啟動,但是這裡我們主要介紹的是作為一個服務運作,這樣在電腦重新開機或者出問題之後可以重新運作起來,這是在産品環境下需要的。

本文會介紹如何使用兩台ubuntu14.04伺服器建立一個node.js的産品環境。一個伺服器運作pm2管理下的node.js應用。另一台運作反向代理nginx,使用者可以通過這個代理通路應用伺服器。

前提

這兩台伺服器使用内網連接配接。在本文中我們會分别稱這兩台伺服器為:

app:我們運作node.js運作時,你的node.js應用和pm2的伺服器.

web:這台伺服器安裝nginx作為反向代理。使用者可以通過ip或者域名通路你的node.js應用。

當然也可以隻用一台伺服器實作上述的功能。隻不過你需要做一些改動。比如,使用localhost ip位址127.0.0.1。

這裡隻介紹實作的技術,如果你需要你的應用可以在公網使用者使用域名通路的話,你需要先購買一個域名。這些文中不做詳細叙述。

安裝node.js

使用homebrew包管理工具安裝nodejs。homebre本來是為mac服務的。後來被移植到了linux上,叫做linuxbrew。可以用來安裝大部分的開源軟體,比如nodejs。而且非常簡單,隻需要一句:

1

<code>brew install node</code>

當然這這錢你需要安裝ruby。linuxbrew安裝之後你需要添加下面的三行到.bashrc或者.zshrc檔案中。

2

3

<code>export path=</code><code>"$home/.linuxbrew/bin:$path"</code>

<code>export manpath=</code><code>"$home/.linuxbrew/share/man:$manpath"</code>

<code>export infopath=</code><code>"$home/.linuxbrew/share/info:$infopath"</code>

使用brew install node指令之後,測試一下nodejs是否成功安裝。在terminal中輸入node -v這個時候應該會有nodejs的版本列印出來。再輸入指令:npm -v,這次列印出來的是npm得版本。

hello world

下面就可以開始寫你的nodejs應用了。随便找一個文本編輯器就可以編寫nodejs代碼。這裡推薦webstom。是一個靠譜的ide。在檔案中添加如下的代碼:

4

5

6

<code>var</code> <code>http = require(</code><code>'http'</code><code>);</code>

<code>http.createserver(</code><code>function</code> <code>(req, res) {</code>

<code>  </code><code>res.writehead(200, {</code><code>'content-type'</code><code>: </code><code>'text/plain'</code><code>});</code>

<code>  </code><code>res.end(</code><code>'hello world\n'</code><code>);</code>

<code>}).listen(&lt;strong&gt;&lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;8080&lt;/span&gt;&lt;/strong&gt;, </code><code>'&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;app_private_ip_address&lt;/span&gt;&lt;/strong&gt;'</code><code>);</code>

儲存為hello.js。請确定使用app伺服器的ip位址代替app_private_ip_address。如果你願意可以代替8080端口,但是最好使用大于1024的端口。app伺服器監聽的是我們給定的ip位址和端口。這個時候app伺服器隻能被同一個區域網路的其他伺服器通路到,比如web伺服器。如果通路的話,app會傳回一個200的http通路成功的code。

測試一下

<code>node hello.js</code>

運作起來後,terminal将不會再執行其他的指令,除非你按下ctrl+c。

要測試你的app,可以直接打開浏覽器通路你的位址,也可以使用curl指令:

<code>curl http:</code><code>//&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;app_private_ip_address&lt;/span&gt;&lt;/strong&gt;:&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;8080</code>

<code>&lt;/span&gt;&lt;/strong&gt;</code>

如果terminal中出現了hello world,那麼你的代碼是正确的。最後記得停掉應用的運作。按下ctrl+c。

安裝pm2

pm2是一個node.js應用的程序管理工具。pm2給了一個簡單的方法來管理和虛拟化應用(把它們作為服務運作)。

我們使用npm(node packaged modules,nodejs的包管理工具)來安裝pm2。指令:

<code>sudo npm install pm2 -g</code>

使用pm2管理應用

這裡會講到一些基本的pm2用法。

啟動應用

第一個你需要知道的就是使用pm2 start指令運作你的應用hello.js。

<code>pm2 start &lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;hello.js</code>

<code>&lt;/span&gt;</code>

這個指令也會把你的應用添加到pm2得程序清單中。這個清單在你每次運作一個應用的時候都會列印出來。

<code>┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬────────────┬──────────┐</code>

<code>│ app name │ id │ mode │ pid  │ status │ restarted │ uptime │     memory │ watching │</code>

<code>├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼────────────┼──────────┤</code>

<code>│ hello    │ 0  │ fork │ 5871 │ online │         0 │ 0s     │ 9.012 mb   │ disabled │</code>

<code>└──────────┴────┴──────┴──────┴────────┴───────────┴────────┴────────────┴──────────┘</code>

pm2會自動根據你的應用的檔案名來命名app name。并給你的應用添加一個pm2 id。pm2也會有一些其他的資訊,比如:程序的pid,目前的狀态和記憶體使用量。

pm2中運作的應用會在crash或者被kill掉的時候自動重新開機,但是還是需要設定應用在系統重新開機的時候自動啟動。pm2提供了startup指令,可以很容易做到這一點。

startup指令會生成并配置一個腳本。這個腳本會在系統啟動的時候運作pm2和pm2中托管的應用。但是你必須在指令中明确指定運作的平台是什麼,比如ubuntu等。

<code>pm2 startup ubuntu</code>

這個指令執行的結果為:

<code>[pm2] you have to run </code><code>this</code> <code>command as root</code>

<code>[pm2] execute the following command :</code>

<code>[pm2] &lt;strong&gt;&lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;sudo env path=$path:/usr/local/bin pm2 startup ubuntu -u sammy</code>

運作最後的紅色高亮的指令,這樣pm2就會成為這個伺服器的開機啟動項。  

其他的pm2用法

pm2還提供了很多的指令。你可以使用這些指令檢視你的應用的資訊。

隻運作pm2,會再terminal中列印出來一個幫助頁。其中會包括使用的示例。

停止一個應用:

<code>pm2 stop &lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;example</code>

重新開機一個應用:

<code>pm2 restart &lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;example</code>

目前被pm2托管的應用清單:

<code>pm2 list</code>

檢視一個應用的更多資訊:

<code>pm2 info &lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;example</code>

使用monit指令可以檢視應用的狀态,cpu和記憶體使用情況:

<code>pm2 monit</code>

  

建立反向代理

因為我們已經有了linuxbrew了,那麼安裝什麼都很簡單的一個指令就可以輕松搞定:

<code>brew install nginx</code>

這個會花一定的時間安裝nginx的依賴項。安裝好之後執行指令:

<code>sudo nginx</code>

現在可以用curl指令試試了:

<code>curl http:</code><code>//localhost:8080</code>

如果安裝正确會輸出:

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<code>&lt;!</code><code>doctype</code> <code>html&gt;</code>

<code>&lt;</code><code>html</code><code>&gt;</code>

<code>&lt;</code><code>head</code><code>&gt;</code>

<code>&lt;</code><code>title</code><code>&gt;welcome to nginx!&lt;/</code><code>title</code><code>&gt;</code>

<code>&lt;</code><code>style</code><code>&gt;</code>

<code>    </code><code>body {</code>

<code>        </code><code>width: 35em;</code>

<code>        </code><code>margin: 0 auto;</code>

<code>        </code><code>font-family: tahoma, verdana, arial, sans-serif;</code>

<code>    </code><code>}</code>

<code>&lt;/</code><code>style</code><code>&gt;</code>

<code>&lt;/</code><code>head</code><code>&gt;</code>

<code>&lt;</code><code>body</code><code>&gt;</code>

<code>&lt;</code><code>h1</code><code>&gt;welcome to nginx!&lt;/</code><code>h1</code><code>&gt;</code>

<code>&lt;</code><code>p</code><code>&gt;if you see this page, the nginx web server is successfully installed and</code>

<code>working. further configuration is required.&lt;/</code><code>p</code><code>&gt;</code>

<code>&lt;</code><code>p</code><code>&gt;for online documentation and support please refer to</code>

<code>commercial support is available at</code>

<code>&lt;</code><code>p</code><code>&gt;&lt;</code><code>em</code><code>&gt;thank you for using nginx.&lt;/</code><code>em</code><code>&gt;&lt;/</code><code>p</code><code>&gt;</code>

<code>&lt;/</code><code>body</code><code>&gt;</code>

<code>&lt;/</code><code>html</code><code>&gt;</code>

打開nginx.conf:

<code>vi /usr/local/etc/nginx/nginx.conf</code>

把檔案個的東西全部删掉,之後添加如下代碼。記得在server_name項後面填寫你自己的域名(或者ip位址,如果你沒有域名的話)。并把app_private_ip_address替換為app伺服器的位址。如果你已經在上面的nodejs代碼中修改了端口号,那麼這裡也需要修改為同樣的端口号。

<code>server {</code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name &lt;strong&gt;&lt;span style=</code><code>"color: #ff0000;"</code><code>&gt;example.com&lt;/span&gt;&lt;/strong&gt;;</code>

<code>    </code><code>location / {</code>

<code>        </code><code>proxy_pass http:</code><code>//&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;app_private_ip_address&lt;/span&gt;&lt;/strong&gt;:&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;8080&lt;/span&gt;&lt;/strong&gt;;</code>

<code>        </code><code>proxy_http_version 1.1;</code>

<code>        </code><code>proxy_set_header upgrade $http_upgrade;</code>

<code>        </code><code>proxy_set_header connection </code><code>'upgrade'</code><code>;</code>

<code>        </code><code>proxy_set_header host $host;</code>

<code>        </code><code>proxy_cache_bypass $http_upgrade;</code>

<code>}</code>

這一配置會使web伺服器對根目錄下得通路有response。假設我們的伺服器可以用example.com通路,在浏覽器中輸入http://example.com的話,請求最終會發送到appserver的内網ip位址端口号為8080(或者你設定的端口号)。最後node.js應用會傳回輸出到使用者浏覽器。

你可以在同一個server塊中繼續添加另外的location塊。這樣在同一個web伺服器中可以有多個node.js應用被通路到。比如,你有一個叫做app的應用,運作在端口8081上。你可以添加如下的location塊來允許外界使用http://example.com/app2通路。

<code>location /app2 {</code>

<code>    </code><code>proxy_pass http:</code><code>//app_private_ip_address:8081;</code>

<code>    </code><code>proxy_http_version 1.1;</code>

<code>    </code><code>proxy_set_header upgrade $http_upgrade;</code>

<code>    </code><code>proxy_set_header connection </code><code>'upgrade'</code><code>;</code>

<code>    </code><code>proxy_set_header host $host;</code>

<code>    </code><code>proxy_cache_bypass $http_upgrade;</code>

添加完成之後,儲存并退出。

重新開機nginx:

<code>sudo service nginx restart</code>

假設你的應用已經在運作,并且你的應用niginx的配置也都是正确的。你的應用就可以被外網通路到。

最後

你的node.js應用已經運作在nginx反向代理下了。nginx可以靈活設定,保證你的使用者可以通路到你的全部應用或者靜态web内容。

原文位址:https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/p/4476522.html

繼續閱讀