Hosting a Laravel API on vercel

Summary

Hey all,

I’m looking to host a Laravel API on Vercel as I already have other projects with you guys it makes no sense to use Vapor or Forge and then be paying double.

I’m currently running into an issue where I have to go to /api/api/* to hit an API endpoint. My Laravel application is purely an API so it would make sense for it to go to /api/*.

I’ve currently got the following vercel.json:

{
  "version": 2,
  "framework": null,
  "functions": {
    "api/index.php": { "runtime": "vercel-php@0.7.1" }
  },
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/api/index.php"
    }
  ],
  "env": {
    "APP_ENV": "production",
    "APP_DEBUG": "true",
    "APP_URL": "https://<placeholder>.vercel.app",

    "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"
  }
}

I thought I’d be able to get around it by doing something such as adding a rewrite combined with a routes, but that doesn’t seem possible.

{
  "rewrites": [
    {
      "source": "/api/:path*",
      "destination": "/:path*"
    }
  ],
}

Any ideas how I can achieve my goal?

Thanks in advance.

Example

No response

Steps to Reproduce

N/A


Edit

Resolved by updating apiPrefix in Laravel.

Hi @dmb!

It looks like you’re on the right track with the rewrites config. I would reverse it though since you’re trying to take out the redundant /api from the path.

I did something similar with my Express.js example.

Hey, thanks for responding.

The solution I found was in Laravel rather than Vercel’s config.

I updated the default apiPrefix argument when Laravel is booting from /api to /. This seems to work so far.

Unfortunately I’m coming across a new issue when attempting to install Sentry:

[21:08:17.201] Warning: Failed loading Zend extension 'opcache' (tried: /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/opcache (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/opcache: cannot open shared object file: No such file or directory), /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/opcache.so (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/opcache.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
[21:08:17.201] 
[21:08:17.202] Warning: PHP Startup: Unable to load dynamic library 'sockets' (tried: /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/sockets (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/sockets: cannot open shared object file: No such file or directory), /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/sockets.so (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/sockets.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
[21:08:17.202] 
[21:08:17.202] Warning: PHP Startup: Unable to load dynamic library 'runkit7' (tried: /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/runkit7 (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/runkit7: cannot open shared object file: No such file or directory), /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/runkit7.so (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/runkit7.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
[21:08:17.202] 
[21:08:17.202] Warning: PHP Startup: Unable to load dynamic library 'mongodb' (tried: /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/mongodb (/vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/mongodb: cannot open shared object file: No such file or directory), /vercel/path0/.vercel/builders/node_modules/@libphp/amazon-linux-2-v83/native/php/modules/mongodb.so (libutf8proc.so.1: cannot open shared object file: No such file or directory)) in Unknown on line 0
[21:08:17.305] [14.2MiB/0.05s] Installing dependencies from lock file
[21:08:17.307] [14.4MiB/0.05s] Verifying lock file contents can be installed on current platform.
[21:08:17.312] [14.7MiB/0.06s] Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. It is recommended that you run `composer update` or `composer update <package name>`.
[21:08:17.315] [15.1MiB/0.06s] - Required package "sentry/sentry" is not present in the lock file.
[21:08:17.316] [15.1MiB/0.06s] This usually happens when composer files are incorrectly merged or the composer.json file is manually edited.
[21:08:17.316] [15.1MiB/0.06s] Read more about correctly resolving merge conflicts https://getcomposer.org/doc/articles/resolving-merge-conflicts.md
[21:08:17.316] [15.1MiB/0.06s] and prefer using the "require" command over editing the composer.json file directly https://getcomposer.org/doc/03-cli.md#require-r
[21:08:17.316] [15.0MiB/0.06s] Memory usage: 15.01MiB (peak: 16.2MiB), time: 0.06s
[21:08:17.325] Error: Exited with 4
[21:08:17.325]     at ChildProcess.<anonymous> (/vercel/path0/.vercel/builders/node_modules/vercel-php/dist/utils.js:182:24)
[21:08:17.326]     at ChildProcess.emit (node:events:518:28)
[21:08:17.326]     at ChildProcess.emit (node:domain:488:12)
[21:08:17.326]     at ChildProcess._handle.onexit (node:internal/child_process:294:12)
[21:08:17.356] Error: Command "vercel build" exited with 1
[21:08:17.423] Command "vercel build" exited with 1

Seems like the runtime is struggling to dynamically load the extensions needed for Sentry. I’m going to open a ticket in the relevant repository to try and find help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.