Random Bright, Funny, Info, Deep Thoughts, AI Chats, and More

[
[
[

]
]
]

Setting up a Node.js website on a Hostinger VPS is a great choice for performance and control. Since you mentioned you create article and informational web pages, using a VPS allows you to serve dynamic content efficiently while maintaining full control over your environment.
Here is a streamlined guide to getting your Node.js app live.
1. Prepare Your VPS
First, you need to access your server and ensure it is up to date.
* Access via SSH: Open your terminal (or PuTTY on Windows) and log in using the credentials from your Hostinger hPanel.
   ssh root@your_vps_ip

* Update System: Run these commands to ensure everything is current:
   sudo apt update && sudo apt upgrade -y

2. Install Node.js
While you can install Node.js directly via apt, using NVM (Node Version Manager) is recommended so you can easily switch versions later.
* Install NVM:
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc

* Install Node.js:
   nvm install –lts

3. Upload & Start Your Application
Navigate to your web directory (usually /var/www/) and bring in your code.
* Clone or Upload: You can use git clone or upload your files via SFTP.
* Install Dependencies:
   cd your-project-folder
npm install

* Use PM2 for Process Management: PM2 keeps your app running in the background and restarts it if the server reboots.
   sudo npm install -g pm2
pm2 start app.js  # Replace app.js with your entry file
pm2 save
pm2 startup

4. Configure Nginx as a Reverse Proxy
By default, Node.js runs on a port like 3000. To access it via your domain on port 80 (HTTP), you need Nginx.
* Install Nginx: sudo apt install nginx -y
* Configure Proxy: Create a configuration file for your site:
   sudo nano /etc/nginx/sites-available/yourdomain.com

* Add this Configuration:
   server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000; # The port your app runs on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection ‘upgrade’;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

* Enable & Restart:
   sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Pro Tip: SSL Security
Once your site is live, you should secure it with a free SSL certificate from Let’s Encrypt.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot –nginx -d yourdomain.com

Would you like me to help you generate a basic Express.js boilerplate for your informational pages to get you started?

Since you’re ready to move forward, let’s build a simple Node.js + Express entry point. This will give you a functional “Hello World” page that you can later expand into the article or informational web pages you typically create.
1. Initialize Your Project
On your VPS, run these commands inside your project folder:
# Initialize a new Node.js project
npm init -y

# Install Express (the web framework)
npm install express

2. Create the App File
Create a file named app.js:
nano app.js

Paste this basic “Informational Page” boilerplate:
const express = require(‘express’);
const app = express();
const PORT = 3000;

// Simple route for your home page
app.get(‘/’, (req, res) => {
    res.send(`
        <!DOCTYPE html>
        <html>
        <head>
            <title>My Node.js Site</title>
            <style>
                body { font-family: sans-serif; line-height: 1.6; padding: 40px; max-width: 800px; margin: auto; }
                h1 { color: #333; }
            </style>
        </head>
        <body>
            <h1>Welcome to My Informational Site</h1>
            <p>This page is being served dynamically by Node.js on my Hostinger VPS!</p>
        </body>
        </html>
    `);
});

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

3. Launch with PM2
To ensure your site stays online 24/7, use PM2:
pm2 start app.js –name “my-website”

Understanding the Architecture
This diagram shows how a visitor’s request moves from the internet through your Hostinger VPS layers to reach your Node.js code.
Next Steps
Now that the server is running, how would you like to handle your content?
* Static Approach: Would you like to set up a public folder to serve your existing HTML files directly?
* Dynamic Approach: Would you like to set up a Template Engine (like EJS) so you can easily reuse headers and footers across all your article pages?

https://g.co/gemini/share/216565875fab


Discover more from NathanLegakis.com

Subscribe to get the latest posts sent to your email.

Leave a comment