bolt Valebyte VPS from $4/mo — NVMe, 60s deploy.

Get a VPS arrow_forward

Node.js Hosting on VPS: PM2, Nginx, and SSL in 15 Minutes

calendar_month July 01, 2026 schedule 18 min read visibility 29 views
person
Valebyte Team
Node.js Hosting on VPS: PM2, Nginx, and SSL in 15 Minutes

Deploying a Node.js application on a VPS with PM2 for process management, Nginx as a reverse proxy, and a free SSL certificate from Let's Encrypt can be done in less than 15 minutes by following this step-by-step guide, which covers the entire process: from choosing the optimal VPS to configuring autostart and monitoring.

Hosting Node.js applications on a Virtual Private Server (VPS) is one of the most popular and effective solutions for developers seeking flexibility, control, and performance. Unlike shared hosting or PaaS platforms, a VPS gives you full root access to the operating system, allowing you to finely tune the environment to the specific needs of your Node.js project. This is critically important for a production environment, where stability, security, and speed play a key role.

This guide from Valebyte.com will walk you through the entire process of deploying Node.js on a VPS, using a proven technology stack: NVM for easy Node.js version management, PM2 for reliable application startup and monitoring, Nginx as a high-performance reverse proxy, and Certbot for automatic acquisition and renewal of free SSL certificates from Let's Encrypt. We will focus on practical steps and specific commands so you can quickly and efficiently launch your Node.js application into production.

Which VPS to choose for a Node.js application?

Choosing the right VPS for your Node.js application is the first and one of the most important steps. The optimal configuration depends on the expected load, application complexity, and performance requirements. Overpaying for excessive resources is just as undesirable as a lack of capacity, which will lead to slowdowns and failures.

Minimum Requirements and Recommendations

For most small to medium Node.js applications, including API services, blogs, small online stores, or testing environments, modest resources are sufficient. However, applications with high load, a large number of concurrent users, or intensive computations will require more powerful configurations.

Key parameters to consider:

  • Processor (CPU): Node.js is a single-threaded environment, so a high clock speed of a single core is often more important than a large number of weaker cores. However, for running multiple instances of a Node.js application (e.g., with PM2 in cluster mode) or other services (Nginx, database), multiple cores will be beneficial. A minimum of 1 vCPU, but 2 vCPUs are preferable for stable operation.
  • Random Access Memory (RAM): Node.js applications can consume a significant amount of memory, especially when working with large datasets, many requests, or complex operations. A minimum of 2 GB RAM is recommended for a production environment, especially if a database (e.g., MongoDB, PostgreSQL) will also run on the same VPS. For more complex applications or multiple services, it's better to start with 4 GB RAM.
  • Disk Space (Storage): The type and volume of the disk affect application loading speed, log management, and database operations. NVMe drives significantly outperform regular SSDs in read/write speeds, which is critically important for I/O-intensive applications. Volume: a minimum of 25-50 GB for the system, application, and logs. If you plan to store large amounts of data or use a database on the same VPS, more will be required.
  • Network Bandwidth: High network connection speed is important for web applications. Most VPS providers offer ports from 1 Gbit/s. Pay attention to the monthly traffic limit to avoid additional costs.
  • Operating System: Ubuntu Server (20.04 LTS or 22.04 LTS) is the most popular choice due to extensive documentation and a large community. CentOS/AlmaLinux are also good options.

Scaling and Tariff Selection

Valebyte.com offers various VPS plans that can be adapted to your needs. Here's an approximate table to help you navigate:

Usage Scenario vCPU RAM Disk (NVMe) Bandwidth Approx. Cost/month
Testing/Development
(1-5 users, low load)
1 1-2 GB 25 GB 500 GB - 1 TB $5 - $10
Small Production
(10-50 users, moderate load, API)
2 2-4 GB 50 GB 1-2 TB $10 - $25
Medium Production
(50-200 users, medium load, database)
4 4-8 GB 80-160 GB 2-4 TB $25 - $50
High-Load Production
(200+ users, high load, cluster)
6-8+ 8-16+ GB 160-320+ GB 4-8+ TB $50 - $100+

