Development

how-to-install-nodejs-and-nginx-on-debian-10

Captain Salem 4 min read

Node.js is an open-source JavaScript runtime environment that can serve dynamic and responsive content and is often used to create and serve web applications. When serving Node.js applications, NGINX is commonly used to create a reverse proxy that points at a running Node.js server. In this guide, you will install and configure NGINX on Debian 10 Buster. NGINX will handle requests to static files, like index.html and also, create a reverse proxy to a Node.js server. You will then create a test JavaScript file in order to test your running Node.js server.

Install and Configure NGINX

Restart NGINX to load your site’s configuration.

sudo systemctl restart nginx

Verify that there are no syntax errors in your site’s configuration file.

 sudo nginx -t

Your output should resemble the following:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Remove the symbolic link from NGINX’s default site configuration file.

sudo rm /etc/nginx/sites-enabled/default

Create a symbolic link from your NGINX configuration file in the sites-available directory to the sites-enabled directory.

 sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Using your preferred text editor, create a new NGINX site configuration file located in the /etc/nginx/sites-available/ directory. Replace the example file name and any instances of example.com with your own 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 ~* \.(js)$ {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
}
}

Start NGINX and enable it to start automatically on reboots.

sudo systemctl start nginx
sudo systemctl enable nginx

Install NGINX and the screen utility.

sudo apt-get install nginx screen -y

Create Your Site’s Index File

Ensure you replace example.com with your own site’s name or IP address in all commands and examples in this section.

  1. Using the text editor of your choice, create your site’s index file in the root directory.

Create your site’s root directory, which will store the index.html file you will create in the next step. The directory’s location should be the one you designated in your site’s NGINX configuration file for the root configuration.

 sudo mkdir -p /var/www/example.com

Create Your Node.js Web Server

Install Node.js

  1. Install Node.js. As of writing this guide, the latest LTS version of Node.js is v12.18.3. Update this command with the version of Node.js you would like to install.nvm install 12.18.3

Use NVM to run your preferred version of Node.js.

 nvm use 12.18.3

You should get an output as

Now using node v12.16.2 (npm v6.14.4)

Load NVM in your current terminal session.

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Verify that you have access to NVM by printing its current version.

 nvm --version

You should see a similar output:

0.35.3

Install the Node Version Manager (NVM) for Node.js. This program helps you manage different Node.js versions on a single system.

sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Create a Test JavaScript File

In the Install and Configure NGINX section you configured NGINX to listen on port 80 to serve its static content. You also configured a reverse proxy to your systems localhost:3000 when a request for the /index.js file is made. In this section you will create the index.js file to be able to test your Node.js web server.

Ensure you replace example.com with your own site’s name or IP address in all commands and examples in this section.

  1. Create the index.js file in your site’s root directory.

Your Node.JS server is working.

You are now using Javascript on both the client-side and the server-side.

Display the date and time.

Create the Node.js Web Server File

In this section, you will create a file named server.js that will use Node.js modules to help you write a simple web server that can handle client requests and return responses to them.

  1. In your site’s root directory, create the server.js file with the following content.```javascript
  2. //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 + “”); 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 ```
  3. Exit the screen session by pressing Ctrl+A then d.
  4. Open a browser and navigate to your site’s domain or IP address. You should see your site’s index.html page load.
  5. Click on the page to load the index.js page whose content will be served dynamically with your Node.js web server.
  6. Click on the test page’s Display the date and time button to dynamically display the current date and time.

Run your Node.js web server. Appending & to the end of a command will keep the web server’s process running in the background.

 node server.js &

You should see your terminal return a process ID after issuing the previous command. Return to your command prompt by entering CTRL+C.

Navigate to your root directory where your index.js file is located.

 cd /var/www/example.com

Run a new session using the screen command.

 screen

Press return when prompted.

You have now completed the basic configurations to proxy requests to the Node.js server you wrote.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.