Unlock the Power of Node.js and NGINX on CentOS 8 with These Easy Steps!

In this guide, we will demonstrate how to install and configure NGINX on a CentOS 8 Linode, to serve Node.js applications. NGINX will be used to create a reverse proxy that points to the Node.js server, and also to handle requests for static files such as index.html. Additionally, we will create a test JavaScript file to test the running Node.js server. Node.js is an open-source JavaScript runtime environment used to create and serve dynamic and responsive content, making it an ideal choice for web applications. By following this guide, users can easily set up a powerful, reliable Node.js server with NGINX.

Unlock the Secrets of Success: Get Ready to Start Your Journey with this Pre-Launch Checklist!

  1. Secure Your Site with a Custom Domain Name – Purchase a Domain Name & Create a Domain Record on Linode’s DNS Manager.
  1. A Step-by-Step Guide to Easily Set up and Secure Your Linode Compute Instance!
  1. Unlock the Power of SELinux with Python Utilities: Manage Your System in an Easy and Fine-Grained Way!
 sudo yum install -y policycoreutils-python-utils

Unlock the Power of NGINX: Master Installation and Configuration Easily!

Generally, NGINX site-specific configuration files are kept in both the /etc/nginx/sites-available and /etc/nginx/sites-enabled/ directories. For each domain or subdomain you will be hosting, you will create a new file containing a server block in the sites-available directory and then create a symlink to it in the sites-enabled directory. This way, you can easily manage your domains and subdomains with the same configuration files.
  1. Unlock the Power of the Command Line: Install NGINX, tmux, and tar Today!
sudo dnf update
sudo dnf install @nginx tmux tar
  1. Start Your Web Server Quickly with NGINX: Enable Automatic Reboots!
sudo systemctl start nginx
sudo systemctl enable nginx
  1. Unlock the Power of Your Own Website: Create Your Site’s Root Directory Today!
sudo mkdir -p /var/www/example.com
  1. Secure Your Web Content with SELinux’s chcon Command!
sudo chcon -t httpd_sys_content_t /var/www/example.com -R

 

 

 

sudo chcon -t httpd_sys_rw_content_t /var/www/example.com -R
  1. Unlock the Power of NGINX Configuration: Create Directories to Store Your Site’s Files!
sudo mkdir -p /etc/nginx/{sites-available,sites-enabled}
  1. Create Your Perfect NGINX Site Configuration in Minutes with this Easy Step-by-Step Guide!
File: /etc/nginx/sites-available/example.com
#Names a server and declares the listening port
server {
listen 80 default_server;
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;
}
}
  1. Create a Symbolic Link from NGINX Configuration File to Sites-Enabled Directory Now!
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  1. Unlock the Power of Your Server: Learn How to Easily Update Your NGINX Configuration File Right Now!
  • Eliminate Default Server Parameter to Maximize Performance – How to Remove the listen Directive’s default_server Parameter
  • Learn How to Include Directives to the Nginx Sites-Enabled Directory and Enhance Your Configuration File!
File: /etc/nginx/nginx.conf
...
http {
...
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

server {
listen 80;
listen [::]:80;
...
}
  1. Unlock The Power Of Secure Online Connectivity: Open Your System’s Firewall For HTTP & HTTPS Traffic!
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload
  1. Ensure Your Site’s Smooth Operation – Check for Syntax Errors in Your Configuration File!
sudo nginx -t
Experience Unparalleled Comfort and Relaxation with Our Revolutionary New Product!
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. Bring Your Site Back To Life – Learn How To Restart NGINX!
sudo systemctl restart nginx

Ready to Launch Your Site? Create Your Site’s Index File Now and Get Ready to Go Live!

  1. Learn How to Create Your Site’s Index File in the Root Directory – Step-by-Step Guide!
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-centos-8/">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>

Create Your Node.js Web Server in Minutes – Unbelievable Speed and Performance!

Unlock the Power of Node.js – Instantly Install and Get Started Now!

  1. Unlock Maximum Versatility with Node.js: Install the Node Version Manager (NVM) Today!
sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  1. Unlock the Power of nvm in Your Terminal Instantly with These Easy Commands!
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 NVM Version Now – Make Sure You Have the Most Up-to-Date Access!
nvm --version
See the Incredible Results: What You Get When You Follow This Simple Output!
0.35.3
  1. Unlock Endless Possibilities – Get Started By Installing Node.js Today!
nvm install 12.16.2
  1. Take Control of Your Development Environment – Use NVM to Run the Node.js Version You Need!
nvm use 12.16.2
Bring Your Vision to Life: Take Control of Your Creative Destiny with Our Innovative Solutions!
Now using node v12.16.2 (npm v6.14.4)

Create Your JavaScript Test File Now: Uncover Hidden Bugs and Enhance Code Quality!

In the Install and Configure NGINX section, you successfully configured NGINX to listen on port 80 to serve its static content and also configured a reverse proxy to your Linode’s localhost:3000. Consequently, in this section you will create the test.js file to be able to test your Node.js web server when a request for the /test.js file is made, which will be created in the next section.
  1. Unlock the Power of Your Site – Create a test.js File in Your Root Directory Now!
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: Learn How to Create Your Very Own Web Server File!

In this section, you will create a file named server.js that will use Node.js modules to construct a simple web server, capable both of handling client requests and returning responses to them. Specifically, you will use the modules to write a server that can manage client requests and return responses accordingly.
  1. Create a Server.js File in Your Site’s Root Directory and Kickstart Your Website!
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. Start a New Tmux Session and Take Control of Your Terminal!
tmux
Discover the Incredible Benefits of [Content Name] – Get Ready to Be Amazed!
  1. Unlock the Power of Your Test.js File: Learn How to Navigate to Your Root Directory!
cd /var/www/example.com
  1. Keep Your Node.js Web Server Running in the Background – Append & to Command for Continuous Processing!
node server.js &
Get Instantly Connected to the Command Line: CTRL+C to Return to Your Command Prompt!
  1. Say Goodbye to Your Tmux Session – Easily Exit in Just a Few Steps!
exit
  1. Experience Your Site Instantly – Open Your Browser and Navigate to Your Domain Now!
  1. Start Serving Dynamic Content Instantly – Click the ‘Go to test.js’ Button Now!
  1. See the Current Date and Time Instantly – Click the ‘Display the Date and Time’ Button Now!
Having completed the basic configurations to proxy requests to the Node.js server, the next step is to consider further NGINX configurations for serving static content and dynamic content from a reverse proxy. Furthermore, this may help to optimize performance and security.
Discover the Power of JavaScript: Explore Express.js, Ember.js, and Vue.js to Develop Your Web Apps!

Unlock the Secrets of More Knowledge: Discover What’s Hidden Behind ‘More Information’!

Despite the potential benefits of consulting the resources provided, it is important to note that we cannot guarantee their accuracy or timeliness. Therefore, while these resources may be useful, it is advisable to exercise caution when using them.