Deploy Nest JS App using PM2 to Linux Server
Hi guys, in this article I will show you how to deploy Nest JS application to Linux Server. For this tutorial I use Ubuntu 20.04 LTS.
STEP 1 (Install Node JS)
First, you need to install Node JS on your ubuntu server using this command :
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
$ sudo apt-get install -y nodejs
STEP 2 (Install Nest JS CLI)
After that, to run and build Nest JS App you need to install Nest JS CLI using this command :
$ sudo npm i -g @nestjs/cli
STEP 3 (Install PM2)
PM2 is a library
$ sudo npm install pm2@latest -g
STEP 4 (Clone your project from Source Control)
You have Node JS, Nest CLI and PM2 installed in your server, now you can clone your project from The Source Control Repository to a folder :
$ git clone http://sourcecontrolrepository.com/projectname.git
$ cd projectname
$ npm install
STEP 5 (Run project)
After downloading all packages on your project, you need to build your Nest JS Project first :
$ npm run build
If the build success, it will generate folder /dist on the root directory. Run your application using this command :
$ pm2 start dist/main.js --name <application_name>
In the code above, You can specify the application_name as you want. To make your application autostart after system reboot, use this command :
$ pm2 startup systemd
$ pm2 save
Now your Nest JS application has been successfully deployed to Ubuntu Server. Enjoy!