Struggling to deploy my Node.js website

Hi,

I am very new to Cyberpanel and I have recently setup a CentOS 7 VPS to deploy my Node.js website.

Cyberpanel details below.

Current Cyberpanel Version: 2.3

Build: 4

Current Commit: 6eeaca15d3bb0defa1ef76bbaec642fb82230570

Latest Version: 2.3

Latest Build: 4

Latest Commit: 5512f136775fc22b1baf669b801dd6f69821ff20

I have tried following the steps in the below article and added the ‘context’ code add the bottom of the pre-filled vHost Conf file but when I try and visit the link, it just loads a 404 error page:

Here is my folder structure:

Any help and advice would be great as I have been trying to get this working for the past week!

Thanks in advance.
Jerry

Welcome @TheJezza Happy you are here

Please provide the error you are seeing ? There is no error posted in your topic

Hi Joseph, thanks for replying. Unfortunately I am not sure where to get the error that I am seeing. After adding the above vHost Conf at the bottom of the file, my preview link just shows a 404 error.

And nothing appears in my error logs:

The closest I have got is that my preview link just displays the code of my index.js file. Do I need to change any ports? Do I need to somehow link to one of my html.ejs files in my ‘views’ folder?

Could it also be because all my routes are stored in a separate folder named ‘routes’?

Look through this

thanks for your links Joseph! I will give them a read and see how I get on!

Hi Joseph, I have been trying everything from the links you provided but I am still having issues.

When I ssh in and run ‘serve -s build’, I get the following error:

file:///usr/lib/node_modules/serve/build/main.js:235
throw new Error(Failed to serve: ${error2.stack?.toString() ?? error2.message});
^

Error: Failed to serve: Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1740:16)
at listenInCluster (node:net:1788:12)
at Server.listen (node:net:1876:7)
at file:///usr/lib/node_modules/serve/build/main.js:247:14
at new Promise ()
at startServer (file:///usr/lib/node_modules/serve/build/main.js:245:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///usr/lib/node_modules/serve/build/main.js:517:40
at Server. (file:///usr/lib/node_modules/serve/build/main.js:235:11)
at Server.emit (node:events:513:28)
at emitErrorNT (node:net:1767:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

This is my ‘app.js’ file:

import express from ‘express’
const app = express();
import path from ‘path’
import { fileURLToPath } from ‘url’;
import { dirname } from ‘path’;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import bodyParser from ‘body-parser’
import cookieParser from ‘cookie-parser’
const port = 3000
import Sequelize from ‘sequelize’
import sequelize from “./config/database.js”;
import ejs from ‘ejs’
import ejsLint from ‘ejs-lint’
import expressLayouts from ‘express-ejs-layouts’
import mainRoutes from “./routes/mainroute.js”;

app.use(express.json())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, ‘/public’)));
app.use(cookieParser());

// Connect all the routes to my app
app.use(mainRoutes)

// EJS
app.set(‘views’, path.join(__dirname, ‘views’));
app.set(‘view engine’, ‘ejs’);

// Test connection to database
sequelize
.authenticate()
.then(() => {
console.log(‘Connected to database successfully.’);
})
.catch(err => {
console.error(‘Unable to connect to the database:’, err);
});

sequelize.sync()
.then(() => {
console.log(‘Tables created if it does not already exist’);
}).catch(err => {
console.log(‘Table has not been created’);
});

app.listen(port, () => {
console.log(Connected to server on port ${port})
})

And this is the vHost Conf file:

docRoot $VH_ROOT/public_html
vhDomain $VH_NAME
vhAliases www.$VH_NAME
adminEmails [email protected]
enableGzip 1
enableIpGeo 1

index {
useServer 0
indexFiles index.php, index.html
}

errorlog $VH_ROOT/logs/$VH_NAME.error_log {
useServer 0
logLevel WARN
rollingSize 10M
}

accesslog $VH_ROOT/logs/$VH_NAME.access_log {
useServer 0
logFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i””
logHeaders 5
rollingSize 10M
keepDays 10
compressArchive 1
}

scripthandler {
add lsapi:sicil5304 php
}

extprocessor sicil5304 {
type lsapi
address UDS://tmp/lshttpd/sicil5304.sock
maxConns 10
env LSAPI_CHILDREN=10
initTimeout 600
retryTimeout 0
persistConn 1
pcKeepAliveTimeout 1
respBuffer 0
autoStart 1
path /usr/local/lsws/lsphp81/bin/lsphp
extUser sicil5304
extGroup sicil5304
memSoftLimit 2047M
memHardLimit 2047M
procSoftLimit 400
procHardLimit 500
}

