DEV Community

Cover image for 🚀 Deploy Your Astro Static Site on Railway
Gersom Hernandez Ramos
Gersom Hernandez Ramos

Posted on

🚀 Deploy Your Astro Static Site on Railway

Before we begin, note that there are two ways to deploy an Astro site on Railway:

  1. If your website uses SSR (Server-Side Rendering) or adapters.
  2. If your website is completely static.

This post focuses on the second option: deploying a fully static Astro site on Railway.

1. Initialize Your Astro Project

First, create your Astro project. Open your terminal and run:

npm create astro@latest

Follow the prompts to set up a static site. In my case, I'm building a personal portfolio to showcase my projects.

You can fork or contribute to my repo here: https://github.com/Gersomsim/Pesonal-web

2. Prepare Your Site for Deployment

Once your site is ready, modify your package.json:

"scripts": {
  "dev": "astro dev",
  "build": "astro build",
  "preview": "astro preview",
  "start": "npx serve dist" // Crucial for static Astro sites
}
Enter fullscreen mode Exit fullscreen mode

Then add this at the end of package.json:

"engines": {
  "node": ">=18.0.0" // Tells Railway the minimum Node.js version
}
Enter fullscreen mode Exit fullscreen mode

3. Configuration Files

3.1 astro.config.mjs

Projects scaffolded with Astro default to static output. Just confirm:

import { defineConfig } from 'astro/config';

export default defineConfig({
  // output: "static" (default value)
});
Enter fullscreen mode Exit fullscreen mode

Uncomment the output line if unsure, but it’s static by default.

3.2 Create Railway’s Config File (railway.json)

Add this to instruct Railway’s builder:

{
  "builds": [
    {
      "src": "package.json",
      "use": "@railway/nixpacks"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

3.3 Optional: static.json (Static Server Config)

Create this file to configure static file serving:

{
  "root": "dist",
  "clean_urls": true,
  "routes": {
    "**": "index.html"
  }
}
Enter fullscreen mode Exit fullscreen mode

Ready to Deploy!

With these steps complete, your static site is prepped for Railway.

Deployment is straightforward:

1.- Log into Railway with your GitHub account
2.- Grant repository access
3.- Select your repo
4.- Railway handles the rest automatically

Why Railway?

You might wonder: "Why Railway over Vercel/Netlify?" While alternatives exist, I chose Railway for its $5/month Hobby Plan. This lets me host:

  • Frontend projects
  • Backend APIs (Laravel, NestJS, etc.)
  • My entire portfolio

For the price of a coffee, I avoid juggling multiple services.

Thanks for reading my first post! 🚀

Cheers!

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.