Deploy Laravel application using Vercel

Dec 11, 2024

Deploying your Laravel application to Vercel offers a streamlined, serverless solution for hosting your projects. This guide consolidates information from several resources to provide a complete walkthrough.

Deploy Laravel application using Vercel

Deploying a Laravel Application to Vercel: A Comprehensive Guide

Deploying your Laravel application to Vercel offers a streamlined, serverless solution for hosting your projects. This guide consolidates information from several resources to provide a complete walkthrough.

Setting Up Your Laravel Application

Before deploying, ensure you have a Laravel application ready. If you don't, create a new one using Composer:

composer create-project laravel/laravel laravel-vercel-project

(Refer to the Laravel Installer for detailed installation instructions.)

You can use an existing project, but you might need to make adjustments for compatibility. For this guide, we'll assume you're not using a database, but database integration is possible by configuring environment variables to point to your database (hosted externally on a service like AWS).

Vercel Configuration Files

To deploy to Vercel, you need to create three files: api/index.php, vercel.json, and .vercelignore.

api/index.php

Create a new index.php file inside a new api folder in your Laravel project's root directory. Add the following code:

<?php

// Forward Vercel requests to normal index.php
require __DIR__ . '/../public/index.php';

This acts as an entry point, forwarding requests to Laravel's standard public/index.php. Vercel requires the entry point to reside within the api directory.

.vercelignore

Create a .vercelignore file in your project's root directory and add:

/vendor

This prevents uploading the entire vendor directory, optimizing deployment size. Vercel will handle installing Composer dependencies.

vercel.json

Create a vercel.json file in the root directory. The configuration will vary slightly depending on the source, but a common configuration looks like this:

{
  "version": 2,
  "functions": {
    "api/index.php": { "runtime": "[email protected]" }
  },
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/api/index.php"
    }
  ],
  "env": {
    "APP_ENV": "production",
    "APP_DEBUG": "true",
    "APP_URL": "https://yourproductionurl.com",
    "APP_CONFIG_CACHE": "/tmp/config.php",
    "APP_EVENTS_CACHE": "/tmp/events.php",
    "APP_PACKAGES_CACHE": "/tmp/packages.php",
    "APP_ROUTES_CACHE": "/tmp/routes.php",
    "APP_SERVICES_CACHE": "/tmp/services.php",
    "VIEW_COMPILED_PATH": "/tmp",
    "CACHE_DRIVER": "array",
    "LOG_CHANNEL": "stderr",
    "SESSION_DRIVER": "cookie"
  }
}
  • version: Specifies Vercel version 2.
  • functions: Defines your Laravel app as a single serverless function, using the vercel-php runtime (check for the latest version).
  • routes: Redirects all URIs to the api/index.php function.
  • env: Sets environment variables. Note that sensitive information like APP_KEY should be added through Vercel's environment variable settings, not directly in this file. The cache settings are pointed to /tmp because serverless functions are stateless.

Alternative vercel.json configurations exist: Some examples include using builds instead of functions and specifying static asset handling. Refer to the provided articles for variations.

Deployment to Vercel

  1. Git Integration: Push your project (including the newly created files) to a GitHub repository.
  2. Vercel Login: Install the Vercel CLI (npm i -g vercel) and log in using vercel login.
  3. Import Project: In your Vercel dashboard, import your GitHub repository. Vercel will guide you through the process, prompting you to select the project root and name.
  4. Environment Variables: After importing, navigate to your project's settings in Vercel. Add your sensitive environment variables (like APP_KEY, database credentials, etc.) securely through the Vercel interface.
  5. Deployment: Vercel will automatically build and deploy your application. You can then access it via the URL provided by Vercel. Future pushes to your GitHub repository's main branch will trigger automatic redeployments.
Vercel Deployment Screenshot Deployed Laravel App Screenshot Vercel Deployment Success Screenshot

Troubleshooting

The most common error is "No Output Directory named 'dist' found". This can be resolved by:

  • Creating a dist folder: Create an empty dist folder in your project's root directory and add a .gitkeep file inside it. This allows Git to track the empty directory.
  • Vercel Output Directory Setting: In Vercel's project settings, ensure the "Output Directory" is correctly set (often to api or public, depending on your configuration).
  • Vercel PHP Runtime Version: Ensure you are using a compatible and up-to-date version of the vercel-php runtime.

This guide provides a general approach. Refer to the original articles for more detailed explanations and potential variations in the configuration steps. Remember to always prioritize secure handling of sensitive information like API keys and database credentials.

Recent Posts