Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Render #23

@attogram

Description

@attogram

Guide: Deploying a Laravel Site on Render.com (Free Tier)

This guide provides a complete walkthrough for deploying a Laravel application, including a PostgreSQL database, on Render's free tier. We will use Render's "Blueprint" feature (render.yaml), which allows you to define and manage your entire application infrastructure as code.


Understanding Render's Free Tier

Before starting, be aware of the free tier's features and limitations:

  • Free Web Service: The application is hosted in a container with a shared CPU and 512MB RAM.
  • Free PostgreSQL Database: You get a 1GB PostgreSQL database. Note: Free databases on Render are automatically deleted after 30 days. This makes the free tier suitable for demos, learning, and temporary staging environments.
  • Spinning Down: The web service will "spin down" after 15 minutes of inactivity. The next request will restart it, causing an initial load delay of 20-30 seconds.
  • Automatic Git Deploys: Connect a GitHub or GitLab repository for seamless, automatic deployments on every git push.

Step 1: Prepare Your Laravel Application

Make these adjustments to your Laravel project to ensure it runs correctly on Render's proxy-based infrastructure.

1. Configure Trusted Proxies

Render uses a load balancer, so you must configure Laravel to trust the proxy.

In app/Http/Middleware/TrustProxies.php, update the $proxies property:

// app/Http/Middleware/TrustProxies.php

namespace App\Http\Middleware;

use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string|null
     */
    protected $proxies = '*'; // Important: Change this

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB;
}

2. Ensure a Health Check Route Exists

Render needs a lightweight endpoint to check if your application is running. Laravel 11+ includes a default /up route. If you are on an older version, add a simple route in routes/web.php:

// routes/web.php
use Illuminate\Support\Facades\Route;

Route::get('/ping', function () {
    return 'pong';
});

Step 2: Create the render.yaml Blueprint File

This is the core of the deployment setup. In the root directory of your project, create a new file named render.yaml. This file instructs Render on how to build and run your entire application stack.

Copy the following configuration into your render.yaml file.

# render.yaml

services:
  # The Laravel Web Service
  - type: web
    name: my-laravel-app # Change this to your app's name
    runtime: php
    plan: free # Specifies the free instance type
    buildCommand: |
      composer install --no-dev --optimize-autoloader
      php artisan config:cache
      php artisan route:cache
      php artisan view:cache
      php artisan migrate --force
    startCommand: php artisan serve --host 0.0.0.0 --port $PORT
    healthCheckPath: /up # Use /ping for older Laravel versions
    envVars:
      - key: APP_URL
        value: https://${RENDER_EXTERNAL_URL}
      - key: APP_KEY
        generateValue: true # Render will generate a secure key for you
      - key: APP_ENV
        value: production
      - key: DB_CONNECTION
        value: pgsql
      - key: DB_HOST
        fromService:
          type: psql
          name: my-laravel-db # Must match the database name below
          property: host
      - key: DB_PORT
        fromService:
          type: psql
          name: my-laravel-db
          property: port
      - key: DB_DATABASE
        fromService:
          type: psql
          name: my-laravel-db
          property: database
      - key: DB_USERNAME
        fromService:
          type: psql
          name: my-laravel-db
          property: user
      - key: DB_PASSWORD
        fromService:
          type: psql
          name: my-laravel-db
          property: password

  # The PostgreSQL Database
  - type: psql
    name: my-laravel-db # Change this to your database name
    plan: free # Specifies the free database type
    # Note: Free tier databases are deleted after 30 days.
    # To keep this database, you must upgrade to a paid plan.

Important: Ensure the name of the psql service (my-laravel-db in the example) exactly matches the name used in the fromService keys under envVars.


Step 3: Deploy on Render

  1. Commit and Push Your Changes
    Add the render.yaml file and your code modifications to your Git repository.

    git add .
    git commit -m "feat: Configure for Render deployment"
    git push origin main
  2. Create the Blueprint on Render

    • Go to your Render Dashboard.
    • Click New + and select Blueprint.
    • Connect your GitHub/GitLab account and choose the repository you just pushed to.
    • Render will automatically detect and parse render.yaml. It will display the services (web service and database) it plans to create.
    • Review the plan and click Apply.

Render will now provision your database and deploy your application. You can monitor the build and deployment logs in real-time. Once complete, your site will be live at the URL provided in the dashboard, typically https://your-app-name.onrender.com.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions