how to setup nodejs

install nodejs for a single user


nodejs apps needs to be run under user not glabally or even root this will encounter errors

add the user in sudoers file

usermod -aG sudo username

passwd username

then

install nvm globally

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

then install node js using nvm

then add bash profile manually

vi ~/.bashrc 

export NVM_DIR="$HOME/.nvm"

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

add above to lines in bashrc

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

strap5887@vmi728455:~/public_html/strapi-backend$ sudo mkdir -p /home/strap5887




if you make anychnges to bashrc just do relogin changes does not work without relogin or exit





Install nodejs globally

# Install node
sudo apt install -y curl
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Set node global packages folder as /usr/local/lib/node_modules
NPM_PACKAGES="/usr/local/lib/node_modules"
sudo mkdir -p $NPM_PACKAGES
sudo chmod 777 $NPM_PACKAGES
# Update the path and manpath to read from npm packages
echo "export PATH="\""\$PATH:$NPM_PACKAGES/bin"\""
export MANPATH="\""\${MANPATH-\$(manpath)}:$NPM_PACKAGES/share/man"\""
npm config set prefix $NPM_PACKAGES" | sudo tee '/etc/profile.d/node-path.sh'
source /etc/profile.d/node-path.sh

to check all node version

nvm ls-remote 

nvm install v14.17.3

How can the default node version be set using NVM?



nvm alias default 6.11.5 if you want it pegged to that specific version.

You can also do nvm alias default 16.

Either way, you'll want to upgrade to the latest version of nvm (v0.33.11 as of this writing)

$ nvm alias default 16.14.2
# nvm set default node.js version 16.14.2

$ node -v
# v16.14.2

root@server:/home/cloudvariation.in/public_html/blogar# nvm alias default 16.18.0

default -> 16.18.0 (-> v16.18.0)

nvm use default

root@server:/home/cloudvariation.in/public_html/blogar# nvm alias default node
default -> node (-> v16.18.0)
root@server:/home/cloudvariation.in/public_html/blogar# nvm use default
Now using node v16.18.0 (npm v8.19.2)
root@server:/home/cloudvariation.in/public_html/blogar# node -v
v16.18.0

This will set the default to be the most current version of node

nvm alias default node

and then you'll need to run

nvm use default

or exit and open a new tab


for a specific version


nvm use v18.12.1 


 there is file in src > server.js


server.js

const http = require("http");

const app = require("./app");

const port = process.env.PORT || 8088; 

  // const port = process.env.PORT || 8088;

  // const port = process.env.PORT || 8061;

const server = http.createServer(app);

server.listen(port);

console.log("listening at port ", port);


see the port 8088 is defined

and running  by netstat -plunt | grep 8088

tcp6       0      0 :::8088                 :::*                    LISTEN      32256/node /home/ap 


setting up Nginx as reverse proxy for nodejs


set this for that domain configuration

server {
    listen      192.227.69.95:80;
    server_name api.globaltrendz.info www.api.globaltrendz.info;
    root        /home/api.globaltrendz/web/api.globaltrendz.info/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/api.globaltrendz.info.log combined;
    access_log  /var/log/nginx/domains/api.globaltrendz.info.bytes bytes;
    error_log   /var/log/nginx/domains/api.globaltrendz.info.error.log error;

    location / {

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }
location / {
                proxy_pass http://127.0.0.1:8088/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                }

                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                root   html;

                }


 #       location ~ [^/]\.php(/|$) {
 #           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 #           if (!-f $document_root$fastcgi_script_name) {
 #               return  404;
 #           }
#
 #           fastcgi_pass    127.0.0.1:9002;
  #          fastcgi_index   index.php;
   #         include         /etc/nginx/fastcgi_params;
   #     }
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;
}


How to Setup Apache As Frontend Proxy for Node.js






no need to make any changes in config if you need reactjs in frontend
sudo a2enmod proxy proxy_http 

and add the following content.


For API we created another subdomain and install ssl and then proxy pass using above code

https://api.krishit.com/admin/getOverAllEmployeeRecords

through postman he checked


To ru node js app type node server.js from src location

Running nodejs forever 

pm2 start process.json

or

pm2 start processes.json

cat processes.json

{
    "apps" : [{
    "name"        : "Cloud-dhobi-API",
    "script"      : "./src/server.js",
    "watch"       : "../",
    "log_date_format"  : "YYYY-MM-DD HH:mm Z"
    }]

}

To list all pm2 process


Install pm2

npm install pm2 -g

With debian, use the install script:

apt update && apt install sudo curl && curl -sL https://raw.githubusercontent.com/Unitech/pm2/master/packager/setup.deb.sh | sudo -E bash -

node app.js

root@server2:/home/hrms.krishit/web/hrms.krishit.com/public_html# node app.js
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'express'
Require stack:


root@server2:/home/hrms.krishit/web/hrms.krishit.com/public_html# npm install express --save

npm ERR! code 126
npm ERR! path /home/hrms.krishit/web/hrms.krishit.com/public_html/node_modules/bcrypt
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
npm ERR! /usr/bin/env: ‘node’: Permission denied

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-07-22T15_46_02_128Z-debug.log
root@server2:/home/hrms.krishit/web/hrms.krishit.com/public_html# vi /root/.npm/_logs/2022-07-22T15_46_02_128Z-debug.log


error /usr/bin/env: ‘node’: Permission denied

npm install express


pm2 list


sudo apt-get install node-express

npm install express

npm install jade

npm install socket.io



Error: Cannot find module 'Cors'


cors c was in capital that why gettting error



Error: getaddrinfo ENOTFOUND api.krishit.com


make local entry in /etc/hosts


192.227.112.108 api.krishit.com vms.krishit.com



root@server:~/.nvm/versions/node# ls


v14.0.0  v14.17.3  v16.17.1  v16.18.0


root@server:~/.nvm/versions/node# pwd

/root/.nvm/versions/node



PM2 using wrong node.js version



autostart nodejs on boot

root@vmi1093744:~# cat /lib/systemd/system/app.service

[Unit] Description=app After=multi-user.target [Service] Type=idle ExecStart=/root/.nvm/versions/node/v16.13.2/bin/node /home/alphawa/public_html/api.alpha-media.marketing/app.js #WorkingDirectory= User=root [Install] WantedBy=multi-user.target

sudo chmod 644 /lib/systemd/system/app.service

sudo systemctl daemon-reload
sudo systemctl enable app.service
sudo systemctl start app.service
sudo systemctl status app.service

to check all node version

nvm ls-remote 

nvm install v14.17.3

to check node js installed verions

nvm list

remove nvm 

nvm uninstall 18.10.0

For later fellow googlers:

pm2 update



This helped me greatly. Just in case future readers, you can check which node version pm2 is using with this command: pm2 show <appId/ appName>


pm2 monit  is another way to check the node version 



Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation