How to set up nginx on windows, macos, and linux to host a node.Js app

by

PREREQUISITES:

  • A computer running Windows, macOS, or Linux.
  • Basic knowledge of the command line.
  • A Node.js application to host.

STEP 1: INSTALLING NGINX

Windows:

  1. Visit the official NGINX website and download the Windows version.
  2. Extract the downloaded file to a directory, e.g., C:\nginx.
  3. Navigate to the directory and run

nginx.exe . macOS (using Homebrew):

  1. Open Terminal.
  2. Install Homebrew if you haven’t: /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  3. Install NGINX: brew install nginx
  4. Start NGINX: sudo nginx

Linux (Debian/Ubuntu):

  1. Open Terminal.
  2. Update package lists: sudo apt update
  3. Install NGINX: sudo apt install nginx
  4. Start NGINX: sudo systemctl start nginx

STEP 2: CONFIGURING NGINX FOR NODE.JS APP

  1. Navigate to the NGINX configuration directory. Typically, it’s conf for Windows, /usr/local/etc/nginx for macOS, and /etc/nginx for Linux.
  2. Open nginx.conf in a text editor.
  3. Add a new server block to reverse proxy requests to your Node.js app:
SET UP NGINX ON WINDOWS, MACOS,
AND LINUX TO HOST A NODE.JS APP

Replace your_domain_or_IP with your domain name or IP address and YOUR_NODE_APP_PORT with the port your Node.js app runs on.

  1. Save and close the file.

STEP 3: MAKING YOUR LOCAL PC ACCESSIBLE FROM THE INTERNET

If you’re running your Node.js app on a local PC and want it to be accessible from the internet, you’ll need to:

  1. Configure your router to forward incoming requests to your PC (port forwarding).
  2. Use services like ngrok or localtunnel to expose your local server to the internet.

Remember, exposing your local environment to the internet comes with security risks. Always ensure you have proper security measures in place.

STEP 4: ESSENTIAL NGINX COMMAND-LINE COMMANDS

  • Start NGINX: nginx (or ./nginx.exe on Windows)
  • Stop NGINX: nginx -s stop
  • Reload NGINX Configuration: nginx -s reload
  • Test NGINX Configuration: nginx -t

STEP 5: RESTARTING YOUR PC

After installing NGINX, especially on Windows, it’s a good practice to restart your PC. This ensures that all configurations are correctly initialized and that NGINX runs smoothly.

CONCLUSION

Setting up NGINX to host a Node.js application is a straightforward process, whether you’re on Windows, macOS, or Linux. With the right configurations and security precautions, you can have a robust and efficient web server up and running in no time.

all_in_one_marketing_tool