部署Node.js项目(CentOS)

  • A+
所属分类:Linux
高性能企业级服务器首台5折

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效,非常适合运行在分布式设备的数据密集型的实时应用。Node.js 的包管理器 npm,是全球最大的开源库生态系统。典型的应用场景包括:

  • 实时应用:如在线聊天,实时通知推送等等(如socket.io)
  • 分布式应用:通过高效的并行I/O使用已有的数据
  • 工具类应用:海量的工具,小到前端压缩部署(如grunt),大到桌面图形界面应用程序
  • 游戏类应用:游戏领域对实时和并发有很高的要求(如网易的pomelo框架)
  • 利用稳定接口提升Web渲染能力
  • 前后端编程语言环境统一:前端开发人员可以非常快速地切入到服务器端的开发(如著名的纯Javascript全栈式MEAN架构)

适用对象

本文档介绍如何在阿里云CentOS系统的云服务器ECS实例上,安装Nodejs并部署项目。

准备工作

部署之前,请做如下准备工作:

  • 购买ECS实例
  • 您的实例运行的镜像是CentOS7.2
  • 您的实例可以连接公网
  • 本地已经安装用于连接 Linux 实例的工具,如 PuTTY

基本流程

使用云服务器ECS安装Nodejs并部署项目的操作步骤如下:

  1. 购买ECS实例,并连接实例。
  2. 选择以下任一种方法部署Node.js环境:
    • 使用二进制文件。
    • 使用NVM安装多版本。
  3. 部署测试项目。

操作步骤

步骤 1:创建ECS实例

创建ECS实例。选择操作系统为公共镜像CentOS7.2。使用root用户登录Linux实例

步骤2:部署Node.js环境

使用以下任一种方法部署Node.js环境。

使用二进制文件安装

该部署过程使用的安装包是已编译好的二进制文件,解压之后,在bin文件夹中就已存在node和npm,无需手工编译。

