Technology
Deploying a Node.js Application on an NGINX Server: A Comprehensive Guide
Deploying a Node.js Application on an NGINX Server: A Comprehensive Guide
In today's web development landscape, deploying a Node.js application using an NGINX server is a common and efficient approach. This guide will walk you through the essential steps to achieve this, using a process manager like PM2 or forever, and setting up NGINX as a reverse proxy.
Prerequisites
Before diving into the configuration, ensure that your system meets the following prerequisites:
You have a server setup with Ubuntu or a similar Linux distribution. Node.js is installed on your server machine. Nginx is installed and configured on your server. The domain name is registered and maps to your server IP. A DNS resolver is in place to handle the domain name resolution.Step 1: Running the Node.js App with a Process Manager
To ensure your Node.js application runs smoothly and is managed efficiently, you should use a process manager. Two popular choices are PM2 and forever. This section will outline how to use each of these tools.
Using PM2
Create or navigate to your Node.js project directory. Install PM2 globally using npm: $ npm install -g pm2 Start your application with PM2: $ cd /path/to/your/project $ pm2 start app.js To make sure the application starts automatically on server boot, use: $ pm2 startup $ pm2 saveUsing forever
Create or navigate to your Node.js project directory. Install forever globally using npm: $ npm install -g forever Start your application with forever: $ cd /path/to/your/project $ forever start app.jsStep 2: Configuring NGINX as a Reverse Proxy
Nginx is often used as a reverse proxy for Node.js applications because it can handle static content, provide caching and load balancing, and speed up the performance of the application.
Step 2.1: Creating the Nginx Configuration File
Start by editing the Nginx configuration file. Typically, this is located in: /etc/nginx/sites-available/default Add the following configuration to handle the reverse proxy: server { listen 80; server_name your_; # Replace with your domain name location / { proxy_pass http://127.0.0.1:3000; # Replace with your Node.js app'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; } }Step 2.2: Testing and Restarting Nginx
After saving the configuration, test your Nginx configuration for syntax errors: $ sudo nginx -t Restart Nginx to apply the changes: $ sudo systemctl restart nginxStep 3: Mapping Your Server IP to Your Domain Name
To make your application accessible via your domain name, you need to configure your DNS resolver to point your domain to your server's IP address.
Using Cloudflare
Sign in to your Cloudflare account. Go to the DNS settings for your domain. Add a CNAME record for the appropriate subdomain (e.g., www) and an A record for the root domain. Ensure the DNS propagation is complete.Using Google Domains
Sign in to your Google Domains account. Go to the DNS management of your domain. Create an A record pointing to your server's IP address. Create a CNAME record if you want to use a subdomain like www. Ensure the DNS propagation is complete.Step 4: Accessing Your Application
With all the configurations in place, you can now access your Node.js application via your domain name. You should be able to navigate to https://your_ or _, depending on the DNS records you set up.
Conclusion
This guide provides a comprehensive overview of deploying a Node.js application on an NGINX server. By following the steps outlined here, you can set up your application to run efficiently and be accessible via a domain name.
To learn more about this process, you can refer to this detailed resource: How To Set Up a Node.js Application for Production on Ubuntu 16.04 — DigitalOcean.