🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Astro Fundamentals••By Komal Nakrani

Astro Tutorial: Build and Deploy Your First Site

An Astro tutorial for beginners: create a project, add pages, a layout, a component and a Markdown blog, then build and deploy it to a static host for free.

#Astro#Astro Tutorial#Getting Started#Beginners

This Astro tutorial takes you from an empty folder to a deployed website in about an hour: you create a project with npm create astro@latest, add pages in src/pages/, share markup with a layout and a component, publish Markdown posts through a content collection, and deploy the dist/ folder to a free static host. Everything below runs on Astro 7 with Node.js 22.23.4 or newer.

You don’t need to know React or any other framework. If you can write HTML and a little JavaScript, you can follow every step.

Key Takeaways

  • npm create astro@latest scaffolds a project; npm run dev serves it at http://localhost:4321.
  • Every .astro or .md file in src/pages/ becomes a page with a matching URL.
  • Layouts and components are .astro files you import; <slot /> marks where children go.
  • Blog posts live in a content collection defined in src/content.config.ts.
  • npm run build writes plain HTML to dist/, which any static host can serve.

What you’ll build in this Astro tutorial

By the end you’ll have a small personal site with three parts:

  1. A home page and an about page that share a header and footer.
  2. A reusable Card component that takes props.
  3. A blog with a list page and one page per Markdown post, generated at build time.

That covers the core of Astro: file-based routing, components, layouts, content collections and static builds. Interactive islands, server rendering and integrations are add-ons you can learn once this base is solid. Our What is Astro guide explains the ideas behind each of these if you want the theory first.

Step 1: Install Node.js and create the project

Astro 7 requires Node.js 22.23.4 or later. Check your version:

# terminal
node -v

If it prints something lower than v22.23.4, install the current LTS from nodejs.org. Then create a project:

# terminal
npm create astro@latest my-first-astro-site

The wizard asks a few questions. For this tutorial, pick the minimal (empty) template, say yes to installing dependencies, and yes to initializing a git repository. Then start the dev server:

# terminal
cd my-first-astro-site
npm run dev

Open http://localhost:4321. You’ll see a nearly blank page. Leave the server running; it reloads the browser every time you save a file.

Step 2: Understand the files you just created

The minimal template gives you only a handful of files:

my-first-astro-site/
├── public/
│   └── favicon.svg
├── src/
│   └── pages/
│       └── index.astro
├── astro.config.mjs
├── package.json
└── tsconfig.json

src/pages/ is the one folder Astro treats specially: each file in it is a route. public/ holds files copied to the site as they are, like the favicon or robots.txt. astro.config.mjs is where integrations and site options go. Our guide to Astro project structure walks through every folder you’ll add later.

Open src/pages/index.astro. An Astro component has two parts: a frontmatter script between --- fences, which runs at build time, and an HTML template below it.

Step 3: Add pages

Replace the home page with something of your own:

---
// src/pages/index.astro
const name = 'Sam';
const skills = ['HTML', 'CSS', 'Astro'];
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{name}'s site</title>
  </head>
  <body>
    <h1>Hi, I'm {name}</h1>
    <ul>
      {skills.map((skill) => <li>{skill}</li>)}
    </ul>
    <a href="/about/">About me</a>
  </body>
</html>

Curly braces insert JavaScript values into the template, and .map() renders a list. This code runs once when the page builds; the browser receives plain HTML with no JavaScript.

Now create src/pages/about.astro with a heading and a paragraph. Visit http://localhost:4321/about/ and it’s there. No router configuration is needed, because the file path is the URL.

Step 4: Share markup with a layout

Copying the <html> and <head> into every page gets old fast. Move them into a layout:

---
// src/layouts/BaseLayout.astro
interface Props {
  title: string;
}
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{title}</title>
  </head>
  <body>
    <header>
      <a href="/">Home</a> · <a href="/about/">About</a> · <a href="/blog/">Blog</a>
    </header>
    <main>
      <slot />
    </main>
    <footer>© {new Date().getFullYear()}</footer>
  </body>
</html>

<style>
  body { font-family: system-ui, sans-serif; max-width: 42rem; margin: 0 auto; padding: 1rem; }
  header { display: flex; gap: 0.5rem; margin-bottom: 2rem; }
</style>

Astro.props holds the attributes passed to the component, and <slot /> is where the page’s own content is placed. The <style> block is scoped: these rules only apply to elements in this file.

Use it from both pages:

---
// src/pages/about.astro
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="About">
  <h1>About me</h1>
  <p>I build fast websites with Astro.</p>
</BaseLayout>

Step 5: Build a reusable component

Components work the same way as layouts. Here’s a card that takes props:

---
// src/components/Card.astro
interface Props {
  title: string;
  href: string;
  body: string;
}
const { title, href, body } = Astro.props;
---
<a class="card" href={href}>
  <h2>{title}</h2>
  <p>{body}</p>
</a>

