Deploying Nodejs app on Ubuntu server with pm2 & nginx

Deploying Nodejs app on Ubuntu server with pm2 & nginx

Deploying a Node.js application on an Ubuntu server can be a daunting task, but with the right tools, it can be done with ease. In this tutorial, we will go over how to deploy a Node.js application using the process manager pm2 and the web server Nginx.

Prerequisites

Before we get started, make sure you have the following installed on your Ubuntu server:

  • Node.js and npm

  • pm2

  • Nginx

If you don't have these installed, follow the instructions on their respective websites to install them.

Step 1: Set up the Application

First, clone your Node.js application to the server's file system. Make sure all the necessary dependencies are installed using the npm install command. Once everything is installed, you can start the application using the command npm start.

Step 2: Set up pm2

pm2 is a process manager that will keep your application running in the background, even after you exit the terminal. To set up pm2, run the command pm2 start <your-app.js> in the root directory of your application. This will start your application and create a pm2 process.

The Advantages of Using pm2

The pm2 process manager offers several advantages that make it an excellent choice for running Node.js applications in production. For starters, pm2 can automatically restart your application if it crashes, ensuring that your application is always available to your users. Additionally, pm2 can monitor your application for memory leaks and other issues, allowing you to identify and fix problems before they become critical.

Step 3: Set up Nginx

Nginx is a web server that will handle all incoming requests to your application. To set up Nginx, create a new configuration file in the /etc/nginx/sites-available directory. In this file, add the following code:

server {
    listen 80;
    server_name <your-domain-name>;

    location / {
        proxy_pass <http://localhost>:<your-port>;
        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;
    }
}

Replace <your-domain-name> with your domain name and <your-port> with the port your application is running on. Once you've saved the configuration file, create a symbolic link to the sites-enabled directory by running the command sudo ln -s /etc/nginx/sites-available/<your-config-file> /etc/nginx/sites-enabled/.

Finally, restart Nginx by running the command sudo service nginx restart.

The Benefits of Using Nginx

Nginx offers several advantages that make it an excellent choice for serving web content. For starters, Nginx can handle a large number of concurrent connections, making it ideal for high-traffic websites. Additionally, Nginx can cache static content, reducing the load on your application and improving performance.

Step 4: Test Your Application

Your application should now be accessible through your domain name. Test it by navigating to your domain name in a web browser. If everything is working correctly, you should see your application running.

Conclusion

With these tools, you can ensure that your nodejs application is always available to your users and that it performs well under high-traffic conditions.