Online deployment

Install nodejs npm

$ sudo apt update
$ sudo apt install nodejs npm
  • Note: Your nodejs version should be greater than v18, you may need nvm to manage node versions

Installing nginx in Ubuntu system

$ sudo apt update
$ sudo apt install nginx
  • Use systemctl status nginx to get the nginx status
  • Use sudo systemctl restart nginx to restart nginx
  • Use a browser to access the server IP address. If the nginx page is returned, it means that the installation is successful
  • Run whereis nginx to get the installation location
root@ndsk:~# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
root@ndsk:~#
  • Modify the /etc/nginx/nginx.conf file and add include /root/project/*/*.conf; to point to the project directory
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
include /root/project/*/*.conf;
  • Execute the following command in the terminal to create the /root/project/my-app directory and enter
$ mkdir project && cd project
$ mkdir my-app && cd my-app 
  • Add nginx.conf configuration file, here to view detailed configuration of nginx
  • Note: You need to resolve the domain name to the server first
server {
    listen 80;
    listen [::]:80;
    server_name ndsk.dev www.ndsk.dev nodestack.dev www.nodestack.dev;
    #listen 443 ssl;
    #ssl_certificate /root/project/ndsk/cert/ndsk.dev.cert;
    #ssl_certificate_key /root/project/ndsk/cert/private.key;
    #ssl_session_timeout 5m;
    #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #ssl_prefer_server_ciphers on;
    error_log off;
    access_log off;
    location / {
        proxy_pass http://142.93.70.60:3000;  
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  • You should make sure pm2 is installed, execute npm install pm2 -g in the terminal
  • Configure pm2 to start in package.json
"scripts": {
    "dev": "ndsk",
    "build": "ndsk build",
    "start": "ndsk start --pm2"
},
  • Upload the following files to the my-app directory
my-nodestack-app
├── build
├── static
├── nginx.conf
├── ndsk.config.js
└── package.json
  • Install dependencies, start the project, and check whether it starts successfully
$ npm i 
$ npm run start
$ pm2 list
  • If the project fails to start, you should execute pm2 logs to view the error. If the following error is displayed, execute pm2 update to fix it.
Script /root/project/ndsk/node_modules/@ndsk/ndsk/index.js had too many unstable restarts (16). Stopped. "errored"
  • Check nginx configuration, restart nginx
$ nginx -t
$ sudo systemctl restart nginx
  • Access your_domain

  • If you cannot access it through your domain name, you may need to check whether the firewall configuration allows the corresponding port

$ sudo ufw status
$ sudo ufw enable
$ sudo ufw allow ssh
$ sudo ufw allow 80
$ sudo ufw allow 443
$ sudo ufw allow 3000

Next step

Deploy to docker