How to Create a Runtime Interactive Laravel Setup Script

 Setting up a Laravel project manually can be time-consuming. A runtime interactive Laravel setup script can automate the entire process by prompting the user for necessary inputs and configuring the project accordingly. This guide will walk you through creating a script that does the following:


How to Create a Runtime Interactive Laravel Setup Script


✅ Accepts user inputs for Git repository, database name, database user, and password
✅ Clones the Laravel project from the provided Git repository
✅ Installs dependencies (Composer & NPM)
✅ Dynamically sets up the .env file
✅ Runs database migrations & seeds
✅ Configures storage, permissions, cron jobs, and queue workers
✅ Starts the Laravel development server

Step 1: Creating the Setup Script

Create a new Bash script file setup.sh in the root directory of your project:






#!/bin/bash

# Welcome Message
echo "🚀 Laravel One-Click Setup Script"

# Ask for User Inputs
read -p "📥 Enter Git Repository URL: " REPO_URL
read -p "📂 Enter Project Directory Name: " PROJECT_DIR
read -p "📊 Enter Database Name: " DB_NAME
read -p "👤 Enter Database Username: " DB_USER
read -sp "🔑 Enter Database Password: " DB_PASSWORD
echo ""

PHP_VERSION="php" # Adjust for your PHP version (e.g., php8.1)
USER=$(whoami)

# Step 1: Clone Repository
if [ -d "$PROJECT_DIR" ]; then
    echo "⚠️ Directory $PROJECT_DIR already exists. Skipping clone..."
else
    echo "📥 Cloning repository..."
    git clone $REPO_URL $PROJECT_DIR
fi

# Move into project directory
cd $PROJECT_DIR || exit

# Step 2: Install Dependencies
echo "📦 Installing Composer dependencies..."
composer install --no-interaction --prefer-dist

echo "📦 Installing NPM dependencies..."
npm install && npm run build

# Step 3: Set Up .env File
if [ ! -f .env ]; then
    echo "📝 Creating .env file..."
    cp .env.example .env
else
    echo "✅ .env file already exists."
fi

# Step 4: Update Environment Variables
echo "🔧 Updating .env configuration..."
sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_NAME/" .env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USER/" .env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env

# Step 5: Generate App Key
echo "🔑 Generating application key..."
php artisan key:generate

# Step 6: Run Migrations & Seeder
echo "📂 Running migrations..."
php artisan migrate --seed --force

# Step 7: Storage and Permissions
echo "🛠 Setting up storage and permissions..."
php artisan storage:link
chmod -R 775 storage bootstrap/cache
chown -R $USER:$USER storage bootstrap/cache

# Step 8: Set Up Cron Job
echo "⏳ Setting up Laravel Scheduler..."
CRON_JOB="* * * * * cd $(pwd) && $PHP_VERSION artisan schedule:run >> /dev/null 2>&1"
(crontab -l | grep -v artisan || true; echo "$CRON_JOB") | crontab -

echo "✅ Cron job added!"

# Step 9: Start Laravel Queue
echo "⚡ Starting Laravel Queue..."
nohup php artisan queue:work --daemon > storage/logs/queue.log 2>&1 &

echo "✅ Laravel queue started!"

# Step 10: Start Development Server (Optional)
read -p "🌍 Do you want to start the Laravel development server? (y/n): " START_SERVER

if [[ "$START_SERVER" == "y" || "$START_SERVER" == "Y" ]]; then
    php artisan serve --host=0.0.0.0 --port=8000 &
    echo "🚀 Laravel server started at http://127.0.0.1:8000"
fi

echo "🎉 Laravel setup completed successfully!"





#!/bin/bash

# Welcome Message
echo "🚀 Laravel One-Click Setup Script with phpMyAdmin"

# Ask for User Inputs
read -p "📥 Enter Git Repository URL: " REPO_URL
read -p "📂 Enter Project Directory Name: " PROJECT_DIR
read -p "📊 Enter Database Name: " DB_NAME
read -p "👤 Enter Database Username: " DB_USER
read -sp "🔑 Enter Database Password: " DB_PASSWORD
echo ""

PHP_VERSION="php" # Adjust for your PHP version (e.g., php8.1)
USER=$(whoami)

# Step 1: Clone Repository
if [ -d "$PROJECT_DIR" ]; then
    echo "⚠️ Directory $PROJECT_DIR already exists. Skipping clone..."
else
    echo "📥 Cloning repository..."
    git clone $REPO_URL $PROJECT_DIR
fi

# Move into project directory
cd $PROJECT_DIR || exit

# Step 2: Install Dependencies
echo "📦 Installing Composer dependencies..."
composer install --no-interaction --prefer-dist

echo "📦 Installing NPM dependencies..."
npm install && npm run build

# Step 3: Set Up .env File
if [ ! -f .env ]; then
    echo "📝 Creating .env file..."
    cp .env.example .env
else
    echo "✅ .env file already exists."
fi

# Step 4: Update Environment Variables
echo "🔧 Updating .env configuration..."
sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_NAME/" .env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USER/" .env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env

# Step 5: Generate App Key
echo "🔑 Generating application key..."
php artisan key:generate

# Step 6: Run Migrations & Seeder
echo "📂 Running migrations..."
php artisan migrate --seed --force

# Step 7: Storage and Permissions
echo "🛠 Setting up storage and permissions..."
php artisan storage:link
chmod -R 775 storage bootstrap/cache
chown -R $USER:$USER storage bootstrap/cache

# Step 8: Set Up Cron Job
echo "⏳ Setting up Laravel Scheduler..."
CRON_JOB="* * * * * cd $(pwd) && $PHP_VERSION artisan schedule:run >> /dev/null 2>&1"
(crontab -l | grep -v artisan || true; echo "$CRON_JOB") | crontab -

echo "✅ Cron job added!"

# Step 9: Start Laravel Queue
echo "⚡ Starting Laravel Queue..."
nohup php artisan queue:work --daemon > storage/logs/queue.log 2>&1 &

echo "✅ Laravel queue started!"

# Step 10: Install phpMyAdmin
echo "📦 Installing phpMyAdmin..."
mkdir -p public/phpmyadmin
curl -L https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz | tar xz --strip-components=1 -C public/phpmyadmin

# Step 11: Create phpMyAdmin Route in Laravel
echo "🌍 Setting up phpMyAdmin route..."
cat <<EOT > routes/web.php
<?php

use Illuminate\Support\Facades\Route;

Route::get('/phpmyadmin', function () {
    return redirect('/phpmyadmin/index.php');
});
EOT

echo "✅ phpMyAdmin installed at: http://127.0.0.1:8000/phpmyadmin"

# Step 12: Start Development Server (Optional)
read -p "🌍 Do you want to start the Laravel development server? (y/n): " START_SERVER

if [[ "$START_SERVER" == "y" || "$START_SERVER" == "Y" ]]; then
    php artisan serve --host=0.0.0.0 --port=8000 &
    echo "🚀 Laravel server started at http://127.0.0.1:8000"
fi

echo "🎉 Laravel setup completed successfully with phpMyAdmin!"



DEMO DATA

📥 Enter Git Repository URL: https://github.com/example/laravel-project.git

📂 Enter Project Directory Name: my-laravel-app

📊 Enter Database Name: laravel_db

👤 Enter Database Username: root

🔑 Enter Database Password: ****

 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.