Home

Deploy Next.js on DigitalOcean Droplet with SSL and Nginx

17 views

Deploying a Next.js application on a DigitalOcean Droplet involves a series of steps including server setup, configuring your application, and serving it using a process manager and web server. Below is a general guide on how to achieve this:

Step 1: Initial Droplet Setup

  1. Create a Droplet:

    • Log into your DigitalOcean account and create a new droplet.
    • Choose an image (typically Debian or Ubuntu), and select your plan.
    • Add SSH keys for security or set up a root password.
  2. SSH into the Droplet:

    ssh root@your_droplet_ip
    
  3. Update System Packages:

    apt update && apt upgrade -y
    
  4. Install Node.js and NPM:

    • Recommended via NodeSource for LTS version:
    curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
    apt install -y nodejs
    

Step 2: Deploy Next.js Application

  1. Clone App (or upload your code):

    • Use Git or SCP to get your Next.js application onto the server.
    git clone your_repo_url
    cd your_repo_directory
    
  2. Install Dependencies:

    npm install
    
  3. Build the Application:

    • This compiles your Next.js application into a production build.
    npm run build
    
  4. Start the Application:

    • For basic setup, use:
    npm run start
    
    • For production, it's recommended to use a process manager like PM2:
      npm install -g pm2
      pm2 start npm --name "your-app-name" -- start
      pm2 save
      pm2 startup # To start on system boot
      

Step 3: Setup Reverse Proxy with Nginx

  1. Install Nginx:

    apt install nginx -y
    
  2. Configure Nginx:

    • Open a new Nginx configuration file for your site:
      nano /etc/nginx/sites-available/your-site
      
    • Add the following configuration:
      server {
          listen 80;
          server_name your_domain.com;
      
          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;
          }
      }
      
  3. Enable the Configuration:

    ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
    nginx -t # Test the Nginx configuration
    systemctl restart nginx
    

Step 4: Secure Your App with SSL

  1. Install Certbot:

    apt install certbot python3-certbot-nginx -y
    
  2. Obtain SSL Certificate:

    certbot --nginx -d your_domain.com
    
  3. Auto-Renew SSL Certificate:

    • Certbot will set up a cron job for this automatically, or you can verify it:
    crontab -l
    

Conclusion

Your Next.js application should now be running on your Droplet, accessible via your domain name. The app is served through Nginx with a secure SSL certificate via Let's Encrypt. Remember to replace placeholders (like your_domain.com, your_repo_url, and your_app_name) with your specific details. For increased reliability, monitor the application using PM2 logs and ensure your firewall settings on DigitalOcean allow traffic on port 80 and 443.