ubuntu安裝nodejs


 

使用apt安裝nodejs,

sudo apt install nodejs  

安裝npm

apt install npm 

源碼安裝 

1.下載Nodejs源碼

http://nodejs.cn/download/

wget -c  資源地址 

-c表示當網絡中斷恢復后斷點續傳

下載后解壓,進入解壓目錄

2.生成Makefile

編譯需要依賴python環境、c、c++

apt install gcc

 

apt install python

./configure --prefix=/usr/local/nodejs/

運行python腳本

3.make -j 4 && sudo make install

安裝make工具

apt install make
apt install make-guile

-j 4 表示用4個線程去編譯

4.配置環境變量

vi ~/.bashrc

修改完后 source ~/.bashrc 使配置生效

env | grep PATH

env顯示環境變量

 

 

啟動node

node

查看nodejs版本

node --version

 

最簡單的http服務

1.使用require引入http模塊

2.創建http服務

3.偵聽端口

server.js

'use strict'

var http = require('http');

var app = http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/plain'});
    res.end('HelloWorld\n');
}).listen(8080,'0.0.0.0');

啟動Nodejs服務

netstat -ntpl 查詢所有tcp端口

node app.js

瀏覽器訪問ip:8080  顯示HElloWorld

 

 

 

啟動程序的三種方式

node app.js

nohub node app.js

forever start app.js

 

使用forever啟動

1.安裝forever

yum -y install forever -g 

-g 表示在任何目錄下使用forever,如果不加,只能在當前目錄使用

如果執行失敗

使用 npm config set strict-ssl false

forever start server.js 啟動服務

forever stop server.js 關閉服務

 

ssh root@IP 使用ssh連接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM