Ankit Sinha

Navigating Laravel's Journey with Vercel and PlanetScale

Published onNovember 22, 2023
4Reactions
6min read
In the ever-expanding cosmos of development tools, Laravel stands out as my trusted comet, swiftly cutting through the vastness of code. However, for my smaller projects, I sought a deployment solution that was not just cost-effective but also a breeze to set up
Here is my step-by-step guide on how to deploy Laravel applications on Vercel with a PlanetScale database (MySql):
  1. Create a new Laravel application.
  2. Configure the application for Vercel.
  3. Set up a PlanetScale database.
  4. Configure vervel with Environment Variables
  5. Automate the deployment process using Github Actions.
Now, let's sprinkle some creativity into your step-by-step guide:

🌌 Step 1: Creating a Laravel Constellation

We require an application for deployment. Let's swiftly conjure a new Laravel + Jetstream project—a constellation of code and creativity.
Next, we'll set up a local database using Docker Compose, update our environment variables, and run our migrations.
Next, We need to update our
file in our project
Now, Once we had completed this, lets boot up db and migrate our base laravel tables,
Now Create a project in Github and Push this project excluding your env files to Github, as we will be using github action to automatic deploy this application in vercel.

🚀 Step 2: Configuring the Vercel

Before deploying our application to Vercel, we need to make some changes,
  1. Set up a new Vercel project
  2. Create a vercel.json file
  3. Add our api/index.php entry point
  4. Set up our trusted proxies
  5. Ensure the vendor directory is not uploaded

Step 1. Setup a new Vecel project

We'll use the Vercel CLI to create a new project, which we plan to deploy later.
Follow the step-by-step instructions provided by vercel login and vercel link. This will generate a
directory and automatically include it in your
. Open the
file and take note of the orgId and projectId as we'll need them later.

Step 2. Create a vercel.json file

Do change runtime if using another version of PhP:
Let’s quickly explain some of the key concepts here.

Our application is essentially a single serverless function, which we'll create next. We use the community vercel-php runtime to set up the environment and automatically install dependencies with Composer.

Vercel builds our application by running npm run build, but by default, it looks for a dist directory. However, our assets are actually saved in public/build, so we need to instruct Vercel where to find them.
If you're using a tool other than Livewire, you may need to adjust this setting accordingly.

The first route intercepts requests for CSS and JS assets and directs them to the appropriate files. All other requests are managed by our Laravel serverless function, as specified by the second route.

"env": { "APP_ENV": "production", "APP_DEBUG": "true", "APP_URL": "YOUR APP URL",
} Here, we establish our non-secret environment variables. Take note of the
variable. This is crucial for a secure connection to PlanetScale in the following steps. We will set up our secret environment variables in the Vercel dashboard later on.

🎭 Step 3. Add our
entry point

This simple file redirects to the default index.php file provided by Laravel. This is necessary because Vercel only allows functions to be located in the api directory.

🛡️ Step 4. Set up our trusted proxies

Vercel hosts our code behind a load balancer, which forwards requests to port
. This can confuse Laravel when it generates secure links. To solve this issue, we need to make a simple modification to
.

🚫 Step 5. Ensure the vendor directory is not uploaded

Lastly, we need to add a
. This will prevent our vendor (created during composer installation) and any pre-built files from being included, as Vercel will build these for us.

🪐 Step 3. Set up a PlanetScale database.

Visit PlanetScale and create an account if you haven't already. From the dashboard, create a new database.
Once your database is operational, click on "Connect". Generate a new password and record the configuration details.

🌐 Step 4. Configure vervel with Environment Variables

Now we'll add those configuration details to Vercel. This will allow our deployed application to access the database. Go to the dashboard and add the following environment variables:
  • which can be generated by running
  • From PlanetScale Database Dashboard
  • From PlanetScale Database Dashboard
  • From PlanetScale Database Dashboard
  • From PlanetScale Database Dashboard

🚀 Step 5. Automated deployment with Github Actions

Now, we can integrate everything with Github Actions. We will set up a workflow that automatically applies migrations to PlanetScale and deploys our application to Vercel.
To start, add the following
file:
Next, add the following secrets to your Github dashboard:
From PlanetScale:
From .vercel/project.json:
From Vercel settings page:
Now, with secrets in place, every push to the main branch triggers a symphony of actions—installing dependencies, running migrations, and deploying our Laravel creation to Vercel.
Finally, push everything to Github.
Let’s take a closer look at what this workflow is doing:
Your code will be checked out.
  • A PHP environment will be set up, giving you access to php and composer.
  • The Vercel CLI will be installed.
  • Your application dependencies will be installed.
  • Migrations will be run with
    .
  • Your application will be deployed to Vercel.
You should now have a deployed and functioning application!
Tags:
#laravel
#vercel
#webdev
#programming