Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Move a Node API From a Free Host to a VPS (Step by Step)

Hi, I'm Arthur. Free hosting is a great way to get your first backend online. You can build an API, connect a frontend, test your idea, and start getting users without worrying about server costs. But eventually, you may notice things like cold starts, memory limits, sleeping applications, restricted background processes, or simply no SSH access when something goes wrong. That's usually when a…

To move a Node.js API from a free host to a VPS, follow these steps:

1. Set up a non-root user on your VPS for security. Connect using SSH:

```

ssh root@YOUR_SERVER_IP

```

Create the new user:

```

adduser deploy

usermod -aG sudo deploy

```

2. Install Node.js and Nginx on your server:

```

sudo apt update && sudo apt upgrade -y

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

sudo apt install -y nodejs nginx git

```

3. Configure the firewall with UFW (Uncomplicated Firewall). Allow SSH and Nginx:

```

sudo ufw allow OpenSSH

sudo ufw allow 'Nginx Full'

sudo ufw enable

```

4. Move your application repository to the new user's home directory and install dependencies:

```

cd /home/deploy

git clone https://github.com/YOUR_USER/YOUR_REPO.git api

cd api

npm ci --omit=dev

```

5. Use systemd to keep Node.js running. Create an environment file and systemd service file:

```

sudo nano /etc/api.env

DATABASE_URL=postgres://...

JWT_SECRET=change-this

sudo chmod 600 /etc/api.env

sudo nano /etc/systemd/system/api.service

```

6. Configure Nginx as a reverse proxy for Node.js. Create a new Nginx configuration file:

```

sudo nano /etc/nginx/sites-available/api.example.com

```

Add the following configuration:

```

server {

listen 80;

server_name api.example.com;

location / {

proxy_pass http://localhost:3000;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

}

```

Enable the configuration and test it:

```

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

sudo nginx -t

```

7. Redirect HTTP to HTTPS by creating another Nginx configuration file for HTTPS:

```

sudo nano /etc/nginx/sites-available/api.example.com-ssl

```

Add this configuration:

```

server {

listen 443 ssl;

server_name api.example.com;

ssl_certificate /path/to/your/certificate.crt;

ssl_certificate_key /path/to/your/certificate.key;

location / {

proxy_pass http://localhost:3000;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

}

```

Enable this configuration:

```

sudo ln -s /etc/nginx/sites-available/api.example.com-ssl /etc/nginx/sites-enabled/

```

8. Restart Nginx:

```

sudo systemctl restart nginx

```

9. Start the Node.js service using systemd and enable it to start on boot:

```

sudo systemctl enable --now api

```

10. Test your application by curl-ing the health endpoint:

```

curl http://127.0.0.1:3000/health

```

Your Node.js API should now be running on your VPS with HTTPS enabled and managed by systemd.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Monday 5 October →