Hosting & Domaining Forum

Hosting Discussion => Managed Hosting and Services => Topic started by: Piyush on Dec 22, 2022, 03:05 AM

Title: landing webpage (node js) on VDS SSD hosting
Post by: Piyush on Dec 22, 2022, 03:05 AM
Wishing everyone the best of luck!
In this discussion, I would like to explore the process of deploying a landing page, using Node.js, on a Debian server that is hosted on a VPS with SSD without requiring administration.
To connect to the server, I utilized PuTTY.

I'm seeking advice from individuals who have experience setting up and deploying websites for hosting. Specifically, I'm not entirely familiar with how to do it efficiently without causing congestion.
Would appreciate any helpful tips and guidance on where to begin.
Title: Re: landing page (node js) on VDS SSD hosting
Post by: curaqua on Dec 22, 2022, 03:57 AM
Here's a simple method to get started:

1. Create a working directory by typing the command:
mkdir workplace

2. Move into your newly created workplace directory by typing:
cd workplace

3. Upload your project repository with the command:
git clone [path to your project]

4. Navigate to your project directory using the command:
cd [your project directory name]

5. Install any dependencies required for your project using the command:
npm install

6. If your project requires configs, secrets, and api keys, make sure you add them.

7. Set up NGINX using the following configuration block:

server {
    listen 80;
    server_name example.com;
    access_log /var/log/nginx/example.com;

    location / {
        proxy_pass        "http://127.0.0.1:3000";
    }
}

8. Finally, start your project using the command:
npm start.
Title: Re: landing page (node js) on VDS SSD hosting
Post by: messnct on Feb 28, 2023, 03:03 AM
One query that commonly arises is how to prevent node_modules from being added to the repository. In my case, I attempted to address it by creating a .gitignore file for my express-based project that included the string node_modules. However, I soon realized that my application wasn't running on the server due to the "absence of express."
Although adding the node_modules directory to the repository would solve this issue and ensure the app's proper functioning, it is not the recommended practice.
So, what can be done to resolve this problem?
Title: Re: landing webpage (node js) on VDS SSD hosting
Post by: BluellFrono on Oct 13, 2023, 01:15 AM
I'm assuming that you've already installed Node.js on your Debian server. If not, that's the first step.

Now, here are the steps you need to follow to deploy your landing page using Node.js on your Debian VPS:

1. Connect to your Server
Using PuTTY, connect to your Debian server. You'll need your server's IP address or domain and your login credentials.

2. Update your Server
Once you're logged in, it's a good practice to ensure that your server is up-to-date with the latest packages. You can do this with the following commands:

sudo apt-get update
sudo apt-get upgrade
3. Install Node.js and npm
If you haven't already installed Node.js and npm (Node package manager), do so with the following command:

sudo apt-get install nodejs npm
You can check your Node.js and npm versions with the commands node -v and npm -v respectively.

4. Clone Your Project
Now, navigate to the directory where you want to host your Node.js project and clone your project into this directory. For example, if your project is on GitHub, you would use a command like:

git clone https://your-git-repo-url
5. Install Dependencies
Next, navigate into your project directory and install your project's dependencies, which are listed in your package.json file. You can install these dependencies with the following command:

npm install
6. Set Up A Process Manager
A process manager will help keep your app running persistently and re-start it if necessary. An example of a Node.js process manager is 'pm2'. Install and set this up with:

sudo npm install pm2 -g
pm2 start your-app-main-file.js
Ensure to replace "your-app-main-file.js" with the entry point to your Node.js application.

Additionally, ensure that the process manager will start on boot with the following command:

pm2 startup
Then, follow the instruction displayed by the pm2 startup command:

sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u your-username --hp /home/your-username
Save the current running processes with:

pm2 save
7. Set Up a Reverse Proxy
Setting up a reverse proxy like Nginx can help manage traffic and optimize performance. Install Nginx with:

sudo apt-get install nginx
Next, modify the Nginx config file:

sudo nano /etc/nginx/sites-available/default
You can remove the current server block and replace it with a server block that points to your application. Here's an example of what that might look like:

server {
    listen 80;

    server_name your_server_ip;

    location / {
        proxy_pass http://localhost:your_node_port;
        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;
    }
}
Don't forget to replace "your_server_ip" and "your_node_port" with your actual server IP address and the port your Node.js app is running on.

Then, check the syntax of your configuration edits:

sudo nginx -t
If the syntax is okay, restart Nginx to apply the change:

sudo systemctl restart nginx
8. Check Your Application
Finally, you should be able to navigate to your server's IP address in a web browser and see your running application.

