cPanel & WHM Tutorials

How to Host a Node.js App on Your VPS

How to Host a Node.js App on Your VPS

Node.js has become one of the most popular platforms for building fast, scalable applications. Whether you’re developing a real-time chat app, an API, or an e-commerce site, deploying your Node.js project to a VPS (Virtual Private Server) ensures better performance, security, and full control compared to shared hosting.

In this guide, we’ll walk you step-by-step through hosting a Node.js app on your VPS, from installation to deployment.

Step 1: Update Your Server

Before installing Node.js, make sure your VPS is up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js and npm

Node.js comes with npm (Node Package Manager), which you’ll use to manage dependencies. Install it with:

sudo apt install nodejs npm -y

Check the versions to confirm installation:

node -v
npm -v

For the latest stable version, you can also install Node.js using NodeSource:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

Step 3: Upload Your App to the VPS

You can upload your Node.js project files using Git, FileZilla (SFTP), or scp. Example with Git:

git clone https://github.com/yourusername/your-node-app.git
cd your-node-app

Install dependencies:

npm install

Step 4: Test Your App

Run your app locally on the VPS to confirm it works:

node app.js

Or, if you’re using a framework like Express, it might be:

npm start

By default, Node.js apps run on a port (e.g., 3000). Test it in your browser:

http://your-server-ip:3000

Step 5: Use PM2 to Keep Your App Running

By default, if you close the terminal, your app will stop. To solve this, install PM2, a process manager for Node.js:

sudo npm install -g pm2

Start your app with PM2:

pm2 start app.js

Enable PM2 to auto-start on server reboot:

pm2 startup systemd
pm2 save

Now, your Node.js app will always stay online.

Step 6: Set Up a Reverse Proxy with Nginx

Your app is running, but it’s only accessible via port (e.g., :3000). To make it accessible via your domain (e.g., https://example.com), configure Nginx as a reverse proxy.

Install Nginx:

sudo apt install nginx -y

Create a new config file:

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

Add this configuration:

server {
    listen 80;
    server_name yourdomain.com www.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;
    }
}

Enable the config and restart Nginx:

sudo ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Now, your app is available on your domain without needing a port number.

Step 7: Secure Your App with SSL (HTTPS)

For security and SEO, enable SSL using Certbot:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the prompts, and you’ll have HTTPS enabled.

Step 8: Verify Deployment

Visit your domain in a browser:

https://yourdomain.com

Your Node.js app should now be live and secure! 🎉

Conclusion

Hosting a Node.js app on your VPS gives you:
✅ Full control over your environment
✅ Scalability with PM2 and Nginx
✅ Secure access with SSL certificates
✅ Reliability with a dedicated VPS from Vicservers

At Vicservers, we provide VPS and dedicated servers optimized for Node.js and modern web applications. Whether you’re deploying a small project or scaling a large production system, we’ve got the right infrastructure for you.

Ready to host your Node.js app with confidence? Get started today with Vicservers VPS hosting!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button