DEV Community

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

Posted on • Edited 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 example.com with your domain name or IP address:

touch example.com

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

`#Names a server and declares the listening port
server {
listen 80;
server_name example.com www.example.com;

Configures the publicly served root directory

Configures the index file to be served

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

These lines create a bypass for certain pathnames

www.example.com/test.js is now routed to port 3000

instead of port 80

location /test.js {
proxy_pass http://example.com:3000;
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/example.com

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 example.com:
mkdir -p /var/www/example.com

Change to the created directory:
cd /var/www/example.com

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- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Close and reopen your terminal.

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

Create the following file:
touch /var/www/example.com/server.js

Paste the following content into it:
`//nodejs.org/api 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.end();
return;
}
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:
`screen

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/example.com/test.js

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. `

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)