3
votes

I have a simple set of database migrations created within my Laravel 5 application, and they run nicely on my local development environment.

Now its time to run to run the migration on my new production server environment. I have configured the DB connection and deployed the app, and the app sees the database, but there are no tables - so migrations need to be run.

The following command, I believe, should run the migrations using the "production" environment, which is set up with the remote DB connection details:

php artisan --env=production migrate 

The migration works, but it runs on the local environment! Here is the environment file for my production environment(using amazon elastic beanstalk service) :

.elasticbeanstalk.env

APP_ENV=production
APP_DEBUG=true
APP_URL=<myappname.elasticbeanstalk.com>

DB_HOST=<myapp.amazonserveraddress.amazonaws.com:3306>
DB_DATABASE=<mydbname>
DB_USERNAME=<mydbusername>
DB_PASSWORD=<mydbpassword>

So either my environment file is not configured correctly, or artisan is not able to switch to that environment. I could change my .env file (local development environment, named "local") to connect to the remote, production DB, but I want to properly use Laravels environments.

What am I missing? Why does the migration always run on "local"?

Thanks.

1

1 Answers

6
votes

You can't run any remote commands on your local artisan. Anything you run there will only work locally (even if you set the ENV variable).

Setting the ENV variable is just to tell the application to behave as if it is in that environment. But doesn't tell artisan to use the remote production environment.

If you want to run commands on your production server, I suggest you look into Envoy. It is a completely standalone project (and does not have to be used only with Laravel projects) and is specifically for deployment.

It is basically a thin wrapper around SSHing into your remote server and then running commands. An example Envoy.blade.php file on my sites might look like this:

@servers(['web' => 'account@server'])

@task('deploy')
    cd ~/src

    php artisan down
    git pull origin master

    composer install --no-dev --no-progress --prefer-dist
    php artisan migrate --force --no-interaction
    php artisan optimize
    php artisan up
@endtask

This SSHes in, puts the application in maintenance mode, pulls the new code, does the various 'new code' setups like composer install, migrate, etc. and then pulls the application out of maintenance mode.