How to Deploy Your Gatsby Site

Gatsby is a fast and modern React-based static site generator built for performance. In this guide, we’ll walk you through deploying a Gatsby project to an Ubuntu server using CloudRay, a centralised platform that helps you automate server tasks through Bash scripting, manage infrastructure, and execute repeatable deployments.

This guide assumes you have a basic Gatsby project already created. However, if you don’t have a Gatsby project set up yet, you can follow the Gatsby Quick Start Guide to get started.

Contents

Adding Servers to CloudRay

Before getting started, make sure your machine is added to a CloudRay project and connected using the CloudRay Agent.

Create a Deployment Script

Now that your machine is connected to CloudRay, let’s create a reusable Bash script to automate the deployment process. You need to follow these steps to create the script in CloudRay.

Screenshot for deployment script of Gatsby site
  1. Go to Scripts in your CloudRay project
  2. Click New Script
  3. Name: Deploy Gatsby Site. You can give it any name of your choice
  4. Copy this code:
#!/bin/bash

# =============================
# Gatsby Deployment Script
# =============================

set -e

# =============================
# 1. Install Dependencies
# =============================
sudo apt update && sudo apt install -y curl git nginx

# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

# Install PM2 and Serve
sudo npm install -g pm2 serve

# =============================
# 2. Clone the Repository
# =============================
cd /var/www
sudo git clone "{{repo_url}}" "{{app_name}}"
cd "/var/www/{{app_name}}"

# =============================
# 3. Install and Build Gatsby App
# =============================
npm install && npm run build

# =============================
# 4. Start with PM2
# =============================
pm2 start "serve -s public -l 3000" --name "{{app_name}}"
pm2 save
pm2 startup

# =============================
# 5. Configure Nginx
# =============================
sudo bash -c "cat > /etc/nginx/sites-available/{{app_name}}" <<EOL
server {
    listen 80;
    server_name {{domain_name}};

    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;
    }
}
EOL

# Enable Nginx site
sudo ln -sf "/etc/nginx/sites-available/{{app_name}}" "/etc/nginx/sites-enabled/{{app_name}}"
sudo nginx -t && sudo systemctl reload nginx

# =============================
# Done!
# =============================
echo "✅ Deployment complete. Visit: http://{{domain_name}}"

This script handles the entire process of installing dependencies, building your Gatsby project, and running it behind Nginx using PM2.

Define the Variable Group

To make this script reusable across multiple projects or servers, you’ll define a variable group inside CloudRay. These variables dynamically fill in the placeholders like {{repo_url}}, {{app_name}}, and {{domain_name}}. CloudRay processes all scripts as Liquid templates.

Screenshot of adding a new variable group

To ensure that these values are automatically substituted when the script runs, follow these steps to create a variable Group:

  1. Navigate to Variable Groups: In your CloudRay project, go to “Scripts” in the top menu and click on “Variable Groups”.
  2. Create a new Variable Group: Click on “Variable Group”.
  3. Add the following variables:
  • repo_url: The URL of your GitHub repository
  • app_name: The name of the app folder
  • domain_name: The domain pointing to the server

Since the variables is setup, proceed with running the scripts with CloudRay

Run the Deployment Script

CloudRay uses Runlogs to execute scripts on your servers while providing real-time logs of the execution process.

Once the script is saved, you can deploy your Gatsby site by creating a Runlog:

  1. Navigate to Runlogs: In your CloudRay project, go to the Runlogs section in the top menu.
  2. Create a New Runlog: Click on New Runlog.
  3. Configure the Runlog: Fill in the required details:
  • Server: Select the server you added earlier.
  • Script: Choose the “Deploy Gatsby Site”
  • Variable Group (optional): Select the variable group you created earlier.
Screenshot of creating a new runlog
  1. Execute the Script: Click on Run Now to start the execution.
Screenshot of the output of Deploy Gatsby script

CloudRay will automatically connect to your server, run the Deploy Gatsby Site, and provide live logs to track the process. If any errors occur, you can review the logs to troubleshoot the issue.

Once the deployment is complete, you can visit http://<your_domain>. You should see your Gatsby site up and running

Screenshot showing Gatsby Site running

Next Step

To improve your deployment and security:

  • You can use Let’s Encrypt to add HTTPS support
  • Set up automatic redeployment with webhooks or CI/CD

Summary

With CloudRay, deploying your Gatsby site to Ubuntu becomes a structured and repeatable process. Scripts remain organised, server access is centralised, and you can easily tweak configurations with variable groups.

For more deployment guides and use cases, check out our CloudRay Guides or explore the CloudRay Docs.