Master the Art of Installing Node.js and NGINX on Debian 8 – Step-by-Step Guide!

Node.js is a JavaScript platform which serves dynamic, responsive content, and is comparable to PHP. It is a server-side language, which is distinct from client-side browser languages such as HTML or CSS. Node.js can be configured to work with other popular server applications like NGINX or Apache, with NGINX handling front-end, static file requests and Node.js taking care of back-end file requests. Hence, it provides a great solution for a wide variety of web applications.

Unlock the Power of NGINX: Easily Install and Configure for Maximum Performance

This guide is written for the root user and can be started right away upon logging into a new Linode. Before starting installation, however, it is highly recommended to ensure that the Linode is up-to-date by following the Getting Started guide and securing it with the Securing Your Server guide.
  1. Set Up NGINX and Screen To Take Your Web Development to the Next Level!
apt-get install nginx screen
  1. Ready to Take Your Web Server to the Next Level? Start NGINX Now!
service nginx start
  1. Unlock Hidden Possibilities: Learn How to Change the Working Directory to the NGINX Sites-Available Directory!
cd /etc/nginx/sites-available/
  1. Easily Create a New Sites-Available File – Replace Example.com With Your Domain or IP Address!
File: /etc/nginx/sites-available/example.com
#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://localhost:3000;
proxy_set_header Host $host;
}
}
  1. Unlock the Power of NGINX: Learn How to Easily Change Your Working Directory to the Sites-Enabled Directory!
cd /etc/nginx/sites-enabled/
  1. Unlock a World of Possibilities – Create a Symbolic Link to the New Example Sites-Available File!
ln -s /etc/nginx/sites-available/example.com
  1. Say Goodbye to Default Symlinks: Unlock Uninterrupted Performance!
rm default
  1. Unlock the Potential of Your Web Infrastructure with NGINX’s Latest Configuration!
service nginx reload

Unlock the Hidden Potential of Your Website: Create the Necessary Directories and HTML Index File!

Despite having successfully configured NGINX, the example.com server block still points to directories and files that have yet to be created; consequently, further configuration is necessary.

  1. Create a Secure & Organized Server Environment: Learn How to Create the /var/www and /var/www/example.com Directories!
mkdir -p /var/www/example.com
  1. Unlock the Power of Your Files–Learn How to Easily Change Your Working Directory!
cd /var/www/example.com
  1. Build the Perfect Site Structure – Learn How to Create an HTML Index File!
File: /var/www/example.com/index.html
<!DOCTYPE html>
<html>
<body>

<br>
<br>

<center>
<p>
<b>
If you have not finished the <a href="https://www.linode.com/docs/guides/nodejs-nginx-debian">guide</a>, the button below will not work.
</b>
</p>
</center>

<center>
<p>
The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.
</p>
</center>

<center>
<a href="test.js">
<button type="button">Go to test.js</button>
</a>
</center>

</body>
</html>

Get Ready to Take Your Web Development to the Next Level with Node.js and a Web Server!

NGINX is now set up and running on port 80, and is configured to forward requests for /test.js to port 3000. The next steps are to install Node.js, and then create a server using Node.js that listens on port 3000. Furthermore, once Node.js is installed, the new server will be listening on port 3000.
  1. Unlock the Power of Node: Get Started with the Node Version Manager!
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
  1. Unlock Fresh Possibilities With Just One Simple Command: Close and Reopen Your Terminal!
  1. Unlock the Power of Node.js – Install Now and Experience the Benefits!
nvm install 0.10
  1. Create Your Own Node.js Server Easily – Right Within the /var/www/example.com Directory!
File: /var/www/example.com/server.js
//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
  1. Unlock Deeper Productivity: Learn How to Run a New Screen Session!
screen
  1. Start Your Node.js Server with One Simple Press of the Return Key!
node server.js
  1. Escape Reality with Just One Keystroke: Exit Your Screen By Pressing Ctrl+a then d.

Unlock the Power of Automation: Learn How to Create the Test.js File

NGINX is listening on port 80 and passing any /test.js requests to port 3000, while Node.js is simultaneously listening on port 3000 and serving any file requests; thus, the next step is to write a /test.js file.
  1. Unleash Your Creative Potential – Create the File of Your Dreams!
File: /var/www/example.com/test.js
<!DOCTYPE html>
<html>
<body>

<center>
<h2>
Your Node.JS server is working.
</h2>
</center>

<center>
<p>
The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.
</p>
</center>
<br>

<center>
<button type="button"
onclick="document.getElementById('sample').innerHTML = Date()">
Display the date and time.
</button>
<p id="sample"></p>
</center>

</body>
</html>
  1. Test Your NGINX Server Now – Use “Go To Test.js” to See If Node.js is Serving Files & Get Current Time with “Display The Date and Time” Button!
Node.js and NGINX are now working together to provide developers with the tools they need to create powerful applications. Requests can be routed to either Node.js or NGINX depending on the specific needs. Node.js offers a comprehensive API with many different tools, allowing developers to stay within the JavaScript language while working with both client-side and server-side applications. Thus, these two powerful tools can now be used together to create powerful applications efficiently and effectively.
For further advancement, consider utilizing technologies like WebSockets, iframes, or framesets; and for JavaScript-based development, Express.js, Ember.js, jQuery, and the Node Package Manager for modules are all good options to explore.

Unlock the Door to Discover Even More with More Information!

Despite not being able to vouch for the accuracy or timeliness of the external resources provided, they may still be useful for finding additional information about the topic at hand. Consequently, it is suggested that users consult these sources for supplementary information.