Deploying to the Server
Now that your project is built, it’s time to deploy it to your virtual server (V-Server). This section explains how to manually transfer and run your app, as well as how to automate it later if desired.
Prepare the Server
Make sure your server:
- Has Node.js and npm installed (also make sure you have the correct version!)
- Has a dedicated deployment directory (e.g.,
/var/www/nest-app) - Allows SSH access
Create a directory
If this is the first time you are deploying your NestJS application, you most likely need to create a new directory. You can do it manually using it via an FTP client like WinSCP. Or you can use the following command:
mkdir -p /var/www/nest-appTransfer files to Server
Now it is already the time to bring your files to the server. Here are three easy ways to copy your files to your server.
WinSCP
🔎 Info
This is only available for Windows.
Connect to your server, and copy over the files in the previously created directory. You need the following files in your directory:
- /dist
- package.json
- .env.productionUsing scp
scp -r dist package.json .env.production user@your-server-ip:/var/www/nest-appReplace user@your-server-ip with your actual server credentials.
Using rsync
⚠️ Warning ⚠️
This is recommended for updates.
scp -r dist package.json .env.production user@your-server-ip:/var/www/nest-appReplace user@your-server-ip with your actual server credentials.
Install Dependencies
- Login to your server:
ssh user@your-server-ip- Navigate to the app directory
cd /var/www/nest-app- Install only production dependencies
npm install --omit=devRun the Application
To run your application, you can either start it manually, just as you already did when you tested it locally:
NODE_ENV=production node dist/main.jsBut preferably you can use a process manager like PM2. With a tool like this you can also let the application restart automatically if the server restarts.
npm install -g pm2
pm2 start dist/main.js --name nest-app --env production
pm2 save
pm2 startup