Back to blog
Mar 16, 2024
4 min read

How to run a website on VPS

How to Run a website nodejs/expressJs on Ubuntu/Debian vps, nginx configuration

Step 1: Setting Up Your VPS

VPS hosting services are mostly working based on Linux so that you’ll get optimal security for your web platform.

Configuring your VPS is easy, and you need to update and upgrade your VPS at the first step by entering the following command in your terminal:

sudo apt-get update && sudo apt-get upgrade 

Node.js is a part of the open-source community, and it comes with Ubuntu’s official repository by default. So, you only need to enter the following command to get the final stable version of Node.js. Learn more about checking Node version. and install other packages such as npm

sudo apt-get install nodejs npm nginx 

Step 2: update the nodejs application on the server

update using nvm, command below! Learn more about checking nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
node -v && npm -v

Step 3: Configuring Your VPS to Run Your Node.js Application

We can create a simple Node.js application that is running on port number 3000, as below. By entering the command “node app.js,” your app will be started.

cd your_app_node

install dependencies

npm install 

install pm2 to run in real-time

npm install -g pm2 

run the app using pm2

pm2 start app.js --name myapp

check logs your app

pm2 logs myapp

If so, your app will run on the port that matches the port you set if you want to stop pm2 app logs ctrl + c

step 4: configurasi nginx server

Nginx is a powerful web hosting software used by a lot of companies worldwide. It is used as a reverse proxy and helps us provide remote connections to our Node.js application. By default, our Node.js application runs as a local app, and Nginx helps us map requests to the port number where our Node app is running.

Now cd to Nginx virtual host configuration directory and create a server block to establish a reverse proxy.

sudo nano /etc/nginx/sites-available/example_domain.com
server {
    listen 80;
    server_name example_domain.com;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }
} 

Now all the traffic that targets the domain SUBDOMAIN.DOMAIN.TLD will be forwarded to https://PRIVATE_IP:3000, where your Node.js app is running.

set your domain configuration to sites-enabled

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

It’s advisable to test the configuration after saving the above lines by entering the following command in the terminal.

sudo nginx -t

If everything is good, you will get the success message.

The next step involves restarting the Nginx service to load the config file you just changed for it.

sudo service nginx restart

Conclusion

VPS hosting allows developers to easily and efficiently deploy their Node applications in minutes. There is a defined step-by-step process for this, and you only need to install and configure some Node.js tools to start your web platform. If you want to get rid of Node.js app deployment issues, VPS hosting is a trustworthy and reliable solution.