🛠Deploying a Simple Node.js v18 Application to an EC2 Instance
This guide will walk you through the steps to manually deploy a simple Node.js v18 application to an AWS EC2 instance.
Prerequisites
- AWS Account: You need an AWS account.
- EC2 Instance: An Amazon Linux 2 EC2 instance.
- SSH Key: Access to the
.pem
file for SSH access to your EC2 instance. - Node.js Application: A simple Node.js application to deploy.
Step 1: Connect to Your EC2 Instance
-
Open your terminal.
-
Change the permissions of your
.pem
file:chmod 400 your-key.pem
-
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
-
Update the package list:
sudo yum update -y
-
Install
nvm
(Node Version Manager):curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
-
Load
nvm
:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
-
Install Node.js v18:
nvm install 18 nvm use 18
-
Verify the installation:
node -v npm -v
Step 3: Transfer Your Node.js Application to the EC2 Instance
-
On your local machine, compress your Node.js application folder:
tar -czf myapp.tar.gz ~/Downloads
-
Transfer the compressed file to your EC2 instance:
scp -i your-key.pem myapp.tar.gz ubuntu@ec2-your-ec2-public-dns:~
-
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
-
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
-
Install the application dependencies:
npm install
-
Start your application:
node app.js
or
node app.mjs
Replace
app.js
orapp.mjs
with the entry point of your application.
Step 5: Setup PM2 to Keep the Application Running
-
Install PM2 globally:
npm install -g pm2
-
Start your application with PM2:
pm2 start app.js
or
pm2 start app.mjs
-
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.