Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel Passport cannot find or read its oauth key files

ErrorKey path oauth-private.key does not exist or is not readable

Short answer

Passport signs tokens with a key pair and cannot start without it. The keys are normally kept out of source control, so a fresh server has none. Run php artisan passport:keys on that server. If your keys live elsewhere, point Passport at them with Passport::loadKeysFrom, or load them from PASSPORT_PRIVATE_KEY and PASSPORT_PUBLIC_KEY.

  • Applies to Laravel 12, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the path in the message, then look for the key files on the machine that threw the error. This tells you whether they are simply absent.

    find . -name "oauth-*.key" -not -path "./vendor/*"
  2. 2Match what you see, then jump to its fix

    1. 1find lists no oauth-private.key and no oauth-public.key; typical right after a first deploy or a fresh cloneThe key pair was never generated on this serverFix
    2. 2The keys exist in one folder, but the error names a different oneThe app was told to read the keys from another folderFix
    3. 3No key files anywhere, because the project stores the keys in the environment, and the variables are missing on this serverThe keys are meant to come from environment variables, but those are not setFix

What does the error look like?

  • Key path oauth-private.key does not exist or is not readable
  • Key path oauth-public.key does not exist or is not readable

Cause 1 of 3

The key pair was never generated on this server

1Check

The Passport docs say the generated keys are not typically kept in source control, so every new server has to create its own. Look in the folder named in the error.

git status --ignored --short | grep oauth

2Fix

Run the key command once on the server, as part of the first deployment.

php artisan passport:keys

3Verify

Both files now exist, and the request that failed no longer raises the key error.

find . -name "oauth-*.key" -not -path "./vendor/*"

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

The app was told to read the keys from another folder

1Check

Search the app for a Passport::loadKeysFrom call. If one exists, Passport looks only in that folder, so keys left in another folder are ignored.

grep -rn "loadKeysFrom" app/ bootstrap/ config/

2Fix

Either create the keys in the folder named in loadKeysFrom, or fix the path in the AppServiceProvider boot method so it points at where the keys really are.

// app/Providers/AppServiceProvider.php
public function boot(): void
{
    Passport::loadKeysFrom(__DIR__.'/../secrets/oauth');
}

3Verify

The folder you passed to loadKeysFrom contains both key files.

ls -l secrets/oauth/

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The keys are meant to come from environment variables, but those are not set

1Check

After publishing the Passport config file, the keys can be supplied through PASSPORT_PRIVATE_KEY and PASSPORT_PUBLIC_KEY. Confirm the config is published and the two variables exist.

ls config/passport.php
grep -c "PASSPORT_" .env

2Fix

Publish the config if it is missing, then put the full PEM text of both keys into the server's environment.

php artisan vendor:publish --tag=passport-config

# .env
PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
<private key here>
-----END RSA PRIVATE KEY-----"
PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
<public key here>
-----END PUBLIC KEY-----"

3Verify

Retry the token request that failed.

php artisan config:show passport

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Add php artisan passport:keys to the first-deploy checklist for every new server.
  • Keep the private key out of git, and store it in your secret manager if servers are rebuilt often.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.