This should give you a basic single-page application running on your server. There can be additional steps to set up things like SSL (for HTTPS), utilizing a domain name instead of an IP address, setting up CI/CD, etc. which are not covered here since it's not part of the original question but are definitely aspects you should consider exploring.


If you're looking for further steps to enhance your setup, consider the following areas:

Setup Domain
Usually, accessing your application via an IP address isn't preferable. You'll want to point a domain or subdomain to your server. To do this, you need to adjust your DNS settings where your domain is registered.

Set your domain A Record to point to your server's IP address. This process can vary depending on where your domain is registered, but the fundamental idea is the same.

Setup SSL (for HTTPS)
An SSL certificate is important for ensuring your website communication is secured. Let's Encrypt offers free SSL certificates and certbot can be used for easy setting up. Here are the simplified steps:

Install certbot and the Nginx plugin with:

sudo apt-get install certbot python3-certbot-nginx
Get and install certs with:

sudo certbot --nginx
Follow the instructions and ensure to allow redirection from HTTP to HTTPS.

This will make Certbot edit your Nginx configuration automatically to serve it over HTTPS.

Continuous Integration/Continuous Deployment
If you're making updates to your app frequently, it can be labor-intensive to manually deploy updates to your live server.

You can setup CI/CD pipelines using GitHub Actions, GitLab CI/CD, Jenkins, CircleCI or similar services. These tools will automate the testing and deployment of your app whenever you push updates to your Git repo.

This process, however, will depend largely on the platform you choose.

Configure Firewall
For security, ensure your server's firewall is properly configured. Using ufw (Uncomplicated Firewall) on Debian, you can only allow specific, necessary services. With Nginx installed, it should register itself as a service so you can use:

sudo ufw allow 'Nginx Full'
Rate Limiting
To protect against DDoS attacks, you might want to add a rate-limit to your Nginx server. Here's a simple example of how you might configure this:

In your previous server block, add the limit_req line:

server {
    listen 80;

    server_name your_server_ip;

    limit_req zone=mylimit burst=10 nodelay;

    location / {
        proxy_pass http://localhost:your_node_port;
        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;
    }
}
And above your server block, declare your limit:

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;

server {
    ...
}
Above configuration limits every IP address to 5 requests per second.

Monitoring
In Production environment, monitoring your applications is crucial. pm2 provides some basic monitoring with pm2 monit.

For more advanced monitoring, you might consider setting up a tool like Prometheus or New Relic.


Here are a few more things you could consider for a more robust deployment:

Dockerize Your Application
Docker allows you to package your application with all of its dependencies into a standardized unit for software development known as a Docker container. Dockerizing your application can make your server setup easier and more reproducible, allow you to run multiple services in an isolated manner, and help with scaling up your application if needed.

Here's a simple example of a Dockerfile for a Node.js application:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]
This Dockerfile starts from the node:14 image, sets the working directory to "/usr/src/app", copies your app and its dependencies into the Docker image, and finally defines the command "node server.js" to be run when a container is started from this Docker image. The EXPOSE 8080 command exposes the port on which your app is running so it can be accessed externally.

Use Kubernetes
For distributing and scaling your application, Google's Kubernetes is a powerful solution. This shouldn't be an initial step, but something to consider if your application's demand starts growing and you need to guarantee that it's always available.

Application Performance Management (APM)
Tools like Elastic APM, New Relic, AppDynamics, or Dynatrace help you to monitor and trace activities in your application. They track error rates, response times, throughput, transactions, and more.

Error reporting
Tools like Sentry or LogRocket will help catch errors right when they occur. Usually, they provide an SDK that hooks into your application and sends data about error occurrences and their context, which helps a lot during the debugging process.

Conclusion
Remember, every application and environment is different and what tools you need depend on your exact case. The above mentioned are common practices, but they might not necessarily fit your situation.

It's also crucial to remember not to try and setup a perfect environment from get-go. Start with simple solutions that cover your current needs and as your application evolves, iteratively improve its environment.

And last but not least, always ensure that you have dоcumented your setup and steps properly. This saves a lot of time and effort when you or someone else has to replicate or troubleshoot the environment.
Title: Re: landing webpage (node js) on VDS SSD hosting
Post by: p2ct1 on Dec 25, 2024, 01:26 AM
Deploying a Node.js landing page on a Debian VPS is straightforward if you grasp the essentials. After logging in via PuTTY, update your system and install Node.js. Set up your application using Express for routing and middleware. To manage your app, leverage PM2, which will keep it alive through crashes and reloads. For optimal performance and to avoid congestion, configure Nginx as a reverse proxy.

This setup will efficiently distribute incoming requests and can handle SSL termination, which is a must for modern web applications. Don't overlook logging and monitoring; tools like Loggly or even basic console.log can help you catch issues before they escalate.