What you need
An Ubuntu VPS, SSH access and your Node.js application.
Step 1: Install Node.js
Install a current Node.js LTS release using the NodeSource repository or nvm. Then check it works.
node -v
npm -v
Step 2: Install PM2
sudo npm install -g pm2
PM2 keeps your app running in the background and restarts it if it crashes.
Step 3: Start your app
Go to your project folder and start it.
cd /var/www/myapp
pm2 start npm --name myapp -- start
For a plain Node file you can use pm2 start server.js --name myapp.
Step 4: Start on boot
pm2 save
pm2 startup
Run the command that pm2 startup prints. After a reboot your app will start automatically.
Useful commands
- pm2 list shows running apps
- pm2 logs myapp shows output and errors
- pm2 restart myapp restarts after a deployment
- pm2 stop myapp stops the app
Next step
Put Nginx in front of your app to serve it on ports 80 and 443 with SSL. See our article on setting up Nginx as a reverse proxy.
Need help?
If you get stuck, open a support ticket or email info@webzworld.com and our team will help.