安装步骤:

  1. wget命令下载Node.js安装包。该安装包是编译好的文件,解压之后,在bin文件夹中就已存在node和npm,无需重复编译。
    1. 1
      <span class="pln">wget https</span><span class="pun">:</span><span class="com">//nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz</span>
  2. 解压文件。
    1. 1
      <span class="pln">tar xvf node</span><span class="pun">-</span><span class="pln">v6</span><span class="pun">.</span><span class="lit">9.5</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x64</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">xz</span>
  3. 创建软链接,使node和npm命令全局有效。通过创建软链接的方法,使得在任意目录下都可以直接使用node和npm命令:
    1. 1
      <span class="pln">ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">node</span><span class="pun">-</span><span class="pln">v6</span><span class="pun">.</span><span class="lit">9.5</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x64</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">node </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">node</span>
    2. 1
      <span class="pln">ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">node</span><span class="pun">-</span><span class="pln">v6</span><span class="pun">.</span><span class="lit">9.5</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x64</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">npm </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">npm</span>
  4. 查看node、npm版本。
    1. 1
      <span class="pln">node </span><span class="pun">-</span><span class="pln">v</span>
    2. 1
      <span class="pln">npm </span><span class="pun">-</span><span class="pln">v</span>
  5. 至此,Node.js环境已安装完毕。软件默认安装在/root/node-v6.9.5-linux-x64/目录下。如果需要将该软件安装到其他目录(如:/opt/node/)下,请进行如下操作:
    1. 1
      <span class="pln">mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">node</span><span class="pun">/</span>
    2. 1
      <span class="pln">mv </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">node</span><span class="pun">-</span><span class="pln">v6</span><span class="pun">.</span><span class="lit">9.5</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">x64</span><span class="com">/* /opt/node/</span>
    3. 1
      <span class="com">rm -f /usr/local/bin/node</span>
    4. 1
      <span class="com">rm -f /usr/local/bin/npm</span>
    5. 1
      <span class="com">ln -s /opt/node/bin/node /usr/local/bin/node</span>
    6. 1
      <span class="com">ln -s /opt/node/bin/npm /usr/local/bin/npm</span>

使用NVM安装多版本

NVM(Node version manager)是Node.js的版本管理软件,使用户可以轻松在Node.js各个版本间进行切换。适用于长期做 node 开发的人员或有快速更新node版本、快速切换node版本这一需求的用户。

安装步骤:

  1. 直接使用git将源码克隆到本地的~/.nvm目录下,并检查最新版本。
    1. 1
      <span class="pln">yum install git</span>
    2. 1
      <span class="pln">git clone https</span><span class="pun">:</span><span class="com">//github.com/cnpm/nvm.git ~/.nvm &amp;&amp; cd ~/.nvm &amp;&amp; git checkout `git describe --abbrev=0 --tags`</span>
  2. 激活NVM。
    1. 1
      <span class="pln">echo </span><span class="str">". ~/.nvm/nvm.sh"</span> <span class="pun">&gt;&gt;</span> <span class="str">/etc/</span><span class="pln">profile</span>
    2. 1
      <span class="pln">source </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">profile</span>
  3. 列出Node.js的所有版本。
    1. 1
      <span class="pln">nvm list</span><span class="pun">-</span><span class="pln">remote</span>
  4. 安装多个Node.js版本。
    1. 1
      <span class="pln">nvm install v6</span><span class="pun">.</span><span class="lit">9.5</span>
    2. 1
      <span class="pln">nvm install v7</span><span class="pun">.</span><span class="lit">4.0</span>
  5. 运行 
    1
    nvm ls

     查看已安装Node.js版本,当前使用的版本为v6.9.5。返回结果如下所示。

    1. 1
      <span class="pun">[</span><span class="pln">root@iZXXXXZ </span><span class="pun">.</span><span class="pln">nvm</span><span class="pun">]#</span><span class="pln"> nvm ls</span>
    2. 1
      <span class="pln">      v6</span><span class="pun">.</span><span class="lit">9.5</span>
    3. 1
      <span class="pun"><span class="hljs-meta">-&gt;</span></span><span class="pln"><span class="bash">       v7</span></span><span class="pun"><span class="bash">.</span></span><span class="lit"><span class="bash">4.0</span></span>
    4. 1
      <span class="pln">      system</span>
    5. 1
      <span class="pln">stable </span><span class="pun">-&gt;</span> <span class="lit">7.4</span> <span class="pun">(-&gt;</span><span class="pln"> v7</span><span class="pun">.</span><span class="lit">4.0</span><span class="pun">)</span> <span class="pun">(</span><span class="kwd">default</span><span class="pun">)</span>
    6. 1
      <span class="pln">unstable </span><span class="pun">-&gt;</span> <span class="lit">6.9</span> <span class="pun">(-&gt;</span><span class="pln"> v6</span><span class="pun">.</span><span class="lit">9.5</span><span class="pun">)</span> <span class="pun">(</span><span class="kwd">default</span><span class="pun">)</span>
  6. 运行 
    1
    nvm use v7.4.0

     切换Node.js版本至v7.4.0。返回结果如下所示。

    1. 1
      <span class="pun">[</span><span class="pln">root@iZXXXXZ </span><span class="pun">.</span><span class="pln">nvm</span><span class="pun">]#</span><span class="pln"> nvm </span><span class="kwd">use</span><span class="pln"> v7</span><span class="pun">.</span><span class="lit">4.0</span>
    2. 1
      <span class="typ">Now</span> <span class="kwd">using</span><span class="pln"> node v7</span><span class="pun">.</span><span class="lit">4.0</span>

NVM的更多操作请参考帮助文档:

  1. 1
    <span class="pln">nvm help</span>

步骤3:部署测试项目

  1. 新建项目文件example.js。
    1. 1
      <span class="pln">cd </span><span class="pun">~</span>
    2. 1
      <span class="pln">touch example</span><span class="pun">.</span><span class="pln">js</span>
  2. 使用vim编辑器打开项目文件example.js。
    1. 1
      <span class="pln">yum install vim</span>
    2. 1
      <span class="pln">vim example</span><span class="pun">.</span><span class="pln">js</span>

    输入 

    1
    i

    ,进入编辑模式,将以下项目文件内容粘贴到文件中。使用

    1
    Esc

    按钮,退出编辑模式,输入

    1
    :wq

    ,回车,保存文件内容并退出。
    项目文件内容:

    1. 1
      <span class="kwd">const</span><span class="pln"> http </span><span class="pun">=</span> <span class="kwd">require</span><span class="pun">(</span><span class="str">'http'</span><span class="pun">);</span>
    2. 1
      <span class="kwd">const</span><span class="pln"> hostname </span><span class="pun">=</span> <span class="str">'0.0.0.0'</span><span class="pun">;</span>
    3. 1
      <span class="kwd">const</span><span class="pln"> port </span><span class="pun">=</span> <span class="lit">3000</span><span class="pun">;</span>
    4. 1
      <span class="kwd">const</span><span class="pln"> server </span><span class="pun">=</span><span class="pln"> http</span><span class="pun">.</span><span class="pln">createServer</span><span class="pun">((</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span> <span class="pun">=&gt;</span> <span class="pun">{</span>
    5. 1
      <span class="pln">res</span><span class="pun">.</span><span class="pln">statusCode </span><span class="pun">=</span> <span class="lit">200</span><span class="pun">;</span>
    6. 1
      <span class="pln">res</span><span class="pun">.</span><span class="pln">setHeader</span><span class="pun">(</span><span class="str">'Content-Type'</span><span class="pun">,</span> <span class="str">'text/plain'</span><span class="pun">);</span>
    7. 1
      <span class="pln">res</span><span class="pun">.</span><span class="kwd">end</span><span class="pun">(</span><span class="str">'Hello World\n'</span><span class="pun">);</span>
    8. 1
      <span class="pun">});</span>
    9. 1
      <span class="pln">server</span><span class="pun">.</span><span class="pln">listen</span><span class="pun">(</span><span class="pln">port</span><span class="pun">,</span><span class="pln"> hostname</span><span class="pun">,</span> <span class="pun">()</span> <span class="pun">=&gt;</span> <span class="pun">{</span>
    10. 1
      <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">`Server running at http://${hostname}:${port}/`</span><span class="pun">);</span>
    11. 1
      <span class="pun">});</span>

    注意
    项目文件内容中的3000为端口号,可以自行定义。

  3. 运行项目。
    1. 1
      <span class="pln">node </span><span class="pun">~/</span><span class="pln">example</span><span class="pun">.</span><span class="pln">js</span>

    注意
    可以使用命令 

    1
    node ~/example.js &amp;

     将项目置于后台运行。

  4. 使用命令查看项目端口是否存在。
    1. 1
      <span class="pln">netstat </span><span class="pun">-</span><span class="pln">tpln</span>
  5. 登录ECS管理控制台,并在安全组中 添加安全组规则 放行端口(如本示例中为TCP 3000端口)。
  6. (可选)如果您的实例中开启了防火墙,必须添加端口的入站规则(如本示例中为TCP 3000端口)。
  7. 在本地机器的浏览器中输入 
    1
    http://实例公网IP地址:端口号

     访问项目。
    通过浏览器访问项目

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: