Deploying a Next.js website on an AWS EC2 machine involves several steps. Below is a general guide to help you with the deployment process. Please note that this is a basic guide, and you may need to adapt the steps based on your specific project requirements.

Prerequisites:

1. AWS Account: Make sure you have an AWS account.

2. EC2 Instance: Launch an EC2 instance with the desired specifications.

3. Domain Name (Optional): If you have a domain, configure it to point to your EC2 instance.

Steps to Deploy:

1. Connect to Your EC2 Instance: Use SSH to connect to your EC2 instance:

ssh -i your-key.pem ec2-user@your-ec2-instance-ip

2. Install Node.js and NPM: Update your package list and install Node.js and npm:

sudo yum update -y
sudo yum install nodejs
sudo yum install npm

3. Copy Your Next.js Project to the Server: You can use scp or other methods to copy your Next.js project to the server.

scp -i your-key.pem -r /path/to/your/project ec2-user@your-ec2-instance-ip:/path/on/server

4. Install Dependencies: Navigate to your project directory and install dependencies:

cd /path/on/server
npm install

5. Build Your Next.js App: Build your Next.js app for production:

npm run build

6. Run Your Next.js App: Start your Next.js app using a process manager (e.g., PM2):

npm install -g pm2
pm2 start npm --name "your-app-name" -- start

7. Configure Nginx (Optional):

If you want to use Nginx as a reverse proxy, install and configure it:

sudo yum install nginx

Create an Nginx configuration file and update it with your settings:

sudo nano /etc/nginx/conf.d/your-app.conf

Example Nginx Configuration:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:your-app-port;
        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;
    }
}

Restart Nginx:

sudo service nginx restart

8. Access Your Next.js App: Open your browser and access your Next.js app:

http://your-ec2-instance-ip:your-app-port

If you configured a domain, use:

http://your-domain.com

That’s it! Your Next.js app should now be deployed on your AWS EC2 instance. Keep in mind that this is a basic setup, and you may need to adjust configurations based on your specific project requirements. Additionally, consider using a process manager like PM2 to keep your app running in the background.

Leave a Reply

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