DEV Community

Cover image for Node.js Elegance: Deploying a Secure Backend with PM2 and Add SSL Certificate
Sujit Kumar
Sujit Kumar

Posted on

17 1 1 1

Node.js Elegance: Deploying a Secure Backend with PM2 and Add SSL Certificate

Overview

Hosting a Node.js backend on a server is a crucial step towards making your application accessible to users. In this guide, we'll walk through the process of deploying a Node.js backend using PM2, a process manager for Node.js applications. Additionally, we'll enable SSL encryption to enhance the security of your deployment.

Prerequisites

Before you begin, ensure that your Node.js project is production-ready, and any necessary database configurations are in place. Also, make sure Node.js is installed on your server.

Step 1: Install PM2

On your server, install PM2 globally using npm:

npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Step 2: Start Your Node.js Application

Navigate to your project directory and start your Node.js application using PM2:

pm2 start server.js --name custom_name
Enter fullscreen mode Exit fullscreen mode

Replace server.js with your actual entry file and choose a meaningful custom name.

Step 3: Check Application Status

To ensure your application is running, use the following command:

pm2 list
Enter fullscreen mode Exit fullscreen mode

This will display a list of running PM2 processes, including the one you just started.

Step 4: Enable SSL Certificate

To add SSL encryption, follow these steps:

  1. Clone the script for setting up Nginx proxy with SSL:
   wget https://raw.githubusercontent.com/thepwnexperts/advance-things/04c78015ad05c1e0be07dc82592740ac2a308217/linux/vps/nginx-proxy/localhost/nginx-proxy-with-ssl.sh
Enter fullscreen mode Exit fullscreen mode
  1. Change script permissions:
   chmod +x ./nginx-proxy-with-ssl.sh
Enter fullscreen mode Exit fullscreen mode
  1. Restart Nginx:
   service nginx restart
Enter fullscreen mode Exit fullscreen mode
  1. Run the script:
   ./nginx-proxy-with-ssl.sh
Enter fullscreen mode Exit fullscreen mode
  1. Follow the prompts to register your domain name.

  2. After successful completion, exit the shell.

Conclusion

Congratulations! You've successfully deployed your Node.js backend using PM2 and enabled SSL encryption for added security. Your application is now accessible via HTTPS, providing a secure connection for your users.

Feel free to customize these steps based on your specific project requirements. Happy coding!


Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
anonymous1508 profile image
anonymous1508 •

nice

Collapse
 
abrknine profile image
Abhishek Bahukhandi •

very useful bhay

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more