Simple DevOps Project: Deploying a Node.js Application on a Nginx Server
This blog is for DevOps Beginners. With the help of this blog, they can setup nginx server and easily deploy a sample Nodejs application
DevOps, a combination of development and operations, is a practice that aims to streamline the software development process by integrating various tools and technologies. In this blog post, I will guide you through a simple DevOps project that involves deploying a Node.js application on an Nginx server.
Before we begin, make sure you have the following:
A Linux-based server with Nginx installed
Node.js and npm (Node Package Manager) installed on your local machine
A basic understanding of Linux commands
Setting up Your Node.js Application
Before you begin deploying, you need to have a Node.js application. Here's a simple "Hello World" application:
To Install the nodejs, npm & run the application, you would use the following command:
Installing PM2 to Manage Your Application
PM2 is a process manager for Node.js applications. It will help you keep your app alive and manage it without downtime.
Install PM2 globally using npm:
Start your application with PM2:
Install Nginx using your operating system’s package manager. For example, on Ubuntu:
Configuring Nginx as a Reverse Proxy
Configure Nginx to forward requests to your Node.js application. Edit the default Nginx configuration file:
Add the following server block inside the http block:
Replace example.com with your domain name or IP address.
After saving the configuration file, restart Nginx to apply the changes:
Securing Nginx with Let's Encrypt (Optional)
For production, you should secure your application using SSL/TLS. Let's Encrypt provides free certificates:
Install Certbot and its Nginx plugin:
Obtain and install a certificate:
Follow the on-screen instructions to finish the setup.
Verifying the Deployment
Now you can verify that your application is accessible through the browser by visiting http://example.com. You should see the "Hello World" message served through Nginx.
Monitoring and Logs
Check the status of your Node.js application with PM2:
You have now successfully deployed a Node.js application using Nginx as a reverse proxy. Your application is now more robust, thanks to PM2, and secure if you opted to set up SSL/TLS with Let's Encrypt. Remember to replace the sample code and domain names with your actual application code and domain information.