How to use php in vercel
- User
How to use php in vercel
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
How to use php in vercel
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You can run PHP on Vercel using serverless functions, but it requires a specific setup since Vercel doesn't natively support traditional PHP hosting. Here's how to do it:
Vercel supports PHP as serverless functions in the /api directory.
my-php-app/
├── api/
│ ├── index.php
│ └── hello.php
├── public/
│ └── index.html
└── vercel.json
Create files in the /api folder:
api/index.php
<?php
header('Content-Type: application/json');
echo json_encode([
'message' => 'Hello from PHP on Vercel!',
'timestamp' => date('Y-m-d H:i:s')
]);
?>
api/hello.php
<?php
$name = $_GET['name'] ?? 'World';
echo "Hello, " . htmlspecialchars($name) . "!";
?>
vercel.json
{
"functions": {
"api/*.php": {
"runtime": "vercel-php@0.6.0"
}
}
}
# Install Vercel CLI
npm i -g vercel
# Deploy
vercel
# Or deploy directly
vercel --prod
Your PHP functions will be available at:
https://your-app.vercel.app/api/indexhttps://your-app.vercel.app/api/hello?name=JohnFor full PHP applications (like Laravel, Symfony), use Bref:
composer require bref/bref bref/web-adapter
index.php
<?php
require_once 'vendor/autoload.php';
// Your PHP application code
echo "Hello from PHP on Vercel with Bref!";
?>
vercel.json
{
"functions": {
"index.php": {
"runtime": "vercel-php@0.6.0"
}
},
"routes": [
{ "src": "/(.*)", "dest": "/index.php" }
]
}
Convert your PHP to static HTML during build:
build.php
<?php
// Generate static HTML from PHP
ob_start();
include 'dynamic-content.php';
$content = ob_get_clean();
file_put_contents('public/index.html', $content);
echo "Static site generated!";
?>
vercel.json
{
"buildCommand": "php build.php",
"outputDirectory": "public"
}
What Works:
What Doesn't Work:
api/contact.php
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$name = $input['name'] ?? '';
$email = $input['email'] ?? '';
$message = $input['message'] ?? '';
// Process form (send email, save to database, etc.)
echo json_encode([
'success' => true,
'message' => 'Form submitted successfully!'
]);
} else {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
}
?>
api/index.php
<?php
phpinfo();
?>
vercel.json
{
"functions": {
"api/*.php": {
"runtime": "vercel-php@0.6.0"
}
}
}
vercel
This approach works best for APIs, microservices, or simple PHP scripts rather than full traditional PHP applications. For complex PHP apps, consider using traditional PHP hosting platforms like the ones I mentioned earlier.