|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
Key to Financial Data Visualization: How Selection of Graphs make data more informative?

Explore how various graph and visualization tools, including line graphs, pie charts, bar graphs, treemaps, KPI dashboards, waterfall charts, and heat maps, can transform financial data analysis. Learn how selecting the right visualization method helps in accurate data interpretation and impactful presentations.

Flutter
Accelerating Mobile App Development: Dallo Tech’s 20-Day Sprint with Flutter for Jeevan Vigyan Mobile App

Building a mobile app can be tough and often takes several months to complete. But with good proper planning and precise execution, remarkable results can be achieved in a very short timeframe. An illustrative example of this efficiency lies in the Jeevan Vigyan Mobile app made by Dallo Tech.

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.

DevOps
Deploying Next.js using Docker Compose

Learn how to deploy your Dockerized Next.js application on a remote server with this tutorial. Simplify your deployment process and manage your application efficiently using Docker Compose. Follow step-by-step instructions to push your Docker image to a repository, pull it onto your remote server, and run your Next.js app seamlessly.

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