avatar

Niki Tech

受託開発のほかにも、独自に開発しているアプリもあります🚀

🛠Deploying a Simple Node.js v18 Application to an EC2 Instance

2024-06-10

This guide will walk you through the steps to manually deploy a simple Node.js v18 application to an AWS EC2 instance.

Prerequisites

  1. AWS Account: You need an AWS account.
  2. EC2 Instance: An Amazon Linux 2 EC2 instance.
  3. SSH Key: Access to the .pem file for SSH access to your EC2 instance.
  4. Node.js Application: A simple Node.js application to deploy.

Step 1: Connect to Your EC2 Instance

  1. Open your terminal.

  2. Change the permissions of your .pem file:

    chmod 400 your-key.pem
  3. Connect to your EC2 instance (Ubuntu or Amazon Linux):

    ssh -i your-key.pem ubuntu@ec2-your-ec2-public-dns
    ssh -i your-key.pem ec2-user@your-ec2-public-dns

Step 2: Update the System and Install Node.js

  1. Update the package list:

    sudo yum update -y
  2. Install nvm (Node Version Manager):

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  3. Load nvm:

    export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  4. Install Node.js v18:

    nvm install 18 nvm use 18
  5. Verify the installation:

    node -v npm -v

Step 3: Transfer Your Node.js Application to the EC2 Instance

  1. On your local machine, compress your Node.js application folder:

    tar -czf myapp.tar.gz ~/Downloads
  2. Transfer the compressed file to your EC2 instance:

    scp -i your-key.pem myapp.tar.gz ubuntu@ec2-your-ec2-public-dns:~
  3. Connect to your EC2 instance and extract the application:

    ssh -i your-key.pem ec2-user@your-ec2-public-dns tar -xzf myapp.tar.gz cd your-app-directory
  4. Or transfer folder directly to EC2 instance (should remove .git and node_modules folder before transfering):

    scp -i your-key.pem -r ~/your_local_folder/ ec2-user@your-ec2-public-dns:./your_remote_folder/

Step 4: Install Dependencies and Start the Application

  1. Install the application dependencies:

    npm install
  2. Start your application:

    node app.js

    or

    node app.mjs

    Replace app.js or app.mjs with the entry point of your application.

Step 5: Setup PM2 to Keep the Application Running

  1. Install PM2 globally:

    npm install -g pm2
  2. Start your application with PM2:

    pm2 start app.js

    or

    pm2 start app.mjs
  3. Save the PM2 process list and configure PM2 to start on boot:

    pm2 save pm2 startup

    Run the command output by pm2 startup to complete the setup.

Step 6: Fix error puppeteer in Amazon Linux 2023

Error: Failed to launch the browser process!

in Amazon server

sudo yum install libXcomposite libXdamage libXrandr libxkbcommon pango alsa-lib atk at-spi2-atk cups-libs libdrm mesa-libgbm

In Ubutun

sudo apt-get install chromium-browser sudo apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget sudo apt-get install -y libgbm-dev

Updated 2025/3/31

sudo apt install --reinstall libxfixes3=1:6.0.0-1

Conclusion

You have now manually deployed a simple Node.js v18 application to an AWS EC2 instance. You can manage your application using PM2 and ensure it runs continuously.