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权限)