One-Click Installer for Laravel, automating the setup of environment files, database migrations, seeding, file permissions, and caching. By running php artisan install:one-click, you can ensure that all necessary steps to get your Laravel application running are completed with just one command!
Steps:
Create the Command
Define the handle() Method
Set Permissions and Run Migrations
Automate Configuration
Run Artisan Commands for key tasks (such as migrations, caching, etc.)
Create the Command
In Laravel, you can create a custom Artisan command using the artisan make:command command.
php artisan make:command OneClickInstaller
This will create a new file at
app/Console/Commands/OneClickInstaller.php.
Step 2: Define the Handle Method
Now, open the generated file (app/Console/Commands/OneClickInstaller.php) and modify the handle method to include the tasks you want to automate for the one-click installation.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
class OneClickInstaller extends Command
{
// Command signature for Artisan CLI
protected $signature = 'install:one-click';
// Description of what the command does
protected $description = 'One-click installer for setting up the Laravel application';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info('Starting the One-Click Installer...');
// Step 1: Set the environment variables (Create .env file)
$this->createEnvFile();
// Step 2: Generate Application Key
$this->info('Generating application key...');
Artisan::call('key:generate');
// Step 3: Run migrations to setup the database
$this->info('Running migrations...');
Artisan::call('migrate --force'); // '--force' is required to run migrations in production
// Step 4: Seed the database (if you have seeders)
$this->info('Seeding database...');
Artisan::call('db:seed --force'); // '--force' to run in production
// Step 5: Install necessary packages
$this->info('Installing packages...');
Artisan::call('vendor:publish --provider="Laravel\Passport\PassportServiceProvider"');
// Step 6: Set proper permissions (if needed)
$this->info('Setting file permissions...');
$this->setFilePermissions();
// Step 7: Cache config and routes
$this->info('Caching configuration...');
Artisan::call('config:cache');
Artisan::call('route:cache');
Artisan::call('view:cache');
// Optional: Clear any caches
$this->info('Clearing caches...');
Artisan::call('cache:clear');
$this->info('One-Click Installer completed!');
}
// Create .env file from the .env.example
protected function createEnvFile()
{
$this->info('Creating .env file...');
if (File::exists(base_path('.env'))) {
$this->info('.env file already exists!');
} else {
File::copy(base_path('.env.example'), base_path('.env'));
$this->info('.env file created successfully!');
}
}
// Set the appropriate file permissions
protected function setFilePermissions()
{
$this->info('Setting file permissions...');
// Change permission for Laravel directories: storage, bootstrap/cache
$this->runCommand('chmod -R 775 ' . storage_path());
$this->runCommand('chmod -R 775 ' . bootstrap_path('cache'));
}
// Helper function to run commands
protected function runCommand($command)
{
$this->info("Running: $command");
$output = shell_exec($command);
$this->info($output);
}
}
Step 3: Run Migrations & Setup Database
In the code above, we've already handled:
Generating Application Key (key:generate)
Running Migrations (migrate --force)
Seeding Database (db:seed --force)
Step 4: Set Permissions
In some cases, you need to set proper file and directory permissions (especially in Linux/Unix environments). This ensures that the necessary directories like storage and bootstrap/cache are writable by the web server.
The method setFilePermissions() in the code sets appropriate permissions.
Step 5: Run Artisan Commands for Caching
It’s also common to cache the configuration, routes, and views in production environments. The command caches them for faster loading:
Artisan::call('config:cache');
Artisan::call('route:cache');
Artisan::call('view:cache');
Step 6: Optional - Clear Cache
After installing, you may want to clear caches for configurations, routes, and views:
Artisan::call('cache:clear');
Step 7: Register the Command
To make this command available, you need to register it in the app/Console/Kernel.php file.
// Inside app/Console/Kernel.php
protected $commands = [
\App\Console\Commands\OneClickInstaller::class,
];
Step 8: Run the Command
Once you've added the OneClickInstaller command, you can run the one-click installer using the following Artisan command:
php artisan install:one-click
Real-Life One-Click Installer Example for Laravel
In this example, we'll create a command that does the following:
Generate the application key.
Publish configuration files.
Run database migrations.
Run database seeders.
Set file permissions.
Clear and cache config, routes, and views.
Install necessary Laravel packages.
Clear application cache.
Top comments (0)