<style>
  .card { display: block; padding: 1rem; border: 1px solid #ddd; border-radius: 8px; text-decoration: none; color: inherit; }
  .card:hover { border-color: #888; }
</style>

Import it in the home page and render it as many times as you like:

---
// src/pages/index.astro
import BaseLayout from '../layouts/BaseLayout.astro';
import Card from '../components/Card.astro';
---
<BaseLayout title="Home">
  <h1>Hi, I'm Sam</h1>
  <Card title="Read the blog" href="/blog/" body="Notes on what I'm learning." />
  <Card title="About me" href="/about/" body="Who I am and what I do." />
</BaseLayout>

Because the Props interface is typed, your editor warns you if you forget href or misspell a prop. Our guide to typing Astro props with TypeScript goes further.

Step 6: Add a blog with a content collection

Astro manages Markdown through content collections. First define the collection and its frontmatter schema:

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

const blog = defineCollection({
  loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
  schema: z.object({
    title: z.string(),
    description: z.string(),
    pubDate: z.coerce.date(),
  }),
});

export const collections = { blog };

Then write a post:

---
# src/content/blog/first-post.md
title: "My first post"
description: "What I learned building this site."
pubDate: 2026-11-02
---

This site is built with Astro. Every page is plain HTML.

If a post is missing title or has a date Astro can’t parse, the build fails and names the file. You catch the mistake locally instead of on the live site.

The blog index page

---
// src/pages/blog/index.astro
import { getCollection } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';

const posts = (await getCollection('blog')).sort(
  (a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
---
<BaseLayout title="Blog">
  <h1>Blog</h1>
  <ul>
    {posts.map((post) => (
      <li><a href={`/blog/${post.id}/`}>{post.data.title}</a></li>
    ))}
  </ul>
</BaseLayout>

One page per post

A file name in square brackets is a dynamic route. getStaticPaths() tells Astro which pages to generate:

---
// src/pages/blog/[id].astro
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';

export async function getStaticPaths() {
  const posts = await getCollection('blog');
  return posts.map((post) => ({ params: { id: post.id }, props: { post } }));
}

const { post } = Astro.props;
const { Content } = await render(post);
---
<BaseLayout title={post.data.title}>
  <h1>{post.data.title}</h1>
  <p>{post.data.pubDate.toLocaleDateString('en-US')}</p>
  <Content />
</BaseLayout>

Visit /blog/first-post/. Add a second Markdown file and a second page appears without touching any code. The Content Collections guide covers references, images in frontmatter and custom loaders.

Step 7: Build the site

Stop the dev server and run:

# terminal
npm run build
npm run preview

astro build renders every page to HTML and writes it, with CSS and assets, to dist/. astro preview serves that folder locally so you can check the real output before deploying. Open the page source in your browser: it’s ordinary HTML with no framework runtime attached.

Before deploying, set your production URL so canonical links and sitemaps can build absolute URLs:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://www.example.com',
});

Step 8: Deploy your Astro site

Because the output is static, you don’t need a server or an adapter. Push the project to a GitHub repository, then connect it to a host:

Host Build command Output folder
Cloudflare Pages npm run build dist
Netlify npm run build dist
Vercel npm run build dist
GitHub Pages via the official withastro/action dist

Each host rebuilds and redeploys on every push to your main branch. Our Cloudflare Pages deploy docs show the exact screens, and the Astro deployment guide compares hosts and covers custom domains.

Where to go after this Astro tutorial

You now know the parts every Astro site is built from. Good next steps, in order:

  • Add interactivity with a framework component and a client:* directive. Start with Astro islands architecture.
  • Add MDX with npx astro add mdx so posts can include components.
  • Optimize images with the <Image /> component from astro:assets.
  • Learn the CLI with the Astro CLI commands cheatsheet.

The official Astro installation guide lists every option of the setup wizard. And if you’d rather study a finished project, every Komal theme is a complete Astro 7 site you can run locally and take apart; you can browse Astro themes by site type.

FAQ

Is Astro good for beginners?

Yes. Astro components are mostly HTML with a small script block on top, so anyone who knows HTML and basic JavaScript can be productive in an afternoon. You don’t need a UI framework, state management or a build configuration to make a full site.

How long does it take to learn Astro?

You can build and deploy a simple multi-page site in a day by following a tutorial like this one. Content collections, dynamic routes and islands take a few more days of practice. Server rendering and adapters are only needed for dynamic features and can wait.

Do I need to know React to use Astro?

No. Astro has its own component format and ships no JavaScript by default. You can add React, Vue, Svelte or Solid components later if you need interactive widgets, but a complete site can be built with .astro files only.

What do I need installed to start an Astro project?

Node.js 22.23.4 or newer, npm (which comes with Node.js), a code editor and a terminal. The official Astro extension for VS Code adds syntax highlighting and type checking for .astro files.

Is it free to host an Astro site?

For most static sites, yes. Cloudflare Pages, Netlify, Vercel and GitHub Pages all have free tiers that serve the dist/ folder Astro builds, with HTTPS and automatic deploys from GitHub.