Deploying a TypeScript Application on VPS: A Step-by-Step Guide
You've written a cool application in TypeScript. Everything works perfectly locally, but it's time to show the project to the world. Deploying on a VPS (Virtual Private Server) is a logical and flexible step. Unlike cloud platforms, a VPS gives you full control over the environment.
In this article, we'll break down the step-by-step process of deploying a TypeScript application on a VPS. We'll use Node.js, PM2 for process management, and Nginx as a reverse proxy. All examples are in TypeScript.
It is assumed that you have basic skills in working with Linux and SSH.
1. Server Preparation and Connection
The first step is to rent a VPS (DigitalOcean, Vultr, Timeweb, etc.) and connect via SSH.
ssh root@your_server_ipLet's immediately update the packages and install the necessary utilities:
apt update && apt upgrade -yapt install curl git nginx -yInstall Node.js (we recommend the LTS version):
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -apt install -y nodejsCheck the versions:
node -vnpm -v2. Cloning the Repository and Building TypeScript
Now let's clone your project. For this example, we'll use a simple Express application written in TypeScript:
git clone https://github.com/your_username/your_repository.gitcd your_repositoryInstall the dependencies:
npm installTypeScript needs to be compiled into JavaScript. This is usually done with the command:
npm run buildIn package.json, this should be defined as:
{ "scripts": { "build": "tsc", "start": "node dist/index.js" }}Check that the compiled files appear in the dist folder.
3. Installing and Configuring PM2
PM2 is a process manager for Node.js. It restarts the application if it crashes, supports logs, and clustering.
Install PM2 globally:
npm install pm2 -gStart the application:
pm2 start dist/index.js --name "my-typescript-app"Save the process list so PM2 can restore them after a server reboot:
pm2 savepm2 startupPM2 will output a command that needs to be executed (usually systemctl enable pm2-root). Copy and run it.
You can check the status with the command:
pm2 status4. Configuring Nginx as a Reverse Proxy
Nginx will accept requests on port 80 (HTTP) and forward them to your application (e.g., on port 3000).
Create a configuration file:
nano /etc/nginx/sites-available/my-typescript-appInsert the following code (change server_name to your domain or IP):
server { listen 80; server_name your_domain.ru www.your_domain.ru;
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; }}Activate the site:
ln -s /etc/nginx/sites-available/my-typescript-app /etc/nginx/sites-enabled/nginx -tsystemctl restart nginxNow your application is accessible via IP or domain.
5. Configuring HTTPS (SSL) with Certbot
Going without HTTPS is not an option nowadays. We'll use Let's Encrypt and Certbot.
Install Certbot:
apt install certbot python3-certbot-nginx -yRun the certificate issuance:
certbot --nginx -d your_domain.ru -d www.your_domain.ru