DEV Community

Cover image for Deploy Node App to AWS EC2 Instance.
Kingsley Odim
Kingsley Odim

Posted on

Deploy Node App to AWS EC2 Instance.

Deploying a Node.js application on an EC2 instance and configuring a domain to point to it while ensuring the app keeps running involves several steps. Here's a comprehensive guide to achieve this:

Prerequisites

  1. AWS Account
  2. Domain name
  3. Basic knowledge of Node.js, SSH, and Linux command line

Steps

  1. Launch an EC2 Instance
    • Log in to your AWS Management Console.
    • Navigate to the EC2 Dashboard.
    • Click "Launch Instance".
    • Choose an Amazon Machine Image (AMI), preferably Ubuntu or Amazon Linux.
    • Choose an instance type (e.g., t2.micro for small applications).
    • Configure instance details, storage, and tags as needed.
    • Configure Security Group to allow HTTP (port 80), HTTPS (port 443), and SSH (port 22) access.
    • Review and launch the instance, then download the key pair for SSH access.
  2. Set Up the EC2 Instance

    • Connect to your EC2 instance via SSH:

      ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-ip
      
      
- Update the package list and install the necessary dependencies:
Enter fullscreen mode Exit fullscreen mode
    ```
    sudo apt update
    sudo apt install nodejs npm
    sudo npm install -g pm2

    ```
Enter fullscreen mode Exit fullscreen mode
  1. Deploy Your Node.js Application

    • Transfer your application code to the EC2 instance using scp or by cloning from a Git repository:

      scp -i /path/to/your-key-pair.pem -r /local/path/to/your-app ubuntu@your-ec2-public-ip:/home/ubuntu
      
      
- Navigate to your application directory and install dependencies:
Enter fullscreen mode Exit fullscreen mode
    ```
    cd /home/ubuntu/your-app
    npm install

    ```
Enter fullscreen mode Exit fullscreen mode
- Start your Node.js application using PM2 to keep it running:
Enter fullscreen mode Exit fullscreen mode
    ```
    pm2 start app.js
    pm2 save
    pm2 startup

    ```
Enter fullscreen mode Exit fullscreen mode
  1. Set Up a Domain Name
    • Go to your domain registrar and configure the DNS settings:
      • Create an A record pointing to your EC2 instance's public IP address.
    • (Optional) For SSL, consider using AWS Certificate Manager and setting up an Application Load Balancer (ALB).
  2. Configure a Reverse Proxy with Nginx (Optional but Recommended)

    • Install Nginx:

      sudo apt install nginx
      
      
- Configure Nginx as a reverse proxy:
Enter fullscreen mode Exit fullscreen mode
    ```
    sudo nano /etc/nginx/sites-available/default

    ```
Enter fullscreen mode Exit fullscreen mode
    - Replace the contents with the following (adjust paths and server name as needed):
Enter fullscreen mode Exit fullscreen mode
        ```
        server {
            listen 80;
            server_name your-domain.com;

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

        ```
Enter fullscreen mode Exit fullscreen mode
- Test the Nginx configuration and restart Nginx:
Enter fullscreen mode Exit fullscreen mode
    ```
    sudo nginx -t
    sudo systemctl restart nginx

    ```
Enter fullscreen mode Exit fullscreen mode
  1. Access Your Application
    • Navigate to your domain in a web browser to see your deployed Node.js application.

Additional Tips

  • Security: Regularly update your EC2 instance and installed packages.
  • Monitoring: Use PM2's monitoring tools to keep track of your application's performance.
  • Backups: Consider setting up automatic backups of your EC2 instance and application data.

This guide provides a basic setup. For production environments, you may want to look into more advanced configurations, such as load balancing, auto-scaling, and using a managed database service like Amazon RDS.

Top comments (0)