# Build a Boilerplate for an Elixir Phoenix Web App with LiveView
🚀 Overview
This guide creates a minimal, production-ready boilerplate for a Phoenix 1.7+ web app using Phoenix LiveView. It includes:
- LiveView for interactive UIs (no/minimal JS).
- Tailwind CSS for styling.
- Ecto with PostgreSQL (optional; skip if not needed).
- Basic authentication skeleton (using Phx.Gen.Auth).
- Hot-reload development setup.
- Docker support for easy deployment.
The result is a single-page app scaffold with a counter LiveView demo, user auth, and a dashboard.
📋 Prerequisites
- Elixir 1.15+ & Erlang/OTP 26+ (install via asdf or official).
- Node.js 18+ (for esbuild/Tailwind).
- PostgreSQL (optional; for DB).
- InEx (optional; for Docker).
Verify:
elixir -v
node -v
🛠️ Step 1: Install Phoenix
# Install Hex + Phoenix generators
mix archive.install hex phx_new
# Verify
mix phx.new --version # Should be 1.7.12+
🛠️ Step 2: Generate the Base Project
# Create new app with LiveView + DB (skip --database postgres if no DB needed)
mix phx.new my_app --live --database postgres
# cd in
cd my_app
# Install JS deps
cd assets && npm install && cd ..
# Create & migrate DB (skip if no DB)
mix ecto.create
mix ecto.migrate
Key Generated Files (customize these):
my_app/
├── lib/
│ ├── my_app/
│ │ ├── application.ex # App supervisor
│ │ ├── my_app_web/
│ │ │ ├── live/ # LiveView modules
│ │ │ │ └── page_live.ex # Default counter demo
│ │ │ ├── router.ex # Routes
│ │ │ ├── components.ex # Reusable LiveComponents
│ │ │ └── endpoint.ex # Cowboy/HTTP setup
│ └── my_app_web.ex # Phoenix app module
├── config/ # Dev/test/prod configs
├── assets/ # Tailwind + esbuild
├── priv/
│ └── repo/migrations/ # DB migrations
├── test/
└── mix.exs # Dependencies
Customize mix.exs (add these deps for boilerplate)
# In mix.exs deps(), add/replace:
defp deps do
[
{:phoenix, "~> 1.7.12"},
{:phoenix_ecto, "~> 4.5"},
{:ecto_sql, "~> 3.10"},
{:postgrex, ">= 0.0.0"},
{:phoenix_live_view, "~> 0.20.2"},
{:floki, ">= 0.30.0", only: :test},
{:phoenix_html, "~> 4.1"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_dashboard, "~> 0.8.2"},
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},
{:heroicons, "~> 0.5"},
# Auth boilerplate
{:phx_gen_auth, "~> 0.9", only: [:dev], runtime: false},
# Optional: Pow for auth (lighter than GenAuth)
# {:pow, "~> 1.0.1"}
]
end
Then:
mix deps.get
🛠️ Step 3: Add Authentication Boilerplate
# Generate Auth with LiveView
mix phx.gen.auth Accounts User users
# Migrate
mix ecto.migrate
This adds:
- User registration/login (LiveView-based).
- Session management.
- Routes:
/users/register, /users/log_in, etc.
Update router.ex to protect routes:
# lib/my_app_web/router.ex
defmodule MyAppWeb.Router do
use MyAppWeb, :router
pipeline :browser do
# ...
plug :fetch_session
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :fetch_current_user # From GenAuth
end
pipeline :require_authenticated_user do
scope "/", MyAppWeb do
pipe_through [:browser, :require_authenticated_user]
live "/dashboard", DashboardLive, :index # Protected
end
end
# Public
scope "/", MyAppWeb do
pipe_through :browser
live "/", PageLive, :index
live "/counter", CounterLive, :index # Example
end
end
🛠️ Step 4: Add a Dashboard LiveView Example
Create lib/my_app_web/live/dashboard_live.ex:
defmodule MyAppWeb.DashboardLive do
use MyAppWeb, :live_view
def mount(_params, _session, socket) do
{:ok, assign(socket, counter: 0, users: [])} # Fetch real data here
end
def handle_event("inc", _params, socket) do
{:noreply, update(socket, :counter, &(&1 + 1))}
end
def render(assigns) do
~H"""
<div class="container mx-auto p-8">
<h1 class="text-3xl font-bold mb-4">Dashboard</h1>
<div class="bg-blue-100 p-6 rounded-lg">
<p>Counter: <%= @counter %></p>
<button phx-click="inc" class="bg-blue-500 text-white px-4 py-2 rounded">
Increment
</button>
</div>
</div>
"""
end
end
🛠️ Step 5: Tailwind & Assets Setup
Already included. Customize assets/tailwind.config.js for themes.
Compile assets:
mix tailwind.default --watch # Dev
mix assets.deploy # Prod
🏃♂️ Step 6: Run the App
# Start servers
mix phx.server
# Visit:
# - http://localhost:4000 # Home (counter)
# - http://localhost:4000/counter
# - http://localhost:4000/users/register
# - http://localhost:4000/dashboard (after login)
Dev Tools:
📦 Production Deployment (Docker)
Add Dockerfile:
FROM elixir:1.15-alpine AS builder
# ... (standard Phoenix Docker; see official guide)
FROM debian:bookworm-slim
# ... (runtime)
docker-compose.yml for DB + app:
services:
db:
image: postgres:15
app:
build: .
ports: ["4000:4000"]
depends_on: [db]
Build/Run:
docker-compose up --build
🔧 Customization Tips
- No DB? Regenerate without
--database or remove Ecto deps.
- Add Pow Auth (simpler):
mix pow.install.
- Testing:
mix test (includes LiveView tests).
- ES6/JS: Edit
assets/app.js.
- Hooks: Add
phx-hook in LiveView for JS interop.
- Deploy: Fly.io (
fly launch), Render, or Gigalixir (Elixir-native).
📚 Next Steps
This boilerplate is ready in <5 mins and scales to production. Ping for tweaks! 🕵️♂️