Metatrader4

Trade instantly with MetaTrader 4, designed for fast and efficient Forex trading.

The script creates a nodejs user, installs Node Version Manager (NVM), Node.js (version 22), PM2 (a process manager), and Nginx as a reverse proxy, and configures them for deploying Node.js applications.

  1. Creates a nodejs user with a home directory (/home/nodejs) and Bash shell.
  2. Adds nodejs to the sudo group.
  3. Sets the user password to password.
  4. Runs as nodejs user via sudo -H -u nodejs to ensure correct home directory (/home/nodejs).
  5. Downloads and installs NVM (v0.40.3) from https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh.
  6. Configures .bashrc to source NVM automatically:
    1. Adds export NVM_DIR=”$HOME/.nvm”.
    2. Adds NVM script and bash completion.
  7. Sources .bashrc to make NVM available in the script.
  8. Installs Node.js 22 via NVM as nodejs user.
  9. Sets it as the default version: nvm alias default 22.
  10. Updates npm globally: npm install -g npm.
  11. Installs PM2 globally as nodejs user: npm install -g pm2.
  12. Configures PM2 to start on boot by generating and executing a systemd startup command.
  13. Installs Nginx: apt install -y nginx.
  14. Configures a default Nginx site in /etc/nginx/sites-available/default:
    1. Listens on port 80.
    2. Proxies requests to http://localhost:3000 (assumed Node.js app port).
    3. Includes headers for WebSocket support and client IP forwarding.
  15. Restarts and enables Nginx: systemctl restart nginx, systemctl enable nginx.

NodeJS app setup:

  1. Login via ssh to your server.
  2. Use the following commands in order:
    1. su – nodejs
    2. mkdir /home/nodejs/your-app
    3. git clone your_repo.git /home/nodejs/your-app
    4. cd /home/nodejs/your-app
    5. npm install –omit=dev
    6. pm2 start app.js –name your-app-name
    7. pm2 save
  3. Adjust your app’s port to 3000 or update the NGINX config accordingly (edit /etc/nginx/sites-available/default, test with nginx -t, and restart systemctl restart nginx).
  4. Your app is accessible at http://<server-ip>
  5. Notes:
    1. The nodejs user has sudo access.
    2. Change nodejs user password: passwd nodejs.
    3. Enable HTTPS with Certbot: apt install certbot python3-certbot-nginx, then certbot –nginx.

OS: Ubuntu 24.04

Variables:

  • NVM_VERSION: Node.js version (default: 22).
  • NODEJS_USER_PASSWORD: Password for nodejs user.

Important Files and Directories:

  • /home/nodejs/.nvm/: NVM and Node.js installations.
  • /home/nodejs/.bashrc: Configures NVM sourcing.
  • /etc/nginx/sites-available/default: Nginx configuration for proxying to Node.js.
  • /usr/lib/node_modules/pm2/: PM2 installation.
  • /var/www/html: Default web root.
  • Logs:
    • Nginx: /var/log/nginx/{access.log,error.log}.
    • PM2: pm2 logs or ~/.pm2/logs/ (as nodejs user).
    • Node.js app: Managed by PM2 or app-specific logs.

Future Enhancements:

  • Add HTTPS setup with Certbot.
  • Include app deployment example in the script.
  • MongoDB integration.

Application Details