Next.js is a fast and popular React framework for building dynamic websites. It supports server-side rendering, automatic code splitting and performance optimizations out of the box. Next.js also allows publishing websites as a mix of both static and server-side rendered pages.
Hosting NextJs website on aws ec2
- Setup NodeJs 16 on aws ec2 instance as per instructions on aws docs.
- Create a directory /app on aws ec2 instance for hosting NextJs app files.
- Remove or move out node_modules, .git, .next and other unnecessary folders/files from your local NextJs app folder. Alternately you can also clone your repo directly in ec2 instance and skip next step.
- Copy source files from your local app folder to ec2 instance with command
scp -i key.pem -r local-folder ec2-user@instance.amazonaws.com:/app
- ssh into ec2 instance and run
npm install && npm run build
- Redirect nodejs port 3000 to port 80 with cmd sudo iptables -t nat -A PREROUTING -p tcp —dport 80 -j REDIRECT —to-ports 3000
- Start NextJs app with command
npm run start