简体   繁体   English

如何在 Linux VPS 上部署 nodejs 应用程序

[英]How to deploy nodejs application on Linux VPS

I have already developed an Application in Nodejs.我已经在 Nodejs 中开发了一个应用程序。 It is running quite well on the local machine.它在本地机器上运行得很好。 But the problem rises while I am trying to deploy the Application as a Back-end App on a Linux VPS.但是当我尝试将应用程序部署为 Linux VPS 上的后端应用程序时,问题就出现了。

I have installed the Nodejs on Linux VPS already and already added PM2 module to run the Application.我已经在 Linux VPS 上安装了 Nodejs 并且已经添加了 PM2 模块来运行应用程序。 But it is showing error while executing the command "PM@ start index.js".但是在执行命令“PM@ start index.js”时显示错误。

At the time of looking for the error I found that the version of Node that I have installed is 6.14 which is not compatible for PM2 module.在查找错误时发现我安装的Node版本是6.14,与PM2模块不兼容。 Even if I also tried to upgrade the version of Node along with npm but old version name is showing up.即使我也尝试将 Node 的版本与 npm 一起升级,但旧版本名称出现了。

Please advice how to configure a Linux VPS to run a nodejs application.请建议如何配置 Linux VPS 来运行 nodejs 应用程序。

If you're using Express.js, you need to point it to bin/www, like below.如果您使用 Express.js,则需要将其指向 bin/www,如下所示。

pm2 start bin/www

You also need to add --node-args="-r esm" if you're using ESM.如果您使用 ESM,您还需要添加 --node-args="-r esm"。

pm2 start bin/www --node-args="-r esm"

My advice would be to use the Node Version Manager for installing and managing Node versions in Linux as some versions of Linux have very old versions of Nodejs in their package repositories.我的建议是使用 Node Version Manager 在 Linux 中安装和管理 Node 版本,因为某些版本的 Linux 在它们的包存储库中有非常旧的 Nodejs 版本。 If you have node already installed, please uninstall it using your package manager on your Linux VPS.如果您已经安装了 node,请使用您的 Linux VPS 上的包管理器卸载它。 If you are using Ubuntu based VPS, it should be possible using the command:如果您使用的是基于 Ubuntu 的 VPS,则应该可以使用以下命令:

sudo apt-get purge nodejs

This is how you install the Node Version Manager:这是安装节点版本管理器的方式:

https://github.com/nvm-sh/nvm#installing-and-updating https://github.com/nvm-sh/nvm#installing-and-updating

You will need curl or wget before you can install Nodejs.在安装 Nodejs 之前,您需要 curl 或 wget。 So you will need to do the following:因此,您需要执行以下操作:

sudo apt-get install curl

After that, you run the following command to install the Node Version Manager (nvm).之后,运行以下命令来安装节点版本管理器 (nvm)。

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Now, to install the latest version of NodeJs using NVM, it is a simple command:现在,要使用 NVM 安装最新版本的 NodeJs,只需一个简单的命令:

nvm install node

If you prefer a specific version of NodeJs, you can install it using a command:如果您更喜欢特定版本的 NodeJs,可以使用以下命令安装它:

nvm install 14.8.0

You can check to see if NodeJs is successfully installed by running the following command which will return the version of NodeJs:您可以通过运行以下命令来检查 NodeJs 是否已成功安装,该命令将返回 NodeJs 的版本:

node --version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM