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

How Nmap identifies the Operating Systems?

‘Overlay Software’ and impact on CRE O&M Digital Transformation

Startup Lessons: It’s Time to Improve Your Product Engineering Conversation

No-Brainer HTML, JS, CSS and cypress.io testing starter tutorial — Part 1

How I Learned to Code

DOCKER: POSTGRESQL & PGADMIN

Passing Passwords in Selenium with Python

[LeetCode] #1217. Play with Chips

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

Elastic Beanstalk — Pros and Cons

How to deploy your Next.js app on Cloudflare pages

Setting up NextAuth.js application and deploying it via Vercel

Automated Responsive Images Using React and Cloudflare Images