AKZN Notes

Archives for My Lazy and Forgetful Mind

Deploy NodeJs Application to VPS

Deploying a Node.js application to a Virtual Private Server (VPS) is a crucial step in bringing your web applications to life. In this guide, we'll walk you through the process of setting up your VPS, transferring your Node.js code, and configuring the server to host your application efficiently.

Deploy to VPS

  1. Set Up Your VPS:

    • Ensure you have a VPS with Ubuntu installed.
    • SSH into your VPS using the command: ssh username@your_server_ip.
  2. Install Node.js and npm:

    • Update the package list: sudo apt update.
    • Install Node.js and npm: sudo apt install nodejs npm.
  3. Upload Your Application:

    • You can use SCP or SFTP to upload your Node.js application to the VPS. For example: scp -r your_app_directory username@your_server_ip:/path/to/destination.
  4. Install Dependencies:

    • Navigate to your application directory on the VPS.
    • Run npm install to install your application's dependencies.
  5. Configure Environment Variables:

    • Set any environment variables your application requires.
  6. Set Up a Reverse Proxy (Optional):

    • If you want to run your Node.js application behind a web server like Nginx, set up a reverse proxy to forward requests to your Node.js app.
  7. Start Your Node.js Application:

    • Use a process manager like pm2 to run your Node.js application. Install pm2 globally if you haven't already: npm install -g pm2.
    • Start your app with pm2 start app.js (replace "app.js" with your application's entry point).
  8. Configure Firewall (Optional):

    • If you're using a firewall, make sure to allow traffic on the port your Node.js application is running on.
  9. Set Up Domain and DNS (Optional):

    • If you have a domain, configure DNS settings to point to your VPS IP address.
  10. Secure Your VPS:

    • Harden the security of your VPS by configuring SSH access, disabling root login, and setting up a firewall.
  11. Monitor and Maintain:

    • Regularly monitor your Node.js application and VPS to ensure they are running smoothly.

That's a high-level overview of deploying a Node.js application to a VPS. You may need to adapt these steps depending on your specific application and requirements.

AUTO Start using PM2

Managing Node.js Applications with PM2

PM2 (Process Manager 2) is a powerful process manager and process runner for Node.js applications. It helps you manage and monitor your Node.js applications, providing features like automatic restarts on crashes and process management. In this document, we'll cover key aspects of using PM2 to manage Node.js applications.

Installation

You can install PM2 globally using npm:

npm install -g pm2

Starting a Node.js Application

To start your Node.js application with PM2, use the pm2 start command. You can specify the application's entry file, and you can customize the process name:

pm2 start myapp.js --name mycustomname

but if we use .env, usually the script above will fail to load our env because on absolute path problem, so we need to make pm2 configuration inside our app root dir

/your-app/ecosystem.config.js

// this file are to be used with PM2 (Process Manager 2)
// PM2 is a powerful process manager and process runner for Node.js applications

module.exports = {
    apps: [
        {
            name: 'wa-bot',
            script: 'ABSOLUTE_PATH_TO_YOUR_APP_ROOT_DIR/app.js',
            cwd: 'ABSOLUTE_PATH_TO_YOUR_APP_ROOT_DIR',
        }
    ]
};

dont forget to delete your app from pm2 list first if you already add your app into pm2 before you make pm2 configuration. because it will not update the already created list (im my case)

then you can re-add your app into pm2 using

pm2 start ecosystem.config.js --name mycustomname

Process Monitoring

PM2 continuously monitors your Node.js application processes. If a process crashes, PM2 will automatically restart it. It can be configured to restart processes a specified number of times or indefinitely.

Logging

PM2 captures and saves console output, including console.log statements, from your Node.js application. Log files are created for each process and can be found in the ~/.pm2/logs directory. You can use PM2 commands to view and manage logs, such as:

pm2 logs <app-name>

Custom Process Names

You can name PM2 processes differently from your main application JavaScript file. Use the --name flag to specify a custom process name when starting your application:

pm2 start myapp.js --name mycustomname

Start on Boot

PM2 can generate startup scripts to ensure your Node.js application starts automatically when your VPS reboots. Use the pm2 startup command to create and enable the startup script.

Summary

PM2 is a valuable tool for managing Node.js applications in a production environment. It provides process monitoring, automatic restarts, logging, and custom process names, making it a robust choice for ensuring the availability and stability of your Node.js applications.

Leave a Reply

Your email address will not be published.