Deploying Laravel application with Vercel

Posted July 25, 2021 by Ivan Magdić ‐ 3 min read

In this post we'll look into Vercel, vercel-php package and how to deploy Laravel application using GitHub integration.

Laravel application

We’ll start by creating new Laravel application with CLI:

laravel new laravel-vercel

See Laravel Installer on how to install.

Check my Laravel demo application that I will use in this post: Laravel vercel demo. You can also use existing project, but you’ll need to make few changes to get it working.

We won’t use database for this demo, but you can easily define the configuration variables to point to your database that is hosted on your server or AWS.

With Laravel application scaffolded we are ready to create configuration files for Vercel.

Vercel and vercel-php

Since we’ll be using vercel-php which is PHP runtime for Vercel platform, we need two files:

  • index.php
  • vercel.json

First file index.php has to be in a folder called api, and vercel.json has to be in project root directory like this:

laravel-vercel(project)
├── api
│   └── index.php
└── vercel.json

After creating index.php file we will write the following into file:

<?php

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

This is an entry point file for our application.

In second file vercel.json we want to have following configuration:

{
    "version": 2,
    "builds": [
        { "src": "/api/index.php", "use": "[email protected]" },
        { "src": "/public/**", "use": "@vercel/static" }
    ],
    "routes": [
        {
            "src": "/(css|js|images)/(.*)",
            "dest": "public/$1/$2"
        },
        {
            "src": "/(.*)",
            "dest": "/api/index.php"
        }
    ],
    "env": {
        "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"
    }
}

builds

As mentioned we’ll be using vercel-php that takes care for our Laravel application. We’ll also use vercel-static package which will help us with static content such as css, js and image files.

Note: Put your favicon.ico and other images into public/images folder

routes

In routes section we define which endpoints point where. First one takes care of our static files that are located in public folder, and second one forwards all URIs to application entry point.

env

Here we define environment variables we want to use. Since this is a serverless application, the only folder we can modify at runtime is tmp folder. So that’s why we point all cache variables to /tmp folder.

We can also set other typical Laravel variables such as:

APP_NAME=
APP_ENV=
APP_KEY=
APP_DEBUG=
APP_URL=

but I like to define it in Vercel environment variables tab under Project Settings on their web application.

Vercel environment variables GUI

Deployment

After we are done with configuration we need to deploy the application to Vercel. We’ll use GitHub which will help us with continuous delivery each time we push to master branch.

So, create GitHub repository, add the files to git, commit the files and push them.

Note: Don't worry about composer. vercel-php handles that on each deployment.

Next, we’ll visit Vercel and login.

Import project

After a successful login we need to import GitHub Repository.

Vercel import Git Repository

Next, paste the URL to you Git Repository.

Vercel import Git Repository

On the next few steps, we’ll be asked to select root directory of our project and project name. For selecting root directory, press continue and for naming project you can rename it if you want.

Vercel import Git Repository

After that, press deploy and we are done. Rest is up to Vercel.

Vercel import Git Repository

If you’ve used my Laravel project you should see application live:

Vercel import Git Repository

Conclusion

With the help of vercel-php we are able to host our Laravel application serverless.

Check out related posts for more information: