Deploy Laravel application using Vercel
Snehal Rajeev Moon

Snehal Rajeev Moon @snehalkadwe

About: Full Stack Developer by profession, mostly works in Laravel | Vue JS | PHP | Javascript | React | Node | Express.

Location:
India
Joined:
Jul 23, 2021

Deploy Laravel application using Vercel

Publish Date: Jun 25 '24
72 3

Hello Artisans,

In this blog post, we will see how to deploy the laravel application on Vercel. It is a popular serverless platform.

Step 1: First, we create a new laravel application.

composer create-project laravel/laravel laravel-vercel-project
Enter fullscreen mode Exit fullscreen mode

Step 2: Create api folder, within that folder create index.php file, and add the code below.

<?php  
require __DIR__ . "/../public/index.php";
Enter fullscreen mode Exit fullscreen mode

It is an entry point that will forward the control to public/index.php file which normally gets called when we visit our laravel application.

Step 3: Create .vercelignore file and add the below line in it.

/vendor
Enter fullscreen mode Exit fullscreen mode

Step 4: Now create vercel.json file and add the below code

{
    "version": 2,
    "framework": null,
    "functions": {
        "api/index.php": { "runtime": "vercel-php@0.6.0" }
    },
    "routes": [
        {
            "src": "/(.*)",
            "dest": "/api/index.php"
        }
    ],
    "env": {
        "APP_ENV": "production",
        "APP_DEBUG": "true",
        "APP_URL": "https://your.url.from.vercel.app",
        "APP_KEY": add API key here from your .env file",

        "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"
    }
}
Enter fullscreen mode Exit fullscreen mode
  • In the above code routes array will forward all incoming URIs to our newly set serverless function which we created in Step 2.
"routes": [{
    "src": "/(.*)",
    "dest": "/api/index.php"
}],
Enter fullscreen mode Exit fullscreen mode
  • env will be our Vercel's env file.

Step 4: The last step is to create dist folder in the project directory.

  • Now save all the files and push your repository to GitHub. Now login to your Vercel application.
  • On the right side click Add New then select Project option. After that import your laravel application from GitHub.

  • Now we will configure our project by giving a name to the project.
  • Then select the other option from Framework Preset.

  • The next step is to add the required env.

If your deployment has failed go to the home page select the project then go to settings

  • In the General section go to Node.js version section and select the node version as 18x, save the changes, and trigger to re-deploy the application.

  • Tada!! Your application is live now.

Happy Reading
🦄 ❤️

Comments 3 total

  • rhagilsetiawan
    rhagilsetiawanOct 5, 2024

    "The next step is to add the required env."

    what is the require env? does i need to pul all .env in my laravel project😭

    • Snehal Rajeev Moon
      Snehal Rajeev MoonOct 7, 2024

      Hello @rhagilsetiawan,

      The required env variables are the ones you need to connect your third-party applications or for the API calls you plan to make. You don't need to pull the entire .env file. Please ensure that the necessary environment variables, such as API keys, client IDs, secrets, and URLs, are properly set up in your .env for the specific service you are integrating, if you have a database, then you need to add all required DB connection details from your .env.

    • Bruno Silva
      Bruno SilvaOct 21, 2024

      No, only the variables related to the database or keys that are necessary. In my case, only the database variables (online, in this case) were necessary.

Add comment