|Blogs|Deploying Next.js project on the Linux server using PM2 with SSL certificate(https)
ringring
DevOps
Deploying Next.js project on the Linux server using PM2 with SSL certificate(https)
Published by:Anuj Poudel
Published on:03 Jan, 2024
blogimage
Share

This tutorial provides Step-by-Step Guide to Deploying Next.js Project with SSL Certificate Using PM2

Prerequisites

  • Hosted Linux server

  • Project on git

Step 1: Log in to the remote Linux server via ssh

Many Hosting services provide a Linux server to host the application. Amazon(EC2), and Linode are some popular hosted servers.

This tutorial is based on Ubuntu OS on the hosted Linux server, though procedures are almost similar for other distros of Linux.

We can log in to the Linux server using SSH(Secure Shell). From a Windows machine, we can use the PuTTY client for the connection and from Linux based system we can use the terminal.

ssh ubuntu@<your_ip_address>

Replace <your_ip_address> with the IP of your VPS


Step 2: Installing the required components

First of all, run an update on the Ubuntu server.

sudo apt update

Install Node.Js on VPS

curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh

Install npm, next, pm2 and nginx

sudo apt install -g npm
sudo apt install -g next
sudo npm install -g pm2

Step 3: Fetch the project from git

git clone your_repository_url project_name

It will clone the project inside the folder called project_name


Step 4: Build the NextJs project

For deployment purposes, we generally use the build files. So we need to generate the build folder

cd project_name
npm install --legacy-peer-deps
npm run build

A build folder is generated in the project directory it could be either .next or build which will be mentioned in the project configuration


Step 5: Running the project using pm2

pm2 start npm --name "your-project-name" -- start

Now your project should be running on the server’s 3000 port.

To check the log of the project use the following command

pm2 log your-project-name

 


Step 6: Writing Virtual Host Configuration

cd /etc/nginx/conf.d
nano yourdomain.conf

On the nano text editor paste the following script at the end of the file. (Replace yourdomain.com with your original domain)

map $http_upgrade $connection_upgrade {
    default         upgrade;
    ''              close;
}

server {
      server_name yourdomain.com www.yourdomain.com;
      location / {
       # Next.js project
       proxy_pass         http://127.0.0.1:3000;
       proxy_http_version  1.1;
       proxy_set_header    Host        $host;
       proxy_set_header    Upgrade     $http_upgrade;
       proxy_set_header    Connection  $connection_upgrade;
   }
}

Save the file

After this check the script and restart the Nginx server

sudo nginx -t
sudo systemctl restart nginx

Update the DNS A record of your domain to redirect to server IP and after some time visit http://yourdomain.com, you can find your site hosted.


Step 7: Install Certificate

Now we will install the SSL certificate for the site. We are using the Let’s Encrypt certificate provider.

cd ~
sudo apt install certbot python3-certbot-nginx
certbot --nginx -d yourdomain.com -d www.yourdomain.com

If successfully installed you will see the congratulatory message and you will find the certificate on /etc/letsencrypt/live/yourdomain.com/

Visiting /etc/nginx/conf.d/yourdomain.conf file, You will see the HTTPS rules automatically added to the configuration.

Now restart the Nginx server

sudo systemctl restart nginx

Now visit the site
https://yourdomain.com

Congratulations, you have successfully deployed Next.JS using PM2 along with an SSL certificate.

 

Other related blogs
Technology
Restore MSSQL Database file .bak on a Linux Server

If you need to restore a MSSQL database file (.bak) on a Linux server, this is the perfect blog for you. Additionally, anyone interested in web development, database management, or technology in general may also find this useful and interesting.

Technology
Hexagonal Architecture in NestJS with Sample Code

NestJS is javascript server side progressive framework combining Object Oriented Programming(OOP) and Functional Programming(FP). Since I got introduced to this framework, I have been loving it. Personal Opinion: At first, I became familiar with NestJS framework. After that, I wrote my first API , my first NestJS package and so on. But I was still exploring new things. This time, I wanted to learn about Hexagonal Architecture. Learning never stops !!! Rather than talking about theories, let’s directly dive into codes. I will be referencing code from the below mentioned repository. Github Link: Nestjs-Hexagonal-Architecture-Demo-Project

DevOps
How to Set Up CI/CD pipeline on Next.js project using GitLab on AWS EC2

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern web development, ensuring fast, reliable, and automated software delivery. In this guide, we’ll walk you through setting up a CI/CD pipeline for a Next.js application using GitLab CI/CD. By automating the build and deployment process, you can eliminate manual tasks, reduce errors, and deploy updates seamlessly. Whether you're deploying to a VPS, cloud server, or a containerized environment, this tutorial will help you streamline the deployment of your Next.js app using Docker, GitLab CI/CD, and SSH. Let’s dive into automating your Next.js project for efficient, hassle-free deployment!

DevOps
Automating Next.js Application Deployment with CI/CD Using GitHub Actions

Learn how to automate the deployment of your Next.js application using GitHub Actions and Docker, streamlining the CI/CD process. Follow step-by-step instructions to set up a CI/CD pipeline, configure GitHub Actions workflows, and deploy your Next.js app seamlessly to a remote server.

Discover Dallo Tech, a leading software development company offering expertise in Mobile and Web App Development, Data Analytics, Visualization, AI, and Blockchain solutions. Elevate your digital journey with our innovative technology services.

Open Hours

Sun - Fri:
10:00 am - 5:00 pm

Saturday:
CLOSED

Dallotech as tech partner
Business Team
Devops and Service
Design Team
Quality Assurance
Software Team
Mobile Team
Hire Expert Team
© 2018 - 2025 . DalloTech Pvt. Ltd. All Rights Reserved