nodejs

How to Deploy a Node.js App on a Linux VPS

As you begin your journey as a developer you want to control your app post deployment. A VPS is needed instead of a basic shared hosting service. This is important when you want your app to really start as a startup. There are many important technical nuances you need to know when you really begin to want your app to be professional enough to scale.

First things first. Apply the following advice from the beginning. It is absolutely critical when doing Node.js app deployment the usability of your app is really dependent on your VPS and your Linux VPS. Performance and Resourcing really are at the center of the tech. Therefore, a reliable Linux VPS service is going to guarantee a functional tech stack.

Preparation For Deployment

Make sure your server and local environment are correct before deploying your Node.js app.

Conditions:

  1. A Linux VPS (Ubuntu is the most frequently used)
  2. Node.js and npm are installed
  3. You have SSH access to the server
  4. Basic terminal commands
  5. A Node.js app ready to be deployed

Step 1: Connect to Your VPS

Use SSH to access your server:

ssh root@your-server-ip

Once connected, update your system packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js

You can install Node.js using NodeSource or the default package manager.

Install via NodeSource:

curl -fsSL https://deb.nodesource.com/setup\_18.x | sudo -E bash -

sudo apt install -y nodejs

Verify installation:

node -v
npm -v

Step 3: Upload Your Application

There are several ways to transfer your app files to the VPS.

Common Methods:

Navigate to your project folder:

cd your-app

Install dependencies:

npm install

Step 4: Run Your Application

Start your Node.js app using:

node app.js

However, this method is not ideal for production since the app stops when the terminal closes.

Use PM2 for Process Management

PM2 keeps your app running in the background and restarts it if it crashes.

npm install -g pm2

pm2 start app.js

pm2 save

pm2 startup

Step 5: Set Up a Reverse Proxy with Nginx

To make your app accessible via a domain and standard HTTP/HTTPS ports, configure Nginx.

Install Nginx:

sudo apt install nginx -y

Configure Nginx:

Edit the default config file:

sudo nano /etc/nginx/sites-available/default

Add the following:

server {

   listen 80;

   server_name yourdomain.com;

   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;

   }

}

Restart Nginx:

sudo systemctl restart nginx

Step 6: Secure Your Application

Security is critical when deploying applications online.

Best Practices:

  • Enable firewall: sudo ufw allow OpenSSH sudo ufw allow 'Nginx Full' sudo ufw enable
  • Install SSL with Certbot: sudo apt install certbot python3-certbot-nginx sudo certbot --nginx
  • Keep your server updated regularly

Tips for Optimizing Performance

Some points to keep in mind when attempting to optimize your application efficiency include:

Optimization Tips:

  • Cluster to take advantage of more than one CPU core
  • Turn on gzip compression in Nginx
  • Add caching layers
  • Use PM2 dashboard average response time to watch your app performance

Choosing the Right VPS Provider

Choosing the most appropriate hosting service can be the most difficult aspect of Node.js app deployment. Performance is going to be important in all of the services you are going to be both paying for and providing. That's why many developers like to use https://bluevps.com for technical flexibility, good performance, and a good service all around for Node.js app deployment.

Conclusion

It is critical to know all of the services Node.js deployment of Linux VPS hosting services provides. Also, as a new developer, a lot of the set up and installation work is going to be building apps with yourself. So, you are going to need a lot of the automation and set ups from you on your node hosting service to configure yourself to make your application secure. With good services and set practices, it's easy to password secure Node.js. It is really easy to scale up and down.