phpIniOverride {
php_admin_value open_basedir “/tmp:$VH_ROOT”
}

module cache {
storagePath /usr/local/lsws/cachedata/$VH_NAME
}

rewrite {
enable 1
autoLoadHtaccess 1
}

context / {
type appserver
location /home/mydomain.co.uk/public_html
binPath /usr/bin/node
appType node
maxConns 100

rewrite {

}
addDefaultCharset off
}

Even if I change the IndexFiles to ‘app.js’, my preview link just loads and loads until it times out.

Sorry for all the information but I know that there is something that I might be overlooking but can’t quite get to it!

Run command “node entrypointfile” manually and verify that is working fine.
You may use curl to check the text output of page.

If not perfect, try with 3001 port instead of 3000.

thanks for your comment.

I have run the command but I get the below error:

ssh@ssh:~$ node entrypoint
node:internal/modules/cjs/loader:1078
throw err;
^

Error: Cannot find module ‘/home/mydomain.co.uk/entrypoint’
at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
at Module._load (node:internal/modules/cjs/loader:920:27)
at Function.executeUserEntryPoint [as runMain]
(node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
code: ‘MODULE_NOT_FOUND’,
requireStack: [ ]
}

Node.js v18.16.0

Please note that I am now running Ubuntu 20.04 on my VPS.

I have checked my public_html folder and I can see the package.json file and within it, I can see the “main: app.js” so not sure what else can be happening:
image

I doesnt mean that what command you ran.
It mean node app.js
Please run it.

Thanks for your quick reply.

So I changed the port number in my “app.js” file to 3001.

When I ssh’d into my website/app, I changed directory to “public_html” and ran “node app.js” and got this:

user@ssh:~/public_html$ node app.js
Connected to server on port 3001
Unable to connect to the database: ConnectionRefusedError > [SequelizeConnectionRe fusedError]: connect ECONNREFUSED ::1:3306
at ConnectionManager.connect (/home/mydomain.co.uk/public_html/ node_modules/sequelize/lib/dialects/mysql/connection-manager.js:92:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5 )
at async ConnectionManager._connect (/home/mydomain.co.uk/publi c_html/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:220:24 )
at async /home/mydomain.co.uk/public_html/node_modules/sequeliz e/lib/dialects/abstract/connection-manager.js:174:32 {
parent: Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -111,
code: ‘ECONNREFUSED’,
syscall: ‘connect’,
address: ‘::1’,
port: 3306,
fatal: true
},
original: Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -111,
code: ‘ECONNREFUSED’,
syscall: ‘connect’,
address: ‘::1’,
port: 3306,
fatal: true
}
}
Table has not been created

When I try the preview link, I get the 404 error:

check database credentials and hostname,
In the error it’s showing hostname as ipv6, change it’s to localhost or 127.0.0.1.
Also don’t test url from browser, use curl http://locallhost:3001/page in second ssh terminal.

All these activities for testing the app as local host

Thanks again for your help so far.

I have checked my database connections and I think they are correct. But I can see that there are 3 areas where the credentials are held:

  1. In my ‘database.js’ config file.
  2. In phpMyAdmin.
  3. Directly on the Cyberpanel CP.

I have made sure that the credentials in the ‘database.js’ file and the Cyberpanel CP are correct (including set to ‘localhost’). But not entirely sure about phpMyAdmin.

I also ran ‘node app.js’ and visited “curl http://localhost:3001” and I do see my html.ejs code:

Now find the path of node by “whereis node” or “which node” commands.

it says:

node_modules: /usr/lib/node_modules

Share both commds screenshot

Apologies.

When I run “which node”, I get:

/usr/bin/node

When I run “whereis node”, it changes to “whereis node_modules” and I get:

Seems the above config is fine for you.
You may change port back to 3000.also try below command in public_html npm install.
All the best

I changed my ‘app.js’ back to port 3000 and run the ‘npm install’ command.

But when I run ‘npm.js’, I get the below error:

node:events:491
throw er; // Unhandled ‘error’ event
^

Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1740:16)
at listenInCluster (node:net:1788:12)
at Server.listen (node:net:1876:7)
at Function.listen (/home/mydomain.co.uk/public_html/node_modules/express/lib/application.js:635:24)
at file:///home/mydomain.co.uk/public_html/app.js:61:5
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Emitted ‘error’ event on Server instance at:
at emitErrorNT (node:net:1767:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: ‘EADDRINUSE’,
errno: -98,
syscall: ‘listen’,
address: ‘::’,
port: 3000
}

Check output in browser, there is no need to run any command.

it shows the 404 error: