Introduction: Why VPS is the Ideal Solution for Your Node.js Application?
\nYou've written a cool JavaScript (Node.js) application, tested it locally, and now you want it to be available online 24/7. Shared hosting is often unsuitable for Node.js due to limitations, and cloud services (Heroku, Railway) can be expensive or unpredictable in terms of performance. A Virtual Private Server (VPS) is the sweet spot: full control, high performance, and an affordable price.
\nIn this step-by-step guide, we will walk through the process of deploying a JavaScript (Node.js) application on a VPS from scratch. You will learn how to set up the server, install the necessary software, securely run the application, and make it accessible via a domain. All code examples are functional and tested.
\n\n1. Server Preparation: Initial VPS Setup
\nFirst, you will need to rent a VPS from any provider (DigitalOcean, Vultr, Timeweb, Selectel). Minimum specifications: 1 vCPU, 1-2 GB RAM, 20-30 GB SSD. The operating system should be Ubuntu 22.04 LTS (the most stable version).
\n\n1.1. Connecting to the Server via SSH
\nAfter receiving the IP address and password, connect to the server via terminal (Mac/Linux) or PuTTY (Windows):
\nssh root@your_server_IP\nImmediately change the password and create a new user (working as root is bad practice):
\nadduser deploy_user\nusermod -aG sudo deploy_user\nsu - deploy_user\n\n1.2. Updating the System and Installing Basic Packages
\nsudo apt update && sudo apt upgrade -y\nsudo apt install -y curl git ufw build-essential\nSet up the basic UFW firewall (leave only SSH and the ports we will use):
\nsudo ufw allow OpenSSH\nsudo ufw allow 80/tcp\nsudo ufw allow 443/tcp\nsudo ufw enable\n\n2. Installing Node.js and the PM2 Process Manager
\nTo run a JavaScript application on the server, we need Node.js. We will use the official NodeSource version manager to install the latest LTS version:
\n\n2.1. Installing Node.js (v20 LTS)
\ncurl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -\nsudo apt install -y nodejs\nVerify the installation:
\nnode -v # v20.x.x\nnpm -v # 10.x.x\n\n2.2. Installing and Configuring PM2
\nPM2 is an advanced process manager for Node.js. It automatically restarts the application on crash, manages logs, and allows running multiple instances (clustering).
\nsudo npm install -g pm2\nConfigure PM2 to start automatically on server reboot:
\npm2 startup systemd\nsudo env PATH=$PATH:/usr/bin pm2 startup systemd -u deploy_user --hp /home/deploy_user\n\n3. Deploying the Application and Running via PM2
\nNow we are ready to transfer your JavaScript application to the server. There are two popular methods: via Git (recommended) or via SCP/FileZilla.
\n\n3.1. Cloning the Repository (Git)
\ncd /home/deploy_user\ngit clone https://github.com/your_username/your_repository.git myapp\ncd myapp\nInstall dependencies and build the project (if required):
\nnpm install\n# If there is a build script: npm run build\n\n3.2. Creating the .env Configuration File
\nNever store secrets in code. Create a .env file in the project folder:
\nnano .env\nExample content:
\nPORT=3000\nNODE_ENV=production\nDATABASE_URL=mongodb://localhost:27017/myapp\nJWT_SECRET=your_super_secret_key\n\n3.3. Starting the Application via PM2
\nAssume the entry point of your application is app.js or server.js. Start it:
pm2 start app.js --name "myapp"\nCheck the status:
\npm2 status\npm2 logs myapp