A. 如何使用nodejs搭建開發環境
1.安裝NodeJS
1.編譯環境
源代碼編譯器,通常 Unix/linux平台都自帶了C++的編譯器(GCC/G++)。如果沒有,請通過當前發行版的軟體包安裝工具安裝make,g++這些編譯工具。
Debian/Ubuntu下的工具是apt-get
RedHat/centOS下通過yum命令
Mac OS X下你可能需要安裝xcode來獲得編譯器
2.網路加密
其次,如果你計劃在Node.js中啟用網路加密,OpenSSL的加密庫也是必須的。該加密庫是libssl-dev,可以通過apt-get install libssl-dev等命令安裝。
3.手動編譯
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz
tar zxvf node-v0.6.1.tar.gz
cd node-v0.10.26
./configure
上面幾行命令是通過wget命令下載最新版本的代碼,並解壓之。./configure命令將會檢查環境是否符合Nodejs的編譯需要。
make
make install
2.安裝NPM
1.NPM的全稱是Node Package Manager, 是NodeJs的第三方安裝庫。
curl http://npmjs.org/install.sh | sh
curl http://npmjs.org/install.sh是通過curl命令獲取這個安裝shell腳本,按後通過管道符| 將獲取的腳本交由sh命令來執行。
2.更改第三方庫
npm install underscore
[email protected] ./node_moles/underscore
由於一些特殊的網路環境,直接通過npm install命令安裝第三方庫的時候,經常會出現卡死的狀態。幸運的是國內CNode社區的@fire9 同學利用空餘時間搭建了一個鏡像的NPM資源庫,伺服器架設在日本,可以繞過某些不必要的網路問題。你可以通過以下這條命令來安裝第三方庫:
npm --registry "http://npm.hacknodejs.com/
如果你想將它設為默認的資源庫,運行下面這條命令即可:
npm config set registry "http://npm.hacknodejs.com/ "
通過npm安裝包。安裝好之後會自動被安裝到 /usr/local/bin 目錄下,而相依的函式庫也會自動安裝到 /usr/local/lib/node 目錄下,實在是非常方便。
3.安裝NodeJS調試環境
1.用npm命令安裝全局模式的 node-inspector組件
sudo npm install -g node-inspector
2.更改埠
修改 node-inspector/lib/config.js的埠
』web-port』: {
desc: 『Port to host the inspector』,
convert: conversions.stringToInt,
defaultValue: 6868
},
3.使用
node-inspector啟動一個調試工具
在chrome瀏覽器中輸入http://127.0.0.1:6868/debug?port=5858打開chrome的調試模式
使用node debug調試nodeJS項目
node --debug-brk=5858 read.js
可以在chrome中查看到調試信息
4.使用Sublime構建NodeJS
設置Sublime的Builder->>
Tools ->> Build System ->> New Build System
將如下代碼寫入
{
「cmd」: ["/usr/local/bin/node", 「$file」],
「file_regex」: 「^[ ]File "(…?)」, line ([0-9]*)",
「selector」: 「source.javascript」
}
保存為NodeJs.sublime-build文件
如此可以直接使用Com+B來使用nodejs運行程序
B. 如何用nodejs搭建web伺服器
[linux運維]
1、下載最新node.js二進制源碼安裝包(29MB),V8.2.1更新於2017年7月20日
wget-chttps://nodejs.org/dist/v8.2.1//node-v8.2.1.tar.gz
wget是一個Linux下載文件的工具,centos自帶。
wget-c是斷點續傳下載方式,後面的URL就是提供下載文件的地址
默認獲取的文件地址在/root目錄下,命令pwd顯示當前目錄
2、安裝必要的編譯軟體吖米
yuminstallgccgcc-c++
yum=YellowdogUpdater,Modified。改良黃狗更新器,centos自帶智能包管理器。
中途詢問,輸入y:在線下載安裝d:只下載不安裝N:不下載不安裝
Isthisok[y/d/N]:y
3、解壓源碼
tar-zxvfnode-v8.2.1.tar.gz
當前目錄/root/就會多一個node-v8.2.1的文件夾
-z:透過gzip的支持進行壓縮/解壓縮:此時文件名最好為*.tar.gz
-x:解壓縮的功能
-v:在壓縮/解壓縮的過程中,將正在處理文件名顯示出來
ffilename:-f後面要立刻接被處理的文件名
4、編譯node源碼包
1)進入到node源碼包解壓目錄
cdnode-v8.2.1
指定NodeJS安裝位置
./configure--prefix=/usr/local/node
不指定prefix,則可執行文件默認放在/usr/local/bin,
庫文件默認放在/usr/local/lib,
配置文件默認放在/usr/local/etc。
其它的資源文件放在/usr/local/share。
你要卸載這個程序,要麼在原來的make目錄下用一次makeuninstall(前提是make文件指定過uninstall),
要麼去上述目錄裡面把相關的文件一個個手工刪掉。
執行安裝文件,足足等了40多分鍾
make&&makeinstall
4、添加環境變數
>創建並打開新文件不存在node.sh文件
[[email protected]]#vim/etc/profile.d/node.sh
>輸入node安裝位置的bin目錄所在位置
exportPATH=$PATH:/usr/local/node/bin
ESC輸入:wq
提示:命令輸入錯了vim
-bash:rt:commandnotfound
-bash:vim:commandnotfound
[解決]
i.那麼如何安裝vim呢?
輸入rpm-qa|grepvim命令,如果vim已經正確安裝,會返回下面的三行代碼:
root@server1[~]#rpm-qa|grepvim
vim-enhanced-7.0.109-7.el5
vim-minimal-7.0.109-7.el5
vim-common-7.0.109-7.el5
如果少了其中的某一條,比如vim-enhanced的,就用命令yum-yinstallvim-enhanced來安裝:
yum-yinstallvim-enhanced
如果上面的三條一條都沒有返回,可以直接用yum-yinstallvim*命令
yum-yinstallvim*使用suroot
source/etc/profile.d/node.sh=./etc/profile.d/node.sh
[不間斷運行nodejs服務]
https://yq.aliyun.com/ziliao/3411
npminstallforever-g
foreverstartapp.js
C. 如何在CentOS 7伺服器上安裝NodeJS
Introction
Node.js is a Javascript platform for server-side programming. It
allows users to easily create networked applications that require
backend functionality. By using Javascript as both the client and server
language, development can be fast and consistent.
In this guide, we will show you a few different ways of getting
Node.js installed on a CentOS 7 server so that you can get started. Most
users will want to use the EPEL installation instructions or the NVM
installation steps.
Install Node from Source
One way of acquiring Node.js is to obtain the source code and compile it yourself.
To do so, you should grab the source code from the project』s website.
On the downloads page, right click on the 「Source Code」 link and click
「Copy link address」 or whatever similar option your browser gives you.
On your server, use wget and paste the link that you copied in order to download the archive file:
wget http://nodejs.org/dist/v0.10.30/node-v0.10.30.tar.gz
Extract the archive and move into the new directory by typing:
tar xzvf node-v* && cd node-v*
There are a few packages that we need to download from the CentOS
repositories in order to compile the code. Use yum to get these now:
sudo yum install gcc gcc-c++
Now, we can configure and compile the software:
./configure
make
The compilation will take quite awhile. When it is finished, you can install the software onto your system by typing:
sudo make install
To check that the installation was successful, you can ask Node to display its version number:
node –version
v0.10.30
If you see the version number, then the installation was completed successfully.
Install a Package from the Node Site
Another option for installing Node.js on your server is to simply get
the pre-built packages from the Node.js website and install them.
You can find the Linux binary packages here. Since CentOS 7 only
comes in the 64-bit architecture, right click on the link under 「Linux
Binaries (.tar.gz)」 labeled 「64-bit」. Select 「Copy link address」 or
whatever similar option your browser provides.
On your server, change to your home directory and use the wget
utility to download the files. Paste the URL you just copied as the
argument for the command:
cd ~
wget http://nodejs.org/dist/v0.10.30/node-v0.10.30-linux-x64.tar.gz
Note: Your version number in the URL is likely to be different than
the one above. Use the address you copied from the Node.js site rather
than the specific URL provided in this guide.
Next, we will extract the binary package into our system』s local
package hierarchy with the tar command. The archive is packaged within a
versioned directory, which we can get rid of by passing the
–strip-components 1 option. We will specify the target directory of our
command with the -C command:
sudo tar –strip-components 1 -xzvf node-v* -C /usr/local
This will install all of the components within the /usr/local branch of your system.
You can verify that the installation was successful by asking Node for its version number:
node –version
v0.10.30
The installation was successful and you can now begin using Node.js on your CentOS 7 server.
Install Node from the EPEL Repository
An alternative installation method uses the EPEL (Extra Packages for
Enterprise Linux) repository that is available for CentOS and related
distributions.
To gain access to the EPEL repo, you must modify the repo-list of
your installation. Fortunately, we can reconfigure access to this
repository by installing a package available in our current repos called
epel-release.
sudo yum install epel-release
Now that you have access to the EPEL repository, you can install Node.js using your regular yum commands:
sudo yum install nodejs
Once again, you can check that the installation was successful by asking Node to return its version number:
node –version
v0.10.30
Many people will also want access to npm to manage their Node packages. You can also get this from EPEL by typing:
sudo yum install npm
Install Node Using the Node Version Manager
Another way of installing Node.js that is particularly flexible is
through NVM, the Node version manager. This piece of software allows you
to install and maintain many different independent versions of Node.js,
and their associated Node packages, at the same time.
To install NVM on your CentOS 7 machine, visit the project』s GitHub
page. Copy the curl or wget command from the README file that displays
on the main page. This will point you towards the most recent version of
the installation script.
Before piping the command through to bash, it is always a good idea
to audit the script to make sure it isn』t doing anything you don』t agree
with. You can do that by removing the | bash segment at the end of the
curl command:
curl https//raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh
Take a look and make sure you are comfortable with the changes it is
making. When you are satisfied, run the command again with | bash
appended at the end. The URL you use will change depending on the latest
version of NVM, but as of right now, the script can be downloaded and
executed by typing:
curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash
This will install the nvm script to your user account. To use it, you must first source your .bash_profile:
source ~/.bash_profile
Now, you can ask NVM which versions of Node it knows about:
nvm list-remote
. . .
v0.10.29
v0.10.30
v0.11.0
v0.11.1
v0.11.2
v0.11.3
v0.11.4
v0.11.5
v0.11.6
v0.11.7
v0.11.8
v0.11.9
v0.11.10
v0.11.11
v0.11.12
v0.11.13
You can install a version of Node by typing any of the releases you see. For instance, to get version 0.10.30, you can type:
nvm install v0.10.30
You can see the different versions you have installed by typing:
nvm list
-> v0.10.30
system
You can switch between them by typing:
nvm use v0.10.30
Now using node v0.10.30
To set this version as the default, type:
nvm alias default v0.10.30
default -> v0.10.30
You can verify that the install was successful using the same technique from the other sections, by typing:
node –version
v0.10.30
From the version number output, we can tell that Node is installed on our machine as we expected.
Conclusion
As you can see, there are quite a few different ways of getting
Node.js up and running on your CentOS 7 server. If one of the
installation methods is giving you problems, try one of the other
options.
D. 如何在伺服器上搭建nodejs
先確認下系統環境合不合要求
python -V(確認python版本大於2.6)
訪問http://nodejs.org/download/下載需要的Node.js版本(wget
http://nodejs.org/dist/v0.10.26/node-v0.10.29.tar.gz )
解壓 tar zxvf node-v0.10.26-linux-x64.tar.gz
進入目錄 cd node-v0.10.26-linux-x64
./configure --prefix=/home/work/setups/node-v0.10.26
make
sudo make install
添加到系統環境 echo "export PATH=$PATH:/home/work/setups/node-v0.10.26/bin">>
~/.bash_profile
. ~/.bash_profile執行該文件更新$PATH變數
###安裝Express
sudo npm install express
-gd g參數:把express安裝到NodeJS的lib目錄d參數:同時安裝依賴模塊包
sudo npm install forever -gd
(異常情況:
如果遇到npm 找不到的情況 確認sudo node -v是否找不到 找不到的話:需要added /usr/local/bin to secure_path in /etc/sudoers :
sudo visudo
把 Defaults secure_path =
/sbin:/bin:/usr/sbin:/usr/bin 這行 改為 Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin然後:x保存退出
如果遇到
npm ERR! Error: Hostname/IP doesn't match certificate's altnames
執行 npm config set strict-ssl false
如果遇到 npm ERR! registry error parsing json
npm cache clean
執行 npm config set registry http://registry.npmjs.eu/
)
###部署forever
$ npm install -g forever
啟動
NODE_ENV=pro LANG='zh' /usr/local/bin/foreverstart-o
/home/work/log/node_out.log-e /home/work/log/node_err.log /home/work/webroot/index.js
NODE_ENV=pro 環境變數 pro為生產環境(程序中可以指定)
LANG 環境變數 指定默認語言
node_out.log 輸出日誌 注意路徑別抄成我的了
node_err.log 錯誤日誌
index.js 啟動文件
E. nodejs 如何部署到伺服器上
跟你在本地開發是相同的。
1、安裝指定版本的node.js(伺服器基本軟體的安裝)
2、上傳代碼到伺服器(可以通過ftp、ssh、git等方式)
3、安裝項目依賴的模塊
>npminstall
4、啟動應用(也可以通過forever、pm2等工具進行管理)
>node./www/bin
當然,如果你深諳運維之道,可以直接通過docker等方案將運行環境容容器化。
基本的Linux運維知識的話可以參考linuxprobe.com。
還有幾個需要注意的點:
1、運行許可權:注意做好應用之間的隔離(使用低許可權用戶、文件系統隔離等),避免應用崩潰導致系統宕機等風險
2、80埠:通過nginx等進行反向代理,應用本身佔用1024後的埠(無需root許可權)