DEV Community

Mrityunjaya Prajapati
Mrityunjaya Prajapati

Posted on

Deploy Nest JS App using PM2 on Linux (Ubuntu) Server

In this article, I will cover steps that will help you to deploy Nest JS application on Linux Server. I am using Ubuntu 20.04 LTS **during writing this article and I am assuming you have already installed **Node.js on your server.

Step 1 (Install Nest JS CLI)

Generally, we use npm i command to run all the dependencies but that will not install Nest JS so we have to manually install Nest JS CLI

$ sudo npm i -g @nestjs/cli

Step 2 (Install PM2)

If you didn’t install PM2 earlier then you have to use following command to install PM2.

$ sudo npm install pm2@latest -g

If you want to get more details about PM2 commands, then please visit url: PM2 Commands

Step 3 (Clone Code Repository)

You can clone your repo using following command and install necessary dependencies. If you want more details on Cloning, please refer article: Clone Specific Git Branch

$ git clone http://coderepo.com/projectname.git
$ cd projectname
$ npm install

Step 4 (Build Project)

For Nest JS app, you have to first generate a build then only you can run the project on server.

$ npm run build

Step 5 (Run Project)

If build is generated successfully then will create /dist folder under root directory. Now you can run your project using following PM2 command

$ pm2 start dist/main.js --name <application_name>

application_name you can use to give unique app name so that you can easily identify your apps in pm2 list.

There are some PM2 commands to make your application auto restart after system reboot also

$ pm2 startup systemd
$ pm2 save

Conclusion

I hope this article will help you deploy your Nest JS application on Linux Server. If you liked the solution, please hit the Clap button or comment if you have any suggestion or feedback.

Top comments (3)

Collapse
 
zahirinatzuke profile image
Yohan González

Thanks !!

Collapse
 
ntsoa2112 profile image
MIHAINGOHERILANTO

Thank you @deadwin19

Collapse
 
shaxi007 profile image
Teshayev Shaxboz

Thank you