Skip to content
Advertisement

How can I run php artisan serve using .env.production?

I have a Laravel Web Application, and it works just fine locally, using a local .env file that references the local database.

I have the same Laravel Web Application deployed in production, where I find a .env, which is different from the one that I use locally.

Both the scenarios work perfectly, but when I wanted to perform a test with the remote database (that I can access from my local IP address), I copied the remote .env and renamed it .env.production.

How can I run the php artisan serve using the .env.production ?

The php artisan serve help states that adding a --env parameter should make the trick, as you can see from the command result below

php artisan serve --help
Description:
  Serve the application on the PHP development server

Usage:
  serve [options]

Options:
      --host[=HOST]     The host address to serve the application on [default: "127.0.0.1"]
      --port[=PORT]     The port to serve the application on
      --tries[=TRIES]   The max number of ports to attempt to serve from [default: 10]
      --no-reload       Do not reload the development server on .env file changes
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

but the command php artisan serve --env=production still loads the local database.

What am I doing wrong ?

Advertisement

Answer

After some tests I found a working solution in a Laracast Forum, giving credits to a Laravel.io post that consists into running the following command:

APP_ENV=production php artisan serve

This causes the .env.production to be loaded and used by the local server, as needed.

I post this here hoping it will be useful to someone in my same condition.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement