Installation &amp; setup | laravel-dynamic-servers | Spatie       

 SPATIE  

  Laravel Dynamic Servers 
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-dynamic-servers](https://spatie.be/docs/laravel-dynamic-servers/v1)  Installation &amp; setup

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-dynamic-servers/v1) 

  Installation &amp; setup    
- [ Introduction ](https://spatie.be/docs/laravel-dynamic-servers/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-dynamic-servers/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-dynamic-servers/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-dynamic-servers/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-dynamic-servers/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-dynamic-servers/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-dynamic-servers/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/getting-started)
- [ Configuring your first server type ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/configuring-your-first-server-type)
- [ Ensuring a number of servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/ensuring-a-number-of-servers)
- [ Rebooting servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/rebooting-servers)
- [ Using multiple server types ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/using-multiple-server-types)
- [ Manually increasing and decreasing servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/manually-increasing-and-decreasing-servers)

Advanced usage
--------------

- [ Creating your own server provider ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/creating-your-own-server-provider)
- [ How servers are managed under the hood ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/how-servers-are-managed-under-the-hood)
- [ Dealing with erroring servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/dealing-with-erroring-servers)
- [ Dealing with hanging servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/dealing-with-hanging-servers)
- [ Fired Events ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/fired-events)
- [ Overriding actions and jobs ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/overriding-actions)
- [ Setting a server limit ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/setting-a-server-limit)

 Installation &amp; setup
========================

###  On this page 

1. [ Run the installer ](#content-run-the-installer)
2. [ Migrating the database ](#content-migrating-the-database)
3. [ Configuring queues ](#content-configuring-queues)
4. [ Scheduling commands ](#content-scheduling-commands)

You can install the package via composer:

```
composer require spatie/laravel-dynamic-servers
```

Run the installer
-----------------------------------------------------------------------------------------------------------

Next, you can run the installer with:

```
php artisan dynamic-servers:install
```

This command will:

- publish the migration that will create a `dynamic_servers` table in your database. This table is used to keep track of all servers created by the package.
- create the `dynamic-servers.php` config file in the `/config` directory of your

This is how the config file looks like (there are more install instructions after the contents of the config file).

```
<?php

return [
    'providers' => [
        'up_cloud' => [
            'class' => Spatie\DynamicServers\ServerProviders\UpCloud\UpCloudServerProvider::class,
            'maximum_servers_in_account' => 20,
            'options' => [
                'username' => env('UP_CLOUD_USER_NAME'),
                'password' => env('UP_CLOUD_PASSWORD'),
                'disk_image' => env('UP_CLOUD_DISK_IMAGE_UUID'),
            ],
        ],
    ],

    /*
     * Overriding these actions will give you fine-grained control over
     * how we handle your servers. In most cases, it's fine to use
     * the defaults.
     */
    'actions' => [
        'generate_server_name' => Spatie\DynamicServers\Actions\GenerateServerNameAction::class,
        'start_server' => Spatie\DynamicServers\Actions\StartServerAction::class,
        'stop_server' => Spatie\DynamicServers\Actions\StopServerAction::class,
        'find_servers_to_stop' => Spatie\DynamicServers\Actions\FindServersToStopAction::class,
        'reboot_server' => Spatie\DynamicServers\Actions\RebootServerAction::class,
    ],

    /*
     * Overriding these jobs will give you fine-grained control over
     * how we create, stop, delete and reboot your servers. In most cases,
     * it's fine to use the defaults.
     */
    'jobs' => [
        'create_server' => Spatie\DynamicServers\Jobs\CreateServerJob::class,
        'verify_server_started' => Spatie\DynamicServers\Jobs\VerifyServerStartedJob::class,
        'stop_server' => Spatie\DynamicServers\Jobs\StopServerJob::class,
        'verify_server_stopped' => Spatie\DynamicServers\Jobs\VerifyServerStoppedJob::class,
        'delete_server' => Spatie\DynamicServers\Jobs\DeleteServerJob::class,
        'verify_server_deleted' => Spatie\DynamicServers\Jobs\VerifyServerDeletedJob::class,
        'reboot_server' => Spatie\DynamicServers\Jobs\RebootServerJob::class,
        'verify_server_rebooted' => Spatie\DynamicServers\Jobs\VerifyServerRebootedJob::class,
    ],

    /*
     * When we detect that a server is taking longer than this amount of minutes
     * to start or stop, we'll mark it has hanging, and will not try to use it anymore
     *
     * The `ServerHangingEvent` will be fired, that you can use to send yourself a notification,
     * or manually take the necessary actions to start/stop it.
     */
    'mark_server_as_hanging_after_minutes' => 10,

    /*
     * The dynamic_servers table holds records of all dynamic servers.
     *
     * Using Laravel's prune command all stopped servers will be deleted
     * after the given amount of days.
     */
    'prune_stopped_servers_from_local_db_after_days' => 7,

    'throw_exception_when_hitting_maximum_server_limit' => false,
];
```

Migrating the database
--------------------------------------------------------------------------------------------------------------------------

This package will keep track of all dynamic servers in the `dynamic_servers` table. To create that table, run these commands:

```
php artisan migrate
```

Configuring queues
--------------------------------------------------------------------------------------------------------------

This package uses queued jobs to start and stop servers. Make sure, you have configured [one of the available queuing mechanisms](https://laravel.com/docs/master/queues) in your Laravel app.

Scheduling commands
-----------------------------------------------------------------------------------------------------------------

You should register a couple of commands in your kernel schedule.

The `MonitorDynamicServersCommand` command will take care of creating and destroying servers.

The `HandleHangingServersCommand` command will detect any servers that are starting and stopping, but never did start or stop completely.

To clean up records of stopped servers in the `dynamic_servers` table, you should add Laravel's `model:prune` command. If you already have this command in your schedule, add the `\Spatie\DynamicServers\Models\Server::class` model to its options.

You should add the commands to your schedule, and let them run every minute.

```
// in app/Console/Kernel.php
use Spatie\DynamicServers\Commands\MonitorDynamicServersCommand;
use Spatie\DynamicServers\Commands\DetectHangingServersCommand;
use Spatie\DynamicServers\Models\Server;

protected function schedule(Schedule $schedule)
{
    $schedule->command(MonitorDynamicServersCommand::class)->everyMinute();
    $schedule->command(DetectHangingServersCommand::class)->everyMinute();
    $schedule->command('model:prune', [
        '--model' => [Server::class],
    ])->daily();
}
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