To start, especially if you are deploying Node.js on a VPS for the first time, it is recommended to choose a plan with 2 vCPU and 2-4 GB RAM. This will provide sufficient headroom for most applications and allow comfortable operation with the system, Nginx, and PM2. As your project grows, you can always easily scale your VPS resources on Valebyte.com.

Preparing the VPS for Node.js Deployment: Initial Setup and Security

Before proceeding with Node.js installation and deploying your application, it is necessary to perform a few basic steps to set up and secure your new VPS. These steps are standard practice and ensure a stable and protected environment.

SSH Connection and User Creation

After activating your VPS, you will receive an IP address and credentials for SSH access (usually the root user and password). Connect to the server:

ssh root@YOUR_IP_ADDRESS

First, it is recommended to create a new user with limited privileges and use it for daily operations instead of root. This significantly enhances security.

adduser your_user

Now, add the new user to the sudo group so they can execute commands with administrator privileges when necessary:

usermod -aG sudo your_user

Exit the root session and log in as the new user:

exit
ssh your_user@YOUR_IP_ADDRESS

Going forward, all commands requiring administrator privileges will be prefixed with sudo.

System Update and Firewall Configuration (UFW)

Always start by updating the package manager and installed packages to get the latest security fixes and new software versions.

sudo apt update
sudo apt upgrade -y

Next, we will configure the firewall (UFW - Uncomplicated Firewall) to restrict server access to only the necessary ports. This is a critically important step for Node.js production.

  1. Allow SSH: So you can continue connecting to the server.
  2. sudo ufw allow ssh
    
  3. Allow HTTP and HTTPS: For your Node.js application's web traffic.
  4. sudo ufw allow http
    sudo ufw allow https
    
  5. Enable the firewall:
  6. sudo ufw enable
    
  7. Check firewall status:
  8. sudo ufw status
    

Your VPS is now ready for further configuration. You have created a secure environment for deploying Node.js on a VPS.

Looking for a reliable server for your projects?

VPS from $10/month and dedicated servers from $9/month with NVMe, DDoS protection, and 24/7 support.

View offers →

Installing Node.js on a VPS with NVM: Flexibility and Version Management

To install Node.js on your VPS, we will use NVM (Node Version Manager). This tool allows you to easily install, switch, and manage multiple Node.js versions on a single server. This is especially useful for Node.js hosting when you might need to run different applications with different Node.js versions or update versions without conflicts.

Installing NVM (Node Version Manager)

Installing NVM is quite straightforward. You can download and run the installation script from GitHub:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

After executing this command, you need to either restart your terminal or run the source command to make NVM available in your current session:

source ~/.bashrc

Or for Zsh:

source ~/.zshrc

Verify that NVM is installed correctly:

nvm --version

If you see a version number, NVM is ready to use.

Installing Node.js and npm/yarn

Now that NVM is installed, you can easily install the desired Node.js version. It is recommended to use the latest LTS (Long Term Support) version, as it provides stability and long-term support. At the time of writing, this is, for example, 20.x.x.

nvm install 20

This command will install the latest Node.js 20.x.x version and the corresponding npm version. You can install any other version, for example, nvm install 18.

After installation, NVM will automatically set this version as the current one. You can verify this by checking the Node.js and npm versions:

node -v
npm -v

To set this version as the default, which will be used every time you log in:

nvm alias default 20

If your application uses Yarn instead of npm, install it globally:

npm install -g yarn

Your VPS is now fully ready for configuring Node.js on a VPS and launching your application.

rocket_launch Quick pick

Need a dedicated server?

Compare prices from top providers. Configure and order in minutes.

Browse dedicated servers arrow_forward

Deploying a Node.js Application and Running with PM2: Monitoring and Autostart

Once Node.js is installed, the next step is to deploy your application and ensure its stable operation in production using the process manager PM2. PM2 is a powerful tool that allows you to run Node.js applications as daemons, automatically restart them in case of failures, manage logs, and monitor performance. This is the foundation for any Node.js production deployment.

Cloning the Repository and Installing Dependencies

It is assumed that your Node.js project is located in a Git repository (e.g., GitHub, GitLab, Bitbucket). Navigate to your user's home directory or a directory created for projects, such as /var/www.

cd /var/www
sudo git clone YOUR_REPOSITORY_URL your-application
sudo chown -R your_user:your_user your-application
cd your-application

