Deploying a TypeScript Application on VPS: A Step-by-Step Guide from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why VPS is the Ideal Place for Your TypeScript Project?



You've written a great application in TypeScript — be it an Express server, a Next.js site, or a NestJS API. Locally, everything works perfectly, but it's time to show the project to the world. Many beginners are afraid of the deployment stage, considering it complex and confusing. In reality, understanding this process once will allow you to deploy any TypeScript projects in 15-20 minutes.



In this guide, we will cover the complete deployment cycle of a TypeScript application on a VPS (Virtual Private Server) — from server preparation to domain and HTTPS setup. The article is focused on the Node.js ecosystem, as TypeScript is most often used with it.



What you will learn:

  • How to prepare a VPS for work (Ubuntu 22.04)
  • How to set up Node.js, TypeScript, and the PM2 process manager
  • How to build (compile) TypeScript in production mode
  • How to configure Nginx as a reverse proxy
  • How to connect a free SSL certificate via Let's Encrypt


1. Preparing the VPS and Installing the Necessary Environment



1.1 Initial Server Setup



Assume you have rented a VPS (e.g., from DigitalOcean, Timeweb, or Reg.ru) with Ubuntu 22.04. First, connect to the server via SSH:



ssh root@your-server-ip


Create a separate user (safer than working as root):



adduser deployerusermod -aG sudo deployersu - deployer


1.2 Installing Node.js and npm



For a TypeScript application, we will need Node.js version 18 or 20 (LTS). Use the official NodeSource PPA:



curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt-get install -y nodejs


Verify the installation:



node -vnpm -v


1.3 Installing TypeScript Globally (Optional)



Although TypeScript usually comes as a local project dependency, you can install it globally for quick compilation checks:



sudo npm install -g typescript


1.4 Cloning Your Project



Copy the repository from GitHub/GitLab to the server:



git clone https://github.com/your-username/your-project.gitcd your-project


Install dependencies:



npm install


2. Building TypeScript and Setting Up PM2



2.1 Compiling TypeScript to JavaScript



Your package.json should have a build script. It usually looks like this:



{  "scripts": {    "build": "tsc",    "start": "node dist/index.js",    "dev": "ts-node-dev src/index.ts"  }}


Run the build:



npm run build


After execution, the compiled JavaScript code will appear in the dist folder. If you have a Next.js project, the build command will be npm run build (Next.js compiles TypeScript itself).



2.2 Installing and Configuring PM2



PM2 is a process manager that keeps your application running, restarts it if it crashes, and manages logs. Install it:



sudo npm install -g pm2


Start the application via PM2 (specify the path to the compiled file):



pm2 start dist/index.js --name "my-typescript-app"


If you have a Next.js project, the command will be different:



pm2 start npm --name "next-app" -- start


Configure PM2 to start automatically on server reboot:



pm2 startup systemdpm2 save


Now your application is running on a local port (e.g., 3000). Let's make it accessible via a domain.



3. Configuring Nginx as a Reverse Proxy



3.1 Installing Nginx




                
                

Blogs

Book Recommendations