Setting Up Your EC2 Instance for Next.js Hosting

For any React Native app developer expanding into full-stack and web solutions, deploying a Next.js app on AWS EC2 is a power move. This setup combines the flexibility of cloud infrastructure with the performance of server-side rendering, ideal for businesses needing scalable, cross-platform app development. Whether you’re managing custom business app solutions or an enterprise-grade product, learning to host Next.js on EC2 builds confidence in creating seamless digital ecosystems that connect your mobile app development with robust, production-ready web experiences.

Drag

Be sure to use the desired region.

Drag

Create an SSH key pair and upload it to AWS before generating an EC2 instance. Use this key when setting up your EC2.

Drag

Install and Configure Apache

Drag

sudo apt update

Bash

Drag

sudo apt install apache2

Bash

Drag

sudo systemctl start apache2

Bash

Drag

sudo systemctl enable apache2

Bash

Drag

sudo a2enmod proxy_http

Bash

Drag

sudo systemctl restart apache2

Bash

Drag

Modify files in /etc/apache2/sites-available accordingly and enable and restart, if necessary:

Drag

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<domain>.conf

Bash

Drag

sudo rm -R /var/www/html

Bash

Drag

Example File:

Drag

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName <domain>
    ServerAlias <subdomain>.<domain>
    DocumentRoot /var/www/<project>
...
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Plain text

Drag

sudo a2dissite 000-default

Bash

Drag

sudo a2ensite <domain>

Bash

Drag

sudo systemctl reload apache2

Bash

Drag

Generate an SSL Certificate

Drag

Make sure to add the elastic IP address to the DNS records so the app resolves before completing the next steps.

Drag

sudo apt-get install certbot python3-certbot-apache

Bash

Drag

sudo certbot --apache

Bash

Drag

Install Node and NPM

Drag

sudo apt install nodejs

Bash

Drag

sudo apt install npm

Bash

Drag

Install PM2

Drag

sudo npm install pm2 -g 

Bash

Drag

Clone Project

Drag

ssh-keygen

Bash

Drag

Add your public key to GitLab through the Console.

Drag

sudo chown -R $USER /var/www

Plain text

Drag

git clone git@gitlab.com:<something>/<something>.git

Plain text

Drag

git fetch

Plain text

Drag

git checkout <branch>

Plain text

Drag

git pull

Plain text

Drag

Start App

Drag

Add your variables with:

Drag

sudo nano .env

Plain text

Drag

Inside the project run

Drag

npm install

Plain text

Drag

npm run build

Bash

Drag

pm2 start npm --name <project> -- run start -- -p 3000

Bash

Drag

Gotchas

Drag

Cloning your Project

Drag

Do not use sudo when cloning your project.

Drag

Make Changes

Drag

Pull, install, and:

Drag

pm2 restart <project>

Bash

Drag

Deploying a Next.js app on AWS EC2 gives you a solid foundation for growth, from performance-driven websites to cross-platform apps that feel polished, fast, and dependable. For any React Native app developer, understanding how your web and mobile layers connect is what transforms good code into great user experiences. With custom app development and ongoing support in mind, this workflow keeps your projects secure, scalable, and ready to meet enterprise demands without losing the creative edge your users love.

Drag

If you’re ready to bring your Next.js, Expo, or React Native project to life, or need expert guidance deploying your app on AWS, let’s talk. As a React Native app developer and mobile software engineer, I help startups and organizations build, launch, and scale cross-platform apps that perform beautifully across iOS, Android, and the web. Reach out today!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *