Friday, December 17, 2021

How To Set Up a Node.js Application for Production with pm2 Nginx on Ubuntu 18.04

 Node.js is an open source Javascript runtime environment for easily building server-side and networking applications. The platform runs on Linux, OS X, FreeBSD, and Windows, and its applications are written in JavaScript. Node.js applications can be run at the command line but i will teach you how to run them as a service, so they will automatically restart on reboot or failure, so you can use them in a production environment.

Offcial website:- https://nodejs.org/en|| Github:- https://github.com/nodejs
Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. ||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Our Server Specification:-
Os : Ubuntu 18.04 LTS Bionic Beaver ||     Hostname:- www.example.com           || IP Address of Server:-192.168.232.177
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lsb_release -a ; getconf LONG_BIT  
apt install -y build-essential curl gdebi aptitude git cmake net-tools software-properties-common leafpad nano synaptic vim wget libtool-bin autoconf automake python3-setuptools sqlite3 libtool-bin perl dpkg-dev python3-pip python3-dev libssl-dev checkinstall imagemagick openssl python libkrb5-dev nginx

curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt install nodejs -y

gedit hello.js &>/dev/null
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

npm install pm2@latest -g --no-optional --no-shrinkwrap --no-package-lock
pm2 start hello.js
pm2 startup
systemctl enable pm2-root ; systemctl start pm2-root ; systemctl status pm2-root
pm2 save

nano  /etc/nginx/conf.d/node.conf
server {
        listen 80;
        server_name www.testdomain.com;

        location / {
                proxy_pass http://127.0.0.1:3000;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }
}
nginx -t ; sudo netstat -tlpn | grep 3000 ; hostname -I ; systemctl restart nginx
sudo -- sh -c "echo 192.168.232.177 www.testdomain.com  >> /etc/hosts"
http://www.testdomain.com
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment