How to deploy NextJs Project in Server.

  1. Edit package.json
"scripts": {
"dev": "node server.js",
"prod": "node server.js",
"build": "next build && next export",
"start": "NODE_ENV=production pm2 start server.js --watch",
"export": "npm run build && next export"
},

2. Create server.js

npm install express

const express = require('express');
const next = require('next');
const port = 3661;

const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();

app.prepare().then(() => {
const server = express();
server.all('*', (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`ready on http://localhost:${port}`)
})
}).catch(ex => {
console.error(ex.stack);
process.exit(1);

})

3. pm2 install:

sudo npm i -g pm2

4. Run Project:

npm start
npm start

browse:

http://localhost:3661/

5.Show pm2 list

pm2 listsudo npm i -g pm2
sudo npm install pm2 -g
pm2 list //list all//root
pm2 stop server.js --watch //pmt start
pm2 stop server.js --watch //pmt stop
pm2 start server.js --name "pm2_name" //pm2 start
//root
npm start
pm2 list
pm2 monitor
pm2 unmonitor
pm2 restart
# Display all apps logs in realtime
pm2 logs

# Display only `api` application logs
pm2 logs api

# Display new logs in json
pm2 logs --json

# Display 1000 lines of api log file
pm2 logs big-api --lines 1000
#You can also check logs with the CLI dashboard:
pm2 monit

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Week 7 AC Liquidity Mining Memo

Managing different environments and configurations in xcode for iOS projects

Doubly Linked Lists and How to Implement Them in Python 3

Nexus Repository Manager

Office space?

Access Granted

[Review/Book] The Art Of Readable Code — 1. Surface-Level Improvements

level 1. 문자열 내림차순으로 배치하기 (java)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Safaetul Ahasan

Safaetul Ahasan

Software Engineer

More from Medium

Deploy Blitz Js / NextJs to the Cloud… For people in a hurry!!

Creating speech to text application

Next.Js — User Authentication with WSO2 Identity Server

Ship a Next.js App with a CI/CD Pipeline.