If your repository is private, you will need to configure SSH keys or use an access token.

Now, install all your project's dependencies:

npm install
# or
yarn install

If your application requires building (e.g., React, Vue, Angular), execute the corresponding command:

npm run build
# or
yarn build

Running the Application with PM2

PM2 is a process manager for Node.js that keeps your application running 24/7. Install PM2 globally:

npm install pm2 -g

Now, launch your Node.js application using PM2. If you have an app.js, server.js, or similar file that starts your application:

pm2 start app.js --name "my-node-app"

If you have a script in package.json, for example "start": "node server.js", you can run it like this:

pm2 start npm --name "my-node-app" -- start

Check the status of running applications:

pm2 list

You should see your application in the list with an online status.

PM2 also provides convenient commands for monitoring and management:

  • View real-time logs: pm2 logs my-node-app
  • Restart application: pm2 restart my-node-app
  • Stop application: pm2 stop my-node-app
  • Delete application from PM2: pm2 delete my-node-app
  • Monitor resources (CPU, RAM): pm2 monit

For PM2 to automatically start your applications after a server reboot, you need to generate an autostart script:

pm2 startup systemd

Execute the command that PM2 outputs in response (it will look something like this: sudo env PATH=$PATH:/home/your_user/.nvm/versions/node/v20.11.0/bin /home/your_user/.nvm/versions/node/v20.11.0/lib/node_modules/pm2/bin/pm2 startup systemd -u your_user --hp /home/your_user). Copy and execute it.

Then save the current list of PM2 applications so they are restored on autostart:

pm2 save

Your Node.js application is now running and managed by PM2, ensuring its reliable operation in a Node.js production environment on a VPS.

Configuring Nginx as a Reverse Proxy for Node.js: Availability and Security

While PM2 excels at managing Node.js applications, exposing them directly to the internet is not recommended. Instead, Nginx is used as a high-performance web server acting as a reverse proxy. Nginx will receive all incoming HTTP/HTTPS requests and forward them to your Node.js application running under PM2. This allows for efficient handling of static files, caching requests, providing load balancing (if you have multiple instances), and most importantly, simplifies SSL configuration. This is a key component for the PM2 Nginx Node stack.

Installing Nginx

Install Nginx from your operating system's repositories:

sudo apt install nginx -y

After installation, Nginx should start automatically. You can check its status:

sudo systemctl status nginx

You should see that Nginx is active (active (running)).

If you haven't already opened the HTTP port in UFW, do so now:

sudo ufw allow 'Nginx HTTP'
sudo ufw reload

Now, if you navigate to your VPS's IP address in a browser, you should see the standard Nginx welcome page.

Nginx Configuration for Node.js

Now we will configure Nginx to act as a reverse proxy. Create a new configuration file for your site. It is recommended to use your domain name for the configuration file, for example, your_domain.com.

sudo nano /etc/nginx/sites-available/your_domain.com

Insert the following configuration, replacing your_domain.com with your actual domain and YOUR_APPLICATION_PORT with the port your Node.js application is listening on (default is 3000, but could be 8000, 5000, etc. — ensure your Node.js server is listening on this specific port). Typically, Node.js listens on localhost:PORT.

