Home > AI > Server > Godaddy >

How to deploy a NodeJs app to cPanel

Step 1: Begin by crafting a simple app.js file within the public_html directory and execute it using the command node app.js. Verify its functionality by issuing the command curl http://127.0.0.1:3000 and observing the resulting feedback.

const http = require('http')

const hostname = '127.0.0.1'
# const hostname = 'english92.com'
const PORT = process.env.PORT || 3000

const server = http.createServer( (req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/plain')
  res.end('Hello World! I"m your new NodeJS app!')
});

server.listen(PORT, hostname, () => {
  console.log(`Server running at http://${hostname}:${PORT}`);
})

Step 2: Navigate to cPanel / Application Manager / Register application. Below are the settings.

FieldValue
Application Nameapp
Deployment Domainenglish92.com
Base Application URLenglish92.com/
Application Path/english92/public_html
Deployment EnvironmentProduction
Environment Variables
PORT
3000

Step 3: Navigate to cPanel / File Manager followed by the public_html folder. Open the Settings tab located in the top right corner to reveal hidden files, facilitating the editing of .htaccess. Insert the provided lines accordingly.

# 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]



# english92.com
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ http://english92.com:3000/$1 [P,L]

You can now visit http://english92.com in your browser to view the outcome.

Leave a Reply