leerob/next-self-host
leerob/next-self-host has 1,520 GitHub stars and 203 forks. GitHot measured +1 stars in 24 hours. Last checked 2026-11-06 07:19 UTC. Last push 2026-01-31.
An example deploying Next / Postgres / Nginx to a Ubuntu Linux server.
What you can do
- Image Optimization
- Talking to a Postgres database
- Incremental Static Regeneration
- Reading environment variables
Measured star growth
- in 24h
- +1
- in 7 days
- Update delayed
- in 15 days
- Update delayed
- in 1 month
- Update delayed
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalNext.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.
Prerequisites
- Purchase a domain name
- Purchase a Linux Ubuntu server (e.g. droplet)
- Create an
ADNS record pointing to your server IPv4 address
Quickstart
SSH into your server:
ssh root@your_server_ipDownload the deployment script:
curl -o ~/deploy.sh https://raw.githubusercontent.com/leerob/next-self-host/main/deploy.shYou can then modify the email and domain name variables inside of the script to use your own.
Run the deployment script:
chmod +x ~/deploy.sh ./deploy.sh
Supported Features
This demo tries to showcase many different Next.js features.
- Image Optimization
- Streaming
- Talking to a Postgres database
- Caching
- Incremental Static Regeneration
- Reading environment variables
- Using Middleware
- Running code on server startup
- A cron that hits a Route Handler
Deploy Script
I've included a Bash script which does the following:
- Installs all the necessary packages for your server
- Installs Docker, Docker Compose, and Nginx
- Clones this repository
- Generates an SSL certificate
- Builds your Next.js application from the Dockerfile
- Sets up Nginx and configures HTTPS and rate limting
- Sets up a cron which clears the database every 10m
- Creates a
.envfile with your Postgres database creds
Once the deployment completes, your Next.js app will be available at:
http://your-provided-domain.com
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()
);'
For pushing subsequent updates, I also provided an update.sh script as an example.
Running Locally
If you want to run this setup locally using Docker, you can follow these steps:
docker-compose up -d
This will start both services and make your Next.js app available at http://localhost:3000 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 containersdocker-compose logs web– view Next.js output logsdocker-compose logs cron– view cron logsdocker-compose down- shut down the Docker containersdocker-compose up -d- start containers in the backgroundsudo systemctl restart nginx- restart nginxdocker exec -it myapp-web-1 sh- enter Next.js Docker containerdocker exec -it myapp-db-1 psql -U myuser -d mydatabase- enter Postgres db
Other Resources
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
560,474 starspublic-apis/public-apis
A collective list of free APIs
496,784 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
438,379 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,084 stars