Deploy Laravel application using Vercel
Dec 11, 2024Deploying 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.
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": "vercel-php@0.3.1" }
},
"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 thevercel-phpruntime (check for the latest version).routes: Redirects all URIs to theapi/index.phpfunction.env: Sets environment variables. Note that sensitive information likeAPP_KEYshould be added through Vercel's environment variable settings, not directly in this file. The cache settings are pointed to/tmpbecause 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
-
Git Integration: Push your project (including the newly created files) to a GitHub repository.
-
Vercel Login: Install the Vercel CLI (
npm i -g vercel) and log in usingvercel login. -
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.
-
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. -
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
mainbranch will trigger automatic redeployments.
Troubleshooting
The most common error is "No Output Directory named 'dist' found". This can be resolved by:
- Creating a
distfolder: Create an emptydistfolder in your project's root directory and add a.gitkeepfile 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
apiorpublic, depending on your configuration). - Vercel PHP Runtime Version: Ensure you are using a compatible and up-to-date version of the
vercel-phpruntime.
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.
Exploring the Landscape of AI Web Browsing Frameworks
Published Jan 24, 2025
Explore the landscape of AI web browsing frameworks, from browser-integrated assistants to dedicated automation platforms. Learn how these tools are transforming the web experience with intelligent content extraction, task automation, and user-friendly interfaces....
OpenAI Operator: A New Era of AI Agentic Task Automation
Published Jan 23, 2025
Explore OpenAI Operator, a groundbreaking AI agent automating tasks by interacting with computer interfaces. Discover its capabilities, limitations, and impact on the future of AI....
React OpenGraph Image Generation: Techniques and Best Practices
Published Jan 15, 2025
Learn how to generate dynamic Open Graph (OG) images using React for improved social media engagement. Explore techniques like browser automation, server-side rendering, and serverless functions....