In this guide, you will learn how to install and configure NGINX on a Debian 10 Linode to serve Node.js applications. NGINX will be used to create a reverse proxy that points to a running Node.js server, as well as to handle requests to static files, such as index.html. Additionally, a test JavaScript file will be created to test the running Node.js server. Node.js is an open-source JavaScript runtime environment that is often used to create and serve web applications, providing dynamic and responsive content. Consequently, NGINX and Node.js, when used together, make for a powerful combination.
Unlock the Keys to Success: Get Ready for a Life-Changing Adventure with Before You Begin!
- Secure Your Website with a Custom Domain Name – Easily Set Up with Linode’s DNS Manager!
- Set Up Your Linode: Get Started Now with Step-by-Step Guides on Setting Up and Securing a Compute Instance!
Unlock the Power of NGINX – Learn How to Install and Configure It in No Time!
- Install NGINX and the screen utility. You will use screen in the Create Your the Node.js Web Server File.
sudo apt-get install nginx screen
- Start NGINX Now and Ensure Automated Reboots for Reliable Performance!
sudo systemctl start nginx
sudo systemctl enable nginx
- Create Your Own NGINX Site Configuration File in Minutes – Step-by-Step Guide!
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 ~* \.(js)$ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
}
- Create an Instant Symlink from NGINX Configuration File to Sites-enabled Directory!
- Get Rid of Unwanted Links: How to Remove a Symlink from NGINX’s Default Site Configuration File!
sudo rm /etc/nginx/sites-enabled/default
- Verify Your Site’s Configuration File for Flawless Syntax – Ensure No Errors!
sudo nginx -t
Be Ready to Wow: Unlock the Power of Creative Writing in Just 5 Steps!
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- Reboot Your Site Now – Learn How To Load Your NGINX Configuration!
sudo systemctl restart nginx
Transform Your Site’s Visibility: Learn How to Create Your Site’s Index File Today!
- Unlock Your Site’s Potential – Create a Root Directory and Index.html File Now!
sudo mkdir -p /var/www/example.com
- Learn How to Create Your Site’s Index File in Minutes with this Easy Tutorial!
File: /var/www/example.com/index.html
<!DOCTYPE html>
<html>
<body>
<p><strong>If you have not finished the <a href="https://www.linode.com/docs/guides/how-to-install-nodejs-and-nginx-on-debian-10/">guide</a>, the button below will not work.</strong></p>
<p>The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.</p>
<a href="test.js">
<button type="button">Go to test.js</button>
</a>
</body>
</html>
Bring Your Web Dreams to Life with Node.js – Create Your Own Web Server Now!
Unlock the Power of Node.js: Easy Installation Guaranteed!
- Effortlessly Manage Multiple Node.js Versions with Node Version Manager (NVM)!
sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash
- Unlock the Power of NVM – Instantly 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: check to make sure that you have the most up-to-date version of NVM installed, and then print its current version to confirm your access.
nvm --version
See the Incredible Results: Get the Same Output with This Proven Method!
0.35.3
- Elevate Your Development: Install Node.js and Unlock its Benefits!
nvm install 12.16.2
- Never Run Out of Node.js Versions Again: Use NVM to Manage Your Preferred Version!
nvm use 12.16.2
Unlock Your Potential and Achieve Success with These Proven Strategies!
Now using node v12.16.2 (npm v6.14.4)
Create a Test JavaScript File – Unlock Your Web Development Potential Today!
In the Install and Configure NGINX section, you successfully configured NGINX to listen on port 80 to serve its static content, as well as setting up a reverse proxy to your Linode’s localhost:3000 when a request for the /test.js file is made. Accordingly, in this section you will create the test.js file, in order to then test your Node.js web server which you will create in the next section.
- Create a Test.js File in Your Site’s Root Directory For Maximum Efficiency!
File: /var/www/example.com/test.js
<!DOCTYPE html>
<html>
<body>
<h2>
Your Node.JS server is working.
</h2>
<p>
The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.</p>
<button type="button" onclick="document.getElementById('sample').innerHTML = Date()"> Display the date and time.</button>
<p id="sample"></p>
</body>
</html>
Unlock the Power of Node.js: Create a Fully Functional Web Server in Minutes!
In this section, you will create a server.js file that utilizes Node.js modules to construct a simple web server that can both handle and respond to client requests. Moreover, it will allow for the development of a more complex web server in the future.
- Create a Stunning Server File in Your Site’s Root Directory with this Simple Trick!
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
- Start Fresh – Unleash the Power of a New Screen Session!
screen
Discover the Power of Your Imagination: Unlock the Possibilities of Your Mind!
- Navigate to your root directory where your
test.js
file is located.
cd /var/www/example.com
- Keep Your Node.js Web Server Running in the Background – Append & to the End of Your Commands!
node server.js &
Master the Basics of Command Prompts: Learn How to Return to Your Command Prompt with CTRL+C!
- Effortlessly Exit Your Screen Session with Just Two Keystrokes!
- Experience the Power of Your Site: Open a Browser and Navigate to the Domain or IP Address Now!
- Unlock the Power of Your Node.js Web Server: Click on the ‘Go to test.js’ Button Now!
- See the Current Date and Time Instantly By Clicking the ‘Display the Date and Time’ Button!
Having completed the basic configurations to proxy requests to the Node.js server, the next step is to look into further NGINX configurations for serving both static content and dynamic content from a reverse proxy. Consequently, further configurations should be considered in order to optimally handle this task.
Discover How to Create Powerful Web Apps with JavaScript: Explore Express.js, Ember.js, and Vue.js Now!
Discover the Power of Knowledge: Uncovering More Information to Drive Success.
While the following resources may be useful for additional information on this topic, it is important to note that we cannot vouch for the accuracy or timeliness of externally hosted materials. Thus, it is wise to exercise caution when consulting these resources.