Subscribe - Thapa Technical

Host MERN Stack in VPS using Coolify

Coolify Deployment Guide for MERN Stack

Coolify Deployment Guide

A step-by-step guide to deploy your MERN stack application using Coolify on a VPS

PostgreSQL
Express
React
Node.js
Coolify
1

Install Coolify on VPS

SSH into your VPS and install Coolify using the official script.

ssh user@your_vps_ip
curl -fsSL https://cdn.coollabs.io/coolify/install.sh | bash
2

Access Coolify Web Interface

Once Coolify is installed, it provides public IPv4 and IPv6 links. Click on one to complete the initial setup.

During the configuration process:

  • Choose 'Self-host' option
  • Select 'localhost' for deployment

Note: The installation process might take a few minutes. Keep the terminal window open until it completes.

3

Connect Your Domain

Go to your domain registrar's DNS settings and add the following A records:

Type Name Points to TTL
A @ your VPS IP 60
A * your VPS IP 60

Tip: DNS propagation can take time. You can check the status using online tools like whatsmydns.net

4

Configure Coolify Domain

In Coolify's administration panel:

  1. Navigate to SettingsConfiguration
  2. Update the 'Instance Domain' field with your custom domain
  3. Example: coolify.codewithnepal.com

Note: After changing the domain, Coolify will need to be restarted and you'll need to access it via the new domain.

5

Optional DNS Settings for Local Network

If you experience access issues, adjust your PC's DNS settings:

  1. Go to your PC's Wi-Fi or Ethernet DNS settings
  2. Switch to manual configuration
  3. Set Preferred DNS: 1.1.1.1
  4. Set Alternate DNS: 1.0.0.1

Note: This step is optional and only necessary if you encounter domain resolution issues.

6

Create a New Project in Coolify

Now let's set up your project structure:

  1. Navigate to the Projects section in the Coolify dashboard
  2. Click New Project
  3. Assign a meaningful name to your project (e.g., mern-app)

Tip: Choose a descriptive project name that helps you identify the app easily, especially if you plan to host multiple applications.

7

Add MongoDB Database

Add a MongoDB database to your project:

  1. Within your project, click Add Resource
  2. Select Database
  3. Choose MongoDB from the options
  4. This creates a Docker container running MongoDB
  5. Click Deploy and Start to make it live

Note: Coolify automatically generates connection credentials. Save these credentials safely for connecting your application.

8

Connect to GitHub

Connect your GitHub repository to Coolify:

  1. Back in your project, click Add Resource
  2. Select Private GitHub Repo with GitHub App
  3. Follow the prompts to connect your repository via the GitHub App
  4. Register the webhook when prompted

Tip: This setup enables automatic deployments whenever you push to your repository's main branch.

9

Add the Repository as a Resource

Configure your repository as a deployable resource:

  1. Click Add Resource again
  2. Choose the connected GitHub repo from the list
  3. Select Nixpacks as the build pack
  4. Nixpacks will automatically detect your Node.js app and configure the build process

Note: Nixpacks is a smart build system that detects your project structure and requirements automatically.

10

Configure Application Deployment

Set up the deployment configuration:

  1. Set your custom domain for the application
  2. Add necessary environment variables (database connection, API keys, etc.)
  3. Configure build and start commands:
Build Command: bun run db:migrate && bun run build

Tip: Environment variables are crucial for connecting your application to the PostgreSQL instance. Make sure to include PostgreSQL_URI pointing to the PostgreSQL resource you created earlier.

11

Deploy Your Application

Complete the deployment process:

  1. Review all configuration settings
  2. Click Deploy to start the deployment process
  3. Monitor the deployment logs for any issues
  4. Once complete, your MERN stack app will be live on your specified domain

Success: Congratulations! Your MERN stack application is now deployed with Coolify. Any future changes pushed to your repository will trigger automatic deployments.