server {
    listen 80;
    listen [::]:80;

    server_name your_domain.com www.your_domain.com;

    location / {
        proxy_pass http://localhost:YOUR_APPLICATION_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;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # If your Node.js application also serves static files,
    # you can configure Nginx to serve them directly for improved performance.
    # For example, if static files are located in the 'public' directory of your project:
    # location /static/ {
    #     alias /var/www/your-application/public/;
    #     expires 30d;
    #     add_header Cache-Control "public, no-transform";
    # }
}

Save the file (Ctrl+X, Y, Enter).

Now, activate this configuration file by creating a symbolic link in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/

Remove the default Nginx configuration file to avoid conflicts:

sudo rm /etc/nginx/sites-enabled/default

Check the Nginx configuration syntax for errors:

sudo nginx -t

If everything is in order (you will see syntax is ok and test is successful), restart Nginx to apply the changes:

sudo systemctl restart nginx

Now, if you have correctly configured the DNS records for your domain (an A-record pointing to your VPS's IP), your Node.js application should be accessible via your domain name. This is an important step for configuring Node.js on a VPS for production.

Free SSL for Node.js with Let's Encrypt and Certbot: Traffic Encryption

Having an SSL certificate (HTTPS) is a mandatory requirement for any modern web application. It encrypts traffic between the client and server, protecting user data and increasing trust. Fortunately, thanks to Let's Encrypt and the Certbot tool, obtaining and configuring free SSL certificates for your Node.js hosting has become incredibly simple.

Installing Certbot

Certbot is a Let's Encrypt client that automates the process of obtaining and installing SSL certificates. For Ubuntu, it is recommended to use snapd to install Certbot, as this ensures you get the most up-to-date version.

  1. Update snapd:
  2. sudo snap install core; sudo snap refresh core
    
  3. Install Certbot:
  4. sudo snap install --classic certbot
    
  5. Create a symbolic link for Certbot:
  6. sudo ln -s /snap/bin/certbot /usr/bin/certbot
    

Obtaining and Automatically Renewing the SSL Certificate

Certbot can automatically configure Nginx to use SSL. Ensure that your domain is already configured in Nginx (as in the previous step) and that DNS records point to your VPS.

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

During the execution of this command, Certbot will ask you a few questions:

  • Your email address (for certificate expiration notifications).
  • Agreement to Let's Encrypt's terms of service.
  • Whether you want to redirect HTTP traffic to HTTPS (it is recommended to choose "2" for automatic redirection).

Upon successful completion, Certbot will automatically update your Nginx configuration, adding settings for HTTPS and redirection. You can check the updated Nginx configuration file at /etc/nginx/sites-available/your_domain.com.

Your Node.js application should now be accessible via HTTPS, and all HTTP requests will be automatically redirected. Try opening https://your_domain.com in your browser.

Automatic certificate renewal:

Let's Encrypt certificates are valid for 90 days. Certbot automatically creates a cron job or systemd timer to check and renew certificates before their expiration. You can test the renewal mechanism:

sudo certbot renew --dry-run

If the command completes without errors, then automatic renewal will work correctly. This ensures the uninterrupted operation of your PM2 Nginx Node stack with continuous SSL encryption.

rocket_launch Quick pick

Need a dedicated server?

Compare prices from top providers. Configure and order in minutes.

Browse dedicated servers arrow_forward

Autostart PM2 and Nginx on Server Reboot: Production Environment Reliability

In a production environment, it is crucial that your Node.js application and the Nginx web server automatically start after any VPS reboot. This ensures continuous availability of your service without manual intervention. We have already partially covered this topic when configuring PM2, but let's ensure all components are set up for autostart.

Configuring PM2 for Autostart

As mentioned earlier, PM2 has built-in functionality for creating autostart scripts. If you haven't done so already, execute the following commands:

  1. Generate the systemd autostart script:
  2. pm2 startup systemd
    

    PM2 will output a command that you need to execute with sudo. It will include the full path to the PM2 executable and the user's home directory. Example:

    sudo env PATH=$PATH:/home/your_user/.nvm/versions/node/v20.11.0/bin /home/your_user/.nvm/versions/node/v20.11.0/lib/node_modules/pm2/bin/pm2 startup systemd -u your_user --hp /home/your_user
    

    Execute this command.

  3. Save the current list of PM2 applications:
  4. pm2 save
    

    This command saves the current state of all running PM2 processes. On the next system startup, PM2 will use this saved list to automatically launch your applications. This is critically important for Node.js production.

Checking Service Status

Nginx is configured to autostart by default after installation. You can verify this by checking its status:

sudo systemctl is-enabled nginx

If you see enabled, then Nginx will start on every reboot. If not, enable it:

sudo systemctl enable nginx

To verify that everything is configured correctly, perform a full reboot of your VPS:

sudo reboot

After the reboot (wait a few minutes for all services to start), reconnect via SSH and check the status of Nginx and PM2:

sudo systemctl status nginx
pm2 list

Both services should be active, and your Node.js application should be in online status. Also, ensure your website is accessible via your domain name over HTTPS. Your Node.js on VPS is now configured for maximum reliability.

Monitoring, Logging, and Scaling Node.js Applications

After successfully deploying a Node.js application on a VPS and configuring all components, the next stage is to ensure its stable and efficient operation in the long term. This includes monitoring performance, analyzing logs to identify issues, and planning for scaling as the load grows.

Viewing Logs and Metrics

PM2 Logs: PM2 automatically collects logs from your Node.js application. This is very convenient for debugging and understanding application behavior.

  • View real-time logs for a specific application:
    pm2 logs my-node-app
            
  • View logs for all applications:
    pm2 logs
            
  • View errors:
    pm2 logs --err
            
  • View PM2 system logs:
    pm2 monit
            

    This command opens an interactive monitoring dashboard, showing CPU usage, RAM, RPS (requests per second), and other information for all your applications.

  • Log file location: By default, PM2 logs are stored in the ~/.pm2/logs/ directory. You can configure this in your application's PM2 configuration file.

Nginx Logs: Nginx also maintains detailed access and error logs, which can be useful for debugging web server issues or understanding incoming traffic.

  • Access logs: /var/log/nginx/access.log
  • Error logs: /var/log/nginx/error.log
  • You can view them using tail -f or cat commands.

System Logs: For a deeper analysis of server issues, system logs can be useful:

  • sudo journalctl -u nginx (Nginx logs via systemd)
  • sudo journalctl -u pm2-your_user (PM2 service logs via systemd)
  • /var/log/syslog (general system logs)

When and How to Scale Node.js on a VPS

Sooner or later, as your application's popularity grows, you will need to scale. There are two main approaches:

  1. Vertical Scaling: Increasing the resources of the current VPS (adding CPU, RAM, disk space). This is the simplest method, which can be done on Valebyte.com by simply choosing a more powerful plan.
    • When: When the application reaches its performance ceiling on the current VPS, but the load is not yet high enough to require a complex distributed architecture. For example, if the CPU is constantly at 90%+ utilization or RAM is exhausted.
    • Advantages: Simplicity, no need to change the application's architecture.
    • Limitations: Every server has a physical limit to its resources.
  2. Horizontal Scaling: Adding new VPS instances and distributing the load among them. For Node.js, this can mean running multiple application instances on a single VPS (using PM2 in cluster mode) or on different VPS instances (using a load balancer).
    • PM2 in cluster mode: PM2 allows you to run multiple Node.js processes on a single VPS, utilizing all available CPU cores.
      pm2 start app.js -i max --name "my-node-app"
                      

      -i max tells PM2 to launch as many instances as there are CPU cores available on the server. Nginx will automatically distribute requests among these processes.

    • Multiple VPS and a Load Balancer: For very high-load applications, multiple VPS instances might be required, each with its own Node.js application, and an external load balancer (e.g., Nginx on a separate VPS, a cloud load balancer) to distribute traffic among them. This is a more complex architecture but provides maximum fault tolerance and scalability.
    • When: When vertical scaling is no longer sufficient, or high fault tolerance is required.
    • Advantages: Nearly unlimited scalability, high fault tolerance (if one server goes down, others continue to operate).
    • Limitations: Increased architectural complexity, necessity of synchronizing state between servers (e.g., via Redis, database).

Planning for scaling in advance will help you avoid critical performance issues. Starting with a single VPS, as we described, is an excellent approach for Node.js hosting, and then gradually increasing resources or adding new servers as needed. If you need to migrate data to a more powerful server, refer to our guide on migrating a website to another hosting without downtime.

Conclusion

Deploying a Node.js application on a VPS using NVM, PM2, Nginx, and Let's Encrypt provides a powerful, flexible, and secure environment for production projects. This stack allows for efficient management of Node.js versions, ensures uninterrupted application operation, serves requests with high performance, and provides traffic encryption, which is the standard for the modern web.

For a successful Node.js on VPS setup, choosing a reliable provider is critically important. Valebyte.com offers high-performance VPS with NVMe drives and stable network channels, which are ideal for any Node.js application, from small APIs to high-load services. Start with a basic plan and scale resources as your project grows to always maintain an optimal balance of price and performance.

Ready to choose a server?

VPS and dedicated servers in 72+ countries with instant activation and full root access.

Get started now →
support_agent
Valebyte Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.