Nginx Java Script React JS Node JS Angular JS Mongo DB Nginx AWS JAVA Python Type Script

NGINX Index

Node.js Deployment in NGINX

You read about what is NGINX and its advantages were before this page. On this page, we give some configuration steps for Node.js project deployment in NGINX with AWS.

1. Create Free AWS Account

first of all you should create free AWS Account. Amazon Web Services (AWS) is a comprehensive cloud computing platform provided by Amazon.com. Launched in 2006, AWS offers a wide array of cloud services that cater to businesses, individuals, and organizations, allowing them to access and utilize computing power, storage, and various other resources over the internet. Here we give some steps to create an AWS account

Go to the AWS Free Tier Page : Open your web browser and go to the AWS Free Tier page AWS Free Tier

2. Create and Launch an EC2 instance and SSH into machine

Amazon Elastic Compute Cloud (EC2) is a web service provided by Amazon Web Services (AWS) that allows users to run virtual servers in the cloud. EC2 instances provide scalable compute capacity, allowing you to easily scale up or down based on your application requirements. Here's an overview of key concepts and steps related to Amazon EC2. Steps to Launch an EC2 Instance:

Remember to terminate instances when you no longer need them to avoid unnecessary charges. EC2 offers various features and options for customization, making it a versatile solution for hosting applications in the cloud.

3. Connecting to an EC2 Instance

To connect to an Amazon EC2 instance running Linux, especially for a Node.js project, you'll typically use SSH (Secure Shell). Here's a step-by-step guide assuming you already have an EC2 instance set up:

bash

chmod 400 /path/to/your/keypair.pem

bash

ssh -i /path/to/your/keypair.pem ec2-user@your-instance-ip

4. Install Node.js on the EC2 Instance

Connect to your EC2 instance using SSH, and then install Node.js. You can use a package manager like `yum` for Amazon Linux or `apt` for Ubuntu

For Amazon Linux:

bash

sudo yum install -y nodejs

For Ubuntu:

bash

sudo apt update
sudo apt install -y nodejs

5. Transfer Your Node.js Project

You can use various methods to transfer your Node.js project to the EC2 instance. The most common two methods are scp (secure copy) and cloning your project from GitHub.You use only any one method

1. secure copy (scp)

bash

scp -i /path/to/your/keypair.pem -r /path/to/your/local/project ec2-user@your-instance-ip:/path/on/ec2

2. Clone your project from Github
If Git is not already installed on your EC2 instance, you can install it using the package manager. For example, for Amazon Linux:

If Git is not already installed on your EC2 instance, you can install it using the package manager.

For Amazon Linux:

bash

sudo yum install -y git

For Ubuntu:

bash

sudo apt update
sudo apt install -y git

After Clone the GitHub Repository, Navigate to the directory where you want to clone the GitHub repository and use the git clone command. Replace repository-url with the URL of the GitHub repository:

bash

git clone repository-url

For example:

bash

git clone https://github.com/username/repo.git

6. Install Project Dependencies

Navigate to your project directory and install dependencies. Start your Node.js application.

bash

npm install
npm start

7. Start Your Node.js Application With "pm2"

Before this page we covered about `pm2`.You might want to use a process manager like pm2 to keep your application running:

bash

npm install -g pm2
pm2 start your-app.js

8. Setup Firewall

Open the necessary ports in the firewall. Here we give some example commands for enable Firewall, check status of firewall and allow specific ports

bash

sudo ufw enable
sudo ufw status
sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)

Before this page, we explained in detail about Firewall commands. Those commands will help you understand about firewall Please check this link Basic FireWall Commands

9. Install NGINX and configure

To install NGINX and configure it for a Node.js project, follow these general steps. Keep in mind that specifics might vary based on your operating system and Node.js application setup.

  • 1. Install NGINX
  • bash

    sudo apt install nginx

  • 2. Configure NGINX for Your Node.js Project
    After install Nginx Create an NGINX server block (virtual host) configuration file. Replace `/etc/nginx/sites-available/myapp` with an appropriate path:
  • bash

    sudo nano /etc/nginx/sites-available/myapp

  • Add the following configuration. Modify the values of `server_name`, `location`, and `proxy_pass` based on your project details:
  • bash

    server {
        listen 80;
        server_name your_domain_or_server_ip;
        location / {
            proxy_pass http://localhost:your_node_app_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_or_server_ip` with your actual domain or server IP.Replace `your_node_app_port` with the port where your Node.js application is running.

  • 3. Test the NGINX configuration
    After configuring the file, test the NGINX configuration, This step helps ensure that your NGINX configuration file is free of syntax errors and is structurally correct before applying the changes and potentially causing issues. To test your NGINX configuration, use the following command:
  • bash

    sudo nginx -t

  • 4. Reload the NGINX
    This command is used to reload NGINX without stopping it, allowing you to apply configuration changes.
  • bash

    sudo nginx -s reload

    10. Install NGINX and configure

    To add SSL to your NGINX setup using Let's Encrypt, you can use the Certbot tool. Certbot is an easy-to-use tool that automates the process of obtaining and renewing SSL certificates. Here are the steps to add SSL with Let's Encrypt to your NGINX setup:

  • 1. Install Certbot

  • bash

    sudo apt update
    sudo apt install certbot

  • 2. Obtain SSL Certificate
    Run the following command to obtain an SSL certificate for your domain

  • bash

    sudo certbot --nginx -d your_domain_or_server_ip

  • Follow the interactive prompts to configure Certbot. It will automatically detect your NGINX setup and ask for your email address for renewal notifications.

  • 2. Automatically Renew Certificates
    Certbot will automatically set up a cron job to renew your certificates. However, it's a good practice to test the renewal process manually.If the dry run succeeds, it means the automatic renewal process is set up correctly.

  • bash

    sudo certbot renew --dry-run

  • Follow the interactive prompts to configure Certbot. It will automatically detect your NGINX setup and ask for your email address for renewal notifications.

  • 2. Test Your SSL Configuration
    Restart NGINX to apply the changes.

  • bash

    sudo systemctl reload nginx

  • Visit your site using `https://your_domain_or_server_ip` in a web browser to verify that SSL is working correctly.your NGINX server is configured with SSL using a Let's Encrypt certificate. Certbot will automatically handle the renewal of your SSL certificates, providing a secure connection to your Node.js application.

  • This guide provides a basic setup. Adjust configurations, security settings, and NGINX parameters based on your specific project requirements. Ensure your security groups and firewall rules allow traffic on ports 80 and any other ports your Node.js application uses.