Deploying Node.js applications with PM2 on Vultr

2 months ago 117

In today’s fast-paced digital environment, deploying and managing Node.js applications efficiently is crucial for maintaining performance and scalability. PM2 is a powerful process manager for Node.js applications that simplifies deployment, monitoring, and management. Vultr is a reliable cloud infrastructure provider known for its ease of use and affordability. Combining PM2 with Vultr can help you deploy Node.js applications smoothly, ensuring they run reliably and efficiently.

In this guide, we’ll walk through the steps to deploy Node.js applications with PM2 on a Vultr server, covering everything from setting up your server to configuring PM2 for optimal performance.

 Prerequisites

Before we dive into the deployment process, ensure you have the following:

  • A Vultr Account: Sign up for a Vultr account if you haven’t already.
  • Node.js and npm: Node.js is required for running your application, and npm (Node Package Manager) is necessary for managing your application’s dependencies.
  • PM2: A process manager for Node.js applications.
  • A Basic Understanding of Command Line Interface (CLI): Familiarity with CLI commands will be helpful.

2. Setting Up Your Vultr Server

  • Create a Vultr Instance:
  • Log in to your Vultr account.
  • Navigate to the "Deploy New Instance" section.
  • Choose an operating system (we’ll use Ubuntu for this guide).
  • Select a server plan that fits your needs (the smallest plan should work for most small to medium applications).
  • Deploy the instance.
  • Access Your Server:
  • Once the instance is deployed, you’ll receive an IP address.

Use an SSH client like PuTTY (for Windows) or the terminal (for macOS/Linux) to access your server:
bash
Copy code
ssh root@your_server_ip


3. Preparing Your Server

  • Update the Server:

Update the package list and upgrade all packages to their latest versions:
bash
Copy code
sudo apt update

sudo apt upgrade -y


  • Install Node.js and npm:

Install Node.js and npm using the NodeSource repository for the latest version:
bash
Copy code
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

sudo apt install -y nodejs


Verify the installation:
bash
Copy code
node -v

npm -v


4. Installing PM2

  • Install PM2 Globally:

PM2 can be installed globally using npm:
bash
Copy code
sudo npm install -g pm2


Verify the installation:
bash
Copy code
pm2 -v


5. Deploying Your Node.js Application

  • Upload Your Application:

Use SCP (Secure Copy Protocol) or Git to transfer your Node.js application to the server. For example, using SCP:
bash
Copy code
scp -r /path/to/your/app root@your_server_ip:/path/to/destination


Alternatively, clone your application from a Git repository:
bash
Copy code
git clone https://github.com/your-repo/your-app.git


  • Navigate to Your Application Directory:

Go to the directory where your application is located:
bash
Copy code
cd /path/to/your/app


  • Install Dependencies:

Install the necessary dependencies defined in your package.json:
bash
Copy code
npm install


6. Configuring PM2

  • Start Your Application with PM2:

Use PM2 to start your application. For example, if your entry point is app.js:
bash
Copy code
pm2 start app.js


  • Configure PM2 to Restart on Reboot:

PM2 can be configured to start your application automatically when the server reboots:
bash
Copy code
pm2 startup


  • Follow the instructions provided by the pm2 startup command to save the PM2 configuration.
  • Save the PM2 Process List:

Save the current process list to be restored on reboot:
bash
Copy code
pm2 save


  • Monitoring and Managing Your Application:
  • PM2 provides a variety of commands to monitor and manage your application. Some useful commands include:

View process status:
bash
Copy code
pm2 list


View logs:
bash
Copy code
pm2 logs


Stop a process:
bash
Copy code
pm2 stop app_name


Restart a process:
bash
Copy code
pm2 restart app_name


7. Securing Your Server

  • Set Up a Firewall:

Install ufw (Uncomplicated Firewall) if not already installed:
bash
Copy code
sudo apt install ufw


Allow SSH connections:
bash
Copy code
sudo ufw allow ssh


Allow HTTP/HTTPS connections if your application needs them:
bash
Copy code
sudo ufw allow http

sudo ufw allow https


Enable the firewall:
bash
Copy code
sudo ufw enable


  • Create a Non-Root User:

It’s a good practice to run your applications as a non-root user. Create a new user:
bash
Copy code
sudo adduser yourusername


Grant necessary permissions and switch to the new user:
bash
Copy code
sudo usermod -aG sudo yourusername

su - yourusername


8. Setting Up a Domain and SSL

  • Point Your Domain to Your Server:
  • Update your domain’s DNS settings to point to your Vultr server’s IP address.
  • Install and Configure Nginx:

Install Nginx:
bash
Copy code
sudo apt install nginx


Configure Nginx to serve your Node.js application. Create a new configuration file:
bash
Copy code
sudo nano /etc/nginx/sites-available/yourdomain.com


Add the following configuration:
nginx
Copy code
server {

  listen 80;

  server_name yourdomain.com;


  location / {

    proxy_pass http://localhost:3000; # Update with your application's port

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header X-Forwarded-Proto $scheme;

  }

}


Enable the configuration and restart Nginx:
bash
Copy code
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

sudo systemctl restart nginx


  • Install SSL Certificate:

Use Certbot to obtain a free SSL certificate from Let’s Encrypt:
bash
Copy code
sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx


  • Follow the prompts to configure SSL for your domain.

Deploying Node.js applications with PM2 on Vultr provides a robust and scalable solution for managing your web applications. By following the steps outlined in this guide, you can ensure your application is deployed efficiently, monitored effectively, and secured properly.

With PM2 handling process management and Vultr providing a reliable cloud infrastructure, you can focus on developing your application while maintaining high performance and availability. Keep exploring and adapting best practices for deployment and management to stay ahead in the dynamic world of web development.

Frequently Asked Questions (FAQ) for Deploying Node.js Applications with PM2 on Vultr

1. What is PM2 and why should I use it?

PM2 is a process manager for Node.js applications that simplifies deployment, monitoring, and management. It provides features like process monitoring, automatic restarts on failure, and load balancing across multiple instances, making it ideal for maintaining the reliability and performance of your Node.js applications.

2. How do I choose the right Vultr server plan for my application?

The choice of server plan depends on your application’s resource requirements. For small to medium applications, the basic plans should suffice. As your application grows or if you expect high traffic, you may need to opt for more robust plans with higher CPU, RAM, and storage.

3. Can I deploy multiple Node.js applications on a single Vultr server?

Yes, you can deploy multiple Node.js applications on a single Vultr server. PM2 can manage multiple processes simultaneously. Just ensure your applications listen on different ports and configure your web server (like Nginx) to route traffic to the correct application.

4. How do I update my Node.js application after deployment?

To update your Node.js application:

  • Upload the new version of your application to the server.
  • Navigate to the application directory.
  • Pull the latest changes from your repository or replace the files.

Install any new dependencies:
bash
Copy code
npm install


Restart the application with PM2:
bash
Copy code
pm2 restart app_name


5. What should I do if my application crashes or stops working?

Check the PM2 logs for any errors that might indicate why your application crashed:

bash

Copy code

pm2 logs


You can also restart your application using PM2 to see if it resolves the issue:

bash

Copy code

pm2 restart app_name


If the problem persists, review your application’s code and dependencies for potential issues.

6. How do I configure my application to use a custom port?

Update the port number in your application’s configuration file or code. If you’re using environment variables, set the PORT variable to your desired port:

bash

Copy code

PORT=your_custom_port


Ensure that your PM2 configuration and Nginx (if used) are also set to route traffic to the new port.

7. How can I secure my Vultr server?

To secure your Vultr server:

  • Set up a firewall using ufw to allow only necessary traffic.
  • Use SSH key authentication instead of passwords for accessing your server.
  • Regularly update your server’s software and packages.
  • Create and use a non-root user for running your applications.

8. Can I use a domain name with my Node.js application on Vultr?

Yes, you can use a domain name. Update your domain’s DNS settings to point to your Vultr server’s IP address. Configure a web server like Nginx to handle requests to your domain and route them to your Node.js application.

9. How do I enable HTTPS for my application?

To enable HTTPS, you can use Certbot to obtain a free SSL certificate from Let’s Encrypt. Follow these steps:

Install Certbot and the Nginx plugin:
bash
Copy code
sudo apt install certbot python3-certbot-nginx


Run Certbot to automatically configure SSL:
bash
Copy code
sudo certbot --nginx


  • Follow the prompts to complete the SSL setup.

10. What are some common PM2 commands I should know?

Here are some commonly used PM2 commands:

Start an application:
bash
Copy code
pm2 start app.js


List all processes:
bash
Copy code
pm2 list


Stop a process:
bash
Copy code
pm2 stop app_name


Restart a process:
bash
Copy code
pm2 restart app_name


View logs:
bash
Copy code
pm2 logs


11. How do I back up my Node.js application and data?

To back up your Node.js application:

  • Create regular backups of your application files and configurations.
  • Use tools like rsync or cloud storage solutions to automate the backup process.

For application data, ensure you have a backup strategy in place for your databases and any other persistent data.

12. Can I scale my application with Vultr?

Yes, Vultr provides options to scale your application. You can resize your server instance to a larger plan or deploy additional instances and use load balancers to distribute traffic. PM2 also supports clustering, which allows you to run multiple instances of your application on a single server for better performance.

Get in Touch

Website – https://www.webinfomatrix.com

Mobile - +91 9212306116

Whatsapp – https://call.whatsapp.com/voice/9rqVJyqSNMhpdFkKPZGYK

 Skype – shalabh.mishra

Telegram – shalabhmishra

Email - info@webinfomatrix.com