Categories: Others

How to host next js app on cpanel

How to host next js app in cpanel

Login > Setup Node.js App > Create Application
Enter the values shown below. Change it as per your values

DEMO FILES : https://drive.google.com/drive/folders/1ubfVxTsmb3lgiVk0Na965yKXNKEF8Lgv?usp=drive_link

Node.js version = 20.18.3
Application mode = Production
Application root = domains/dnsflock.com/public_html
Application URL = (select your domain/subdomain here)
Application startup file = server.js
Passenger log file = BLANK

 

Click “Create”.

Upload files to public_html. Add a new file server.js with the below content

const { createServer } = require(‘http’)
const { parse } = require(‘url’)
const next = require(‘next’)

const dev = process.env.NODE_ENV !== ‘production’
const hostname = ‘localhost’
const port = 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass true as the second argument to url.parse.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === ‘/a’) {
await app.render(req, res, ‘/a’, query)
} else if (pathname === ‘/b’) {
await app.render(req, res, ‘/b’, query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error(‘Error occurred handling’, req.url, err)
res.statusCode = 500
res.end(‘internal server error’)
}
})
.once(‘error’, (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})

 

You need to stop your application and then click “Run NPM install”. Wait until it is completed successfully and then start the app. It should work fine.

 

Useful commands:
source /home/demouser/nodevenv/domains/dnsflock.com/public_html/20/bin/activate && cd /home/demouser/domains/dnsflock.com/public_html
cloudlinux-selector list –interpreter nodejs –user demouser
cloudlinux-selector get –interpreter nodejs –get-selector-status
cloudlinux-selector get –json –interpreter nodejs –get-selector-status
cloudlinux-selector stop –json –interpreter nodejs –user demouser –app-root /home/demouser/domains/dnsflock.com/public_html
cloudlinux-selector start –json –interpreter nodejs –user demouser –app-root /home/demouser/domains/dnsflock.com/public_html
cloudlinux-selector restart –json –interpreter nodejs –user demouser –app-root /home/demouser/domains/dnsflock.com/public_html

 

Sufiyan Shaikh

Recent Posts

CyberPanel Server Error (500)

Server Error (500) when trying to access the Cyberpanel If you are getting this error while accessing the cyberpanel, there…

1 year ago

How to connect and use github with DirectAdmin git feature

How to connect git with DirectAdmin Login into SSH and run the following command to create a new key pair…

1 year ago

WHMCS Development environment set

Update via PHPMYADMIN: Disable Captcha : UPDATE tblconfiguration SET value = '' WHERE setting = 'CaptchaSetting'; Update WHMCS link: UPDATE…

1 year ago

Lagom WHMCS notification hide pages list

3dsecure Access Denied Affiliates Affiliates Signup Announcements Banned Configure Products Downloads Downloads Denied Error Login Logout Password Reset Ticket Feedback…

1 year ago

Export/Import database via command line(ssh)

Export Database via SSH mysqldump --routines -u databaseusername -p databasename > database.sql   Import Database via SSH mysql -u databaseusername…

1 year ago

Correct File permissions for WordPress

If you are facing issue with file permissions, run this command within the public_html to fix the permissions issue find…

2 years ago