DEV Community

Cover image for DevOps Labs : Guide to Setup Node.js 23, React, NestJS, Next.js, and Nginx on EC2 Ubuntu 24.04

DevOps Labs : Guide to Setup Node.js 23, React, NestJS, Next.js, and Nginx on EC2 Ubuntu 24.04

Guide to Setup Node.js 23, React, NestJS, Next.js, and Nginx on EC2 Ubuntu 24.04

Table of Contents


Install Node.js 23

Step 1: Update and Install Dependencies

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl gnupg2 lsb-release ca-certificates
Enter fullscreen mode Exit fullscreen mode

Step 2: Add NodeSource Repository

curl -fsSL https://deb.nodesource.com/setup_23.x | sudo -E bash -
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Node.js 23

sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Installation

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Setup React Application

Step 1: Install create-react-app and Create a Project

npx create-react-app my-react-app
cd my-react-app
npm install
Enter fullscreen mode Exit fullscreen mode

Step 2: Start React Application

npm start
Enter fullscreen mode Exit fullscreen mode

Default React app runs on port 3000.


Install and Configure Nginx for React

Step 1: Install Nginx

sudo apt install -y nginx
Enter fullscreen mode Exit fullscreen mode

Step 2: Enable and Start Nginx

sudo systemctl enable nginx
sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Nginx Proxy for React

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace with the following configuration:

server {
    listen 80;

    server_name your_domain_or_IP;

    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

Step 4: Test and Restart Nginx

sudo nginx -t
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Setup NestJS and Next.js Applications

Step 1: Install NestJS CLI and Create a New Project

npm install -g @nestjs/cli
nest new my-nest-app
cd my-nest-app
npm install
Enter fullscreen mode Exit fullscreen mode

Step 2: Change NestJS Port to 4000

Edit src/main.ts and modify:

await app.listen(4000);
Enter fullscreen mode Exit fullscreen mode

Step 3: Start NestJS Server

npm run start
Enter fullscreen mode Exit fullscreen mode

Step 4: Setup Next.js Application

npx create-next-app@latest my-next-app
cd my-next-app
npm install
Enter fullscreen mode Exit fullscreen mode

Step 5: Change Next.js Port to 3001

Modify package.json scripts:

"scripts": {
  "dev": "next dev -p 3001"
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Start Next.js Application

npm run dev
Enter fullscreen mode Exit fullscreen mode

Configure Nginx for NestJS and Next.js

Step 1: Edit Nginx Configuration

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace with:

server {
    listen 80;
    server_name your_domain_or_IP;

    location /react {
        proxy_pass http://localhost:3000;
    }

    location /nestjs {
        proxy_pass http://localhost:4000;
    }

    location /next {
        proxy_pass http://localhost:3001;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Test and Restart Nginx

sudo nginx -t
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Start and Verify Applications

Step 1: Start Applications

cd ~/my-react-app && npm start &
cd ~/my-nest-app && npm run start &
cd ~/my-next-app && npm run dev &
Enter fullscreen mode Exit fullscreen mode

Step 2: Access Applications via Nginx

  • React App: http://your_domain_or_IP/react
  • NestJS App: http://your_domain_or_IP/nestjs
  • Next.js App: http://your_domain_or_IP/next

Troubleshooting

1. Nginx Not Starting?

Check logs:

sudo journalctl -xe
sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

2. React/NestJS/Next.js Not Running?

Check logs:

pm2 logs
Enter fullscreen mode Exit fullscreen mode

Or restart manually:

node app.js 2>&1 | tee error.log
Enter fullscreen mode Exit fullscreen mode

Conclusion

You have successfully:

  • Installed Node.js 23.
  • Set up React, NestJS, and Next.js applications.
  • Configured Nginx as a reverse proxy.

If you face any issues, check logs and verify configurations.


🚀 Happy coding! 🚀

Top comments (0)