This guide explains how to set up a Node.js backend on an Ubuntu EC2 instance and configure it to run in a production environment using Nginx as a reverse proxy.
- Go to the AWS Console and launch a new EC2 instance with Ubuntu as the operating system.
- In the Security Group, allow port 80 (HTTP) and port 22 (SSH) access.
- After creating the instance, download the
.pemkey for SSH access.
Use the following command to connect:
ssh -i your-key.pem ubuntu@<your-ec2-public-ip>- Navigate to the desired directory and clone your project:
git clone https://github.com/your-username/your-repository.git
cd your-repository- Install dependencies:
npm install- Update package list and install Node.js & NPM:
sudo apt update
sudo apt install nodejs
sudo apt install npm- Verify installation:
node -v
npm -vEnsure the scripts section in package.json has the following:
"scripts": {
"start": "node index.js"
}Replace index.js with your server file if necessary.
Install Nginx to serve as a reverse proxy:
sudo apt update
sudo apt install nginxCheck if Nginx is working by visiting http://<your-ec2-public-ip>.
- Create a new configuration file:
sudo nano /etc/nginx/sites-available/login.conf- Add the following configuration:
server {
listen 80;
server_name <your-ec2-public-ip>;
location / {
proxy_pass http://localhost:8080; # Your Node.js app runs on port 8080
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;
}
}- Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/login.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx- Create the
.envfile:
nano .env- Add environment variables:
PORT=8080
MONGO_CONN=
JWT_SECRET="sec-123"PM2 ensures your app runs continuously:
- Install PM2 globally:
sudo npm install pm2 -g- Start the app with PM2:
pm2 start index.js # Replace with your main file name- Set PM2 to restart the app on reboot:
pm2 startup
pm2 save- Open a browser and visit
http://<your-ec2-public-ip>. Your app should be up and running.
Your Node.js backend is now live on AWS EC2 with Nginx as a reverse proxy. The app runs continuously with PM2, and .env keeps your environment variables secure. π
This section explains how to deploy a Vite frontend app on your EC2 instance.
- EC2 instance (Ubuntu or other Linux OS)
- Node.js and npm installed
- Git installed
- Vite project (React, Vue, or other)
- Security Groups configured for port 5173
- SSH into the EC2 instance:
ssh -i your-key.pem ubuntu@<your-ec2-ip>- Install Node.js and npm:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs- Verify the installation:
node -v
npm -v- Install Git (if not already installed):
sudo apt install git- Clone your Vite project:
git clone https://github.com/your-username/your-vite-project.git
cd your-vite-project- Install dependencies:
npm installCreate or update the .env file for necessary configurations, such as:
VITE_URL="http://<backend-ec2-ip>/auth"Run the app and make it accessible:
npm run dev -- --hostCheck the output:
Local: http://localhost:5173/
Network: http://<public-ec2-ip>:5173/- In AWS Console, navigate to Security Groups.
- Add inbound rule for port 5173 (or your Vite port).
From your browser:
http://<public-ec2-ip>:5173/Or using curl:
curl http://<public-ec2-ip>:5173/Use tmux, screen, or pm2 to keep the server running:
- Install tmux:
sudo apt install tmux- Start a tmux session:
tmux new -s frontend- Run your Vite app inside tmux:
npm run dev -- --host- Detach from tmux:
Press Ctrl + b, then d.
You have successfully set up a Vite frontend app on your EC2 instance with a Node.js backend. Your app is now accessible externally, and PM2 ensures it's always running.
Enjoy the smooth deployment of your applications on AWS! π
If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request! Don't hesitate to reach out if you face any problems during the setup processβI'm happy to help!