Effortless Deployment: Launching a Node.js App on Ubuntu Server
A detailed guide on how to deploy your Node.js application on a VPS
First of all to deploy an application on a VPS (virtual private server) you will need a server. To get one for free you may tried out Digital Ocean droplets by redeeming benifits of Github Student Pack.
So let's begin the process!
Connect to your remote server
To connect to your vps from your local machine follow below steps.
ssh root@<VPS_IP>
## Update your server
sudo apt update && sudo apt upgrade -y
Install dependencies to run your application (Node.js in our case) and nginx to expose your application to the internet.
sudo apt install -y nodejs npm nginx
Clone your application from Github
Assuming you have your application code published on github, clone the repository in your vps. If you want to use FTP to import code in your server, consider using tools like Filezilla.
git clone https://github.com/alpha951/Awasar-Job-Tracker
Run your application
Install all the basic dependencies for your application and update environment variables.
vi .env ## creating a .env file to update env credential
npm install
npm start # App starts at port 3000
At this point our application is running at port 3000 but if we try to access the app from our local machine at <your-vps-ip>:3000
you won't be able to access this. Since by default only port 80 is exposed to the internet.
We need to expose our port 3000 to the internet in order to make our application live on internet. To this we will use nginx as a proxy server.
Setting up nginx
sudo nano /etc/nginx/sites-available/our-app
## paste this config in our-app file
server {
listen 80;
server_name YOUR_IP_ADDRESS;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/our-app /etc/nginx/sites-enabled/
Chect the Nginx configuration for any syntax errors:
sudo nginx -t
You must see something like this :
Now run below command to restart nginx:
sudo service nginx restart
Now finally our application is available on internet. But wait, we are running our application using a terminal blocking command like npm start
so if some reason the process gets terminated then our app will stop running. We need to use a process manager like pm2
to make sure our app always up and running.
Setting Up PM2 Process Manager
sudo npm install -g pm2
cd <our-app-location>
pm2 start npm --name "awasar-app" -- start
This command will start the Node.js application with the name “awasar-app” using the npm start command. PM2 will automatically restart the application if it crashes or if the server reboots.
To ensure PM2 starts on boot, run:
pm2 startup
pm2 save
You may check status of all process running by pm2
as below:
pm2 status
You may use commands like these to control your application
pm2 stop our-app # stop running the app
pm2 restart our-app # restart the app
pm2 logs our-app # shows the terminallog of running app
So that was it for today!