Laravel Automations

serversideup/php has a "Laravel Automations" script that helps automate common tasks to maintain your Laravel application and improve it's performance. By default, the script is DISABLED. We only recommend enabling this script in production environments.

What the script does

Environment VariableDefaultDescription
AUTORUN_ENABLEDfalseEnables the Laravel Automations script.
ℹ️ Note: This must be set to true for the script to run.
AUTORUN_DEBUGfalseEnables a special debug mode, specifically for the Laravel Automations script.
AUTORUN_LARAVEL_CONFIG_CACHEtruephp artisan config:cache: Caches the configuration files into a single file.
AUTORUN_LARAVEL_EVENT_CACHEtruephp artisan event:cache: Creates a manifest of all your application's events and listeners.
AUTORUN_LARAVEL_MIGRATIONtruephp artisan migrate: Runs migrations.
AUTORUN_LARAVEL_MIGRATION_ISOLATIONfalseRun your migrations with the --isolated flag.
ℹ️ Note: Requires Laravel v9.38.0+
AUTORUN_LARAVEL_MIGRATION_TIMEOUT30Number of seconds to wait for database connection before timing out during migrations.
AUTORUN_LARAVEL_OPTIMIZEtruephp artisan optimize: Optimizes the application.
AUTORUN_LARAVEL_SEEDfalsephp artisan db:seed: Runs the default seeder.
ℹ️ Note: Set to true to run the default seeder. If you want to run a custom seeder, set this to the name of the seeder class.
AUTORUN_LARAVEL_ROUTE_CACHEtruephp artisan route:cache: Caches the routes.
AUTORUN_LARAVEL_STORAGE_LINKtruephp artisan storage:link: Creates a symbolic link from public/storage to storage/app/public.
AUTORUN_LARAVEL_VIEW_CACHEtruephp artisan view:cache: Caches the views.

Database Connection Checks

Before running migrations, the script performs database connection checks to ensure the database is ready. It will:

  • Clear the Laravel configuration cache before attempting migrations
  • Try to connect to the database for up to AUTORUN_LARAVEL_MIGRATION_TIMEOUT seconds (default: 30)
  • Show detailed connection attempts when AUTORUN_DEBUG is enabled

Creates a symbolic link from public/storage to storage/app/public.

Read more about storage links →

php artisan migrate

Before running migrations, we ensure the database is online and ready to accept connections. By default, we will wait 30 seconds before timing out.

You can enable the --isolated flag by setting AUTORUN_LARAVEL_MIGRATION_ISOLATION to true, which will ensure no other containers are running a migration.

Special Notes for Isolated Migrations:

  • Requires Laravel v9.38.0+
  • Your application must be using the memcached, redis, dynamodb, database, file, or array cache driver as your application's default cache driver. In addition, all servers must be communicating with the same central cache server.

Read more about migrations →

php artisan optimize

Laravel comes with an artisan command called optimize, which will optimize the application by caching the configuration, routes, views, and events all in one command.

You can disable any cache features by setting the corresponding environment variable to false (for example, AUTORUN_LARAVEL_CONFIG_CACHE would disable configuration caching).

If your application is running Laravel v11.38.0 or higher, we will utilize the optimize --except parameter to exclude any cache features you have disabled. Otherwise, we will run the individual optimizations separately.

It's possible to disable the optimize command by setting AUTORUN_LARAVEL_OPTIMIZE to false, but the major advantage of using the optimize command is other dependencies may hook into this action and run other commands.

Read more about optimizing Laravel →

php artisan config:cache

This command caches all configuration files into a single file, which can then be quickly loaded by Laravel. Once the configuration is cache, the .env file will no longer be loaded.

Read more about configuration caching →

php artisan db:seed

This command runs the default seeder. If you want to run a custom seeder, set AUTORUN_LARAVEL_SEED to the name of the seeder class.

Read more about seeding →

php artisan route:cache

This command caches the routes, dramatically decrease the time it takes to register all of your application's routes. After running this command, your cached routes file will be loaded on every request.

Read more about route caching →

php artisan view:cache

This command caches all of the views in your application, which can greatly decrease the time it takes to render your views.

Read more about view caching →

php artisan event:cache

This command creates a manifest of all your application's events and listeners, which can greatly speed up the process of registering them with Laravel.

Read more about event caching →

Debugging the AUTORUN script

It's very important to understand the nature of how containerized environments work when debugging the AUTORUN script. In some cases, some users may become frustrated when they push an update but their changes are never deployed.

In most cases, this is due to a bug in their application code that causes a migration or some other process to fail.

If you are experiencing issues, you can enable the AUTORUN_DEBUG environment variable to get more detailed ouput of what could be going wrong.

If you need even more information, you can set LOG_OUTPUT_LEVEL to debug to get A TON of output of what's exactly happening.

Preventing issues with the AUTORUN script

  • Ensure you are running the latest version of serversideup/php
  • Ensure you have dependencies installed before calling this script
  • Use automated testing to catch issues before deploying