DEV Community

Cover image for How to set up NGINX with Node.js on Debian using HostRooster
Dean Jones for HostRoosterplc

Posted on • Updated on


How to set up NGINX with Node.js on Debian using HostRooster

This post explains how to set up the Debian operating system-powered virtual server where node.js and the NGINX web server will be installed.

*What is Node.js and NGINX?

A JavaScript framework called Node.js is capable of serving responsive and dynamic content. Like HTML or CSS, JavaScript is often a built-in browser language. Additionally, Node.js is a JavaScript server-side platform similar to PHP. Node.js frequently integrates with other well-liked server programs like NGINX or Apache. In this article, we'll look at setting up NGINX to handle requests from the outside world and Node.js to handle requests from the inside.

*Initial requirements

The majority of the commands in this manual call for superuser rights. Install the sudo command, enable superuser mode, and add your user to the sudo group if you encounter the following bash error while using the sudo command: sudo: command not found:

su -
apt-get install sudo -y
usermod -aG sudo yourusername

Update local repositories and packages:

sudo apt-get update && sudo apt-get upgrade

Installing and configuring NGINX

Install NGINX as well as the screen module, which will be used later:

apt-get install nginx screen

Start nginx:

service nginx start

Use the cd command to change to the following directory:

cd /etc/nginx/sites-available/

Create a new file, replacing with your domain name or IP address:


Paste the following lines into the generated file, replacing with your domain name or IP address:

`#Names a server and declares the listening port
server {
listen 80;

Configures the publicly served root directory

Configures the index file to be served

root /var/www/;
index index.html index.htm;

These lines create a bypass for certain pathnames is now routed to port 3000

instead of port 80

location /test.js {
proxy_set_header Host $host;

Save changes and change directory:
cd /etc/nginx/sites-enabled/

Create a symbolic link to the created file:
ln -s /etc/nginx/sites-available/

Remove the default symbolic link:
rm default

Restart NGINX to apply the new configuration:
service nginx reload

Creating directories and HTML files

Create the following directory hierarchy, replacing
mkdir -p /var/www/

Change to the created directory:
cd /var/www/

Create an index file:
touch index.html

Insert the following lines:
`<!DOCTYPE html>

` If you have not finished the guide, the button below will not `work.

` The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server. `

Go to test.js

Installing Node.js`

At this point, NGINX is listening on port 80 and serving the content. It is also configured to forward requests to the /test.js application on port 3000.

Install the Node Version Manager:
wget -qO- | bash

Close and reopen your terminal.

Install Node.js with the following command:
nvm install 0.10

Create the following file:
touch /var/www/

Paste the following content into it:
`// for API docs
//Node.js web server
var http = require("http"), //Import Node.js modules
url = require("url"),
path = require("path"),
fs = require("fs");

http.createServer(function(request, response) { //Create server
var name = url.parse(request.url).pathname; //Parse URL
var filename = path.join(process.cwd(), name); //Create filename
fs.readFile(filename, "binary", function(err, file) { //Read file
if(err) { //Tracking Errors
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "n");
response.writeHead(200); //Header request response
response.write(file, "binary"); //Sends body response
response.end(); //Signals to server that
}); //header and body sent
}).listen(3000); //Listening port
console.log("Server is listening on port 3000.") //Terminal output

Start a new screen session:

Next, press Enter and start the Node.js server:
node server.js`

Press the key combination Ctrl+A, then D .

Create a test application

Create a file with test data:
`touch /var/www/

And paste the following data into it:
<!DOCTYPE html>

Your Node.JS server is working.

` The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side. `

onclick="document.getElementById('sample').innerHTML = Date()">
Display the date and time.

Open port 80 for http connection:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Navigate in your browser to your domain or ip address. The next page will be displayed. Click the Go to the test.js button :

If everything is configured correctly, using the Display the date and time button on a new page, you can display the current time and date:

Node.js and [NGINX]

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.