Skip to main content

Introduction

Deploying a fullstack Laravel Breeze API + Next.js application requires careful consideration of both backend and frontend infrastructure. This guide provides an overview of deployment strategies, hosting options, and production requirements.

Deployment Architecture

Your application consists of two separate components that must be deployed independently:
  • Backend (Laravel API): Requires PHP 8.2+ hosting with database support
  • Frontend (Next.js): Requires Node.js hosting or static hosting depending on rendering strategy

Communication Flow

Hosting Options

Backend Hosting

Traditional VPS

Full control over server configuration. Ideal for complex requirements.
  • DigitalOcean, Linode, Vultr
  • AWS EC2, Google Compute Engine
  • Requires server management skills

Platform as a Service

Managed PHP hosting with automatic scaling and maintenance.
  • Laravel Forge + DigitalOcean
  • Heroku (with PHP buildpack)
  • Platform.sh, Laravel Cloud

Serverless

Pay-per-request pricing with automatic scaling.
  • AWS Lambda (via Bref)
  • Vapor (Laravel’s serverless platform)
  • Google Cloud Run

Shared Hosting

Budget-friendly option for small applications.
  • cPanel hosting with PHP 8.2+
  • Limited control and resources
  • Not recommended for production

Frontend Hosting

Vercel

Optimized for Next.js with zero configuration.
  • Automatic deployments from Git
  • Global CDN and edge functions
  • Free tier available

Netlify

Static and SSR hosting with simple setup.
  • Git-based deployments
  • Form handling and functions
  • Free tier available

Custom Server

Full control with Node.js hosting.
  • VPS with PM2 or Docker
  • Requires server management
  • More configuration needed

Static Hosting

If using static export only.
  • AWS S3 + CloudFront
  • Cloudflare Pages
  • GitHub Pages (with limitations)

Production Requirements

Backend Requirements

  • PHP 8.2 or higher
  • Composer 2.x
  • MySQL 8.0+ or PostgreSQL 13+
  • Redis (optional, for caching and queues)
  • Supervisor (for queue workers)
  • Cron access (for scheduled tasks)
  • BCMath
  • Ctype
  • cURL
  • DOM
  • Fileinfo
  • JSON
  • Mbstring
  • OpenSSL
  • PCRE
  • PDO
  • Tokenizer
  • XML
  • Nginx (recommended) or Apache
  • SSL certificate (Let’s Encrypt)
  • HTTP/2 support
  • Gzip/Brotli compression

Frontend Requirements

  • Node.js 18+ (LTS recommended)
  • npm, pnpm, or yarn
  • Environment variables configured
  • HTTPS enabled (required for Sanctum cookies)

HTTPS Requirements

HTTPS is mandatory for production. Laravel Sanctum requires secure cookies for authentication, which only work over HTTPS connections.

Why HTTPS is Required

  1. Laravel Sanctum Security: Sanctum uses secure, httpOnly cookies for authentication
  2. Cookie Transmission: Browsers block secure cookies over HTTP
  3. API Communication: Cross-origin requests require secure connections
  4. Modern Standards: HTTPS is a baseline security requirement

SSL Certificate Options

  • Let’s Encrypt: Free, automated SSL certificates
  • Cloudflare: Free SSL with CDN and DDoS protection
  • Commercial Certificates: Wildcard and extended validation options

Production Checklist

Before deploying to production, ensure you’ve completed these tasks:

Backend Checklist

1

Environment Configuration

  • Set APP_ENV=production
  • Set APP_DEBUG=false
  • Generate strong APP_KEY
  • Configure database credentials
  • Set proper APP_URL and FRONTEND_URL
2

Security Hardening

  • Review CORS configuration
  • Configure trusted hosts
  • Set secure session settings
  • Enable CSRF protection
  • Configure rate limiting
3

Performance Optimization

  • Run composer install --optimize-autoloader --no-dev
  • Cache configuration: php artisan config:cache
  • Cache routes: php artisan route:cache
  • Cache views: php artisan view:cache
  • Enable OPcache
4

Database Setup

  • Run migrations: php artisan migrate --force
  • Seed production data if needed
  • Configure database backups
  • Set up read replicas if needed
5

Queue and Cron

  • Configure queue workers with Supervisor
  • Set up cron job for scheduler
  • Test queue processing
  • Monitor queue failures
6

Logging and Monitoring

  • Configure log channels
  • Set up error tracking (Sentry, Bugsnag)
  • Enable application monitoring
  • Configure log rotation

Frontend Checklist

1

Environment Configuration

  • Set NEXT_PUBLIC_BACKEND_URL to production API URL
  • Configure any additional environment variables
  • Remove development-only variables
2

Build Configuration

  • Choose rendering strategy (SSR vs Static Export)
  • Configure output settings in next.config.js
  • Optimize images and assets
  • Enable production optimizations
3

Security

  • Review API endpoint configurations
  • Ensure no sensitive data in client code
  • Configure Content Security Policy
  • Enable security headers
4

Performance

  • Run production build: npm run build
  • Test build locally: npm start
  • Analyze bundle size
  • Configure CDN if needed

Deployment Strategies

Deploy backend and frontend independently:
  • Backend: Deploy to PHP hosting (Forge, VPS, etc.)
  • Frontend: Deploy to Vercel, Netlify, or custom Node.js server
  • Advantages: Better separation of concerns, independent scaling, easier rollbacks

2. Monolithic Deployment

Deploy both on the same server:
  • Backend serves API at /api
  • Frontend served by Node.js or built as static files
  • Advantages: Simpler infrastructure, single deployment
  • Disadvantages: Harder to scale, more complex configuration

3. Containerized Deployment

Use Docker for both services:
  • Separate containers for Laravel and Next.js
  • Orchestrate with Docker Compose or Kubernetes
  • Advantages: Consistent environments, easy scaling, portability

Domain Configuration

api.yourdomain.com  → Laravel Backend
app.yourdomain.com  → Next.js Frontend

Alternative Setup

yourdomain.com      → Next.js Frontend
yourdomain.com/api  → Laravel Backend (via reverse proxy)

Environment Variables

Ensure these critical environment variables are set:

Backend (.env)

APP_ENV=production
APP_DEBUG=false
APP_URL=https://api.yourdomain.com
FRONTEND_URL=https://app.yourdomain.com

DB_CONNECTION=mysql
DB_HOST=your-db-host
DB_DATABASE=your-db-name
DB_USERNAME=your-db-user
DB_PASSWORD=your-secure-password

SESSION_DOMAIN=.yourdomain.com
SANCTUM_STATEFUL_DOMAINS=app.yourdomain.com

Frontend (.env.production)

NEXT_PUBLIC_BACKEND_URL=https://api.yourdomain.com

Post-Deployment Testing

After deployment, verify:
  • API health check endpoint responds
  • Database connection works
  • Authentication endpoints functional
  • Queue workers processing jobs
  • Scheduled tasks running
  • SSL certificate valid
  • CORS headers present

Next Steps

Backend Deployment

Deploy your Laravel API to production

Frontend Deployment

Deploy your Next.js application

Environment Configuration

Configure production environment variables

Backend Testing

Learn about testing your Laravel backend

Build docs developers (and LLMs) love