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:
- Visit the official NGINX website and download the Windows version.
- Extract the downloaded file to a directory, e.g., C:\nginx.
- Navigate to the directory and run
nginx.exe . macOS (using Homebrew):
- Open Terminal.
- Install Homebrew if you haven’t: /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
- Install NGINX: brew install nginx
- Start NGINX: sudo nginx
Linux (Debian/Ubuntu):
- Open Terminal.
- Update package lists: sudo apt update
- Install NGINX: sudo apt install nginx
- Start NGINX: sudo systemctl start nginx
STEP 2: CONFIGURING NGINX FOR NODE.JS APP
- Navigate to the NGINX configuration directory. Typically, it’s conf for Windows, /usr/local/etc/nginx for macOS, and /etc/nginx for Linux.
- Open nginx.conf in a text editor.
- Add a new server block to reverse proxy requests to your 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.
- 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:
- Configure your router to forward incoming requests to your PC (port forwarding).
- 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.
You must be logged in to post a comment.