Skip to content

Kai-Animator/next-self-host

 
 

Repository files navigation

Next.js Self Hosting Example

This repo shows how to deploy a Next.js app and a PostgreSQL database on a Ubuntu Linux server using Docker and Nginx. It showcases using several features of Next.js like caching, ISR, environment variables, and more.

🗄️ Original Project

Prerequisites

  1. Purchase a domain name
  2. Purchase a Linux Ubuntu server (e.g. droplet)
  3. Create an A DNS record pointing to your server IPv4 address
  4. New Github repository

Quickstart

  1. SSH into your server:

    ssh root@your_server_ip
  2. Download the deployment script:

    curl -o ~/deploy.sh https://raw.githubusercontent.com/Kai-Animator/next-self-host/refs/heads/main/deploy.sh

    You can then modify the email and domain name variables inside of the script to use your own.

  3. Run the deployment script:

    chmod +x ~/deploy.sh
    ./deploy.sh
    
  4. During script copy generated SSH .pub key to your GH settings and press enter after addition

  5. Into your new repo, add the following secrets: SERVER_USER SERVER_HOST SSH_PRIVATE_KEY AAP_DIR

Deploy Script

Both the Next.js app and PostgreSQL database will be up and running in Docker containers. To set up your database, you could install npm inside your Postgres container and use the Drizzle scripts, or you can use psql:

docker exec -it myapp-db-1 sh
apk add --no-cache postgresql-client
psql -U myuser -d mydatabase -c '
CREATE TABLE IF NOT EXISTS "todos" (
  "id" serial PRIMARY KEY NOT NULL,
  "content" varchar(255) NOT NULL,
  "completed" boolean DEFAULT false,
  "created_at" timestamp DEFAULT now()
);'

Running Locally

If you want to run this setup locally using Docker, you can follow these steps:

docker-compose -f docker-compose.yml up -d web-dev db-dev

This will start both services and make your Next.js app available at http://localhost:3002 with the PostgreSQL database running in the background. We also create a network so that our two containers can communicate with each other.

If you want to view the contents of the local database, you can use Drizzle Studio:

bun run db:studio

Helpful Commands

  • docker-compose ps – check status of Docker containers
  • docker-compose logs web – view Next.js output logs
  • docker-compose logs – view cron logs
  • docker-compose down - shut down the Docker containers
  • docker-compose up -d - start containers in the background
  • docker exec -it myapp-web-1 sh - enter Next.js Docker container (depends on env)
  • docker exec -it myapp-db-1 psql -U myuser -d mydatabase - enter Postgres db (depends on env)

Personal Changes

Replace Nginx for Caddy, added fail2ban Added Github Actions integrations Added zod, trpc and postcss, also updated next

About

An example deploying Next / Postgres / Caddy to a Ubuntu Linux server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 80.3%
  • TypeScript 18.1%
  • JavaScript 1.3%
  • CSS 0.3%