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:
✅ 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 Messageecho "🚀 Laravel One-Click Setup Script"# Ask for User Inputsread -p "📥 Enter Git Repository URL: " REPO_URLread -p "📂 Enter Project Directory Name: " PROJECT_DIRread -p "📊 Enter Database Name: " DB_NAMEread -p "👤 Enter Database Username: " DB_USERread -sp "🔑 Enter Database Password: " DB_PASSWORDecho ""PHP_VERSION="php" # Adjust for your PHP version (e.g., php8.1)USER=$(whoami)# Step 1: Clone Repositoryif [ -d "$PROJECT_DIR" ]; thenecho "⚠️ Directory $PROJECT_DIR already exists. Skipping clone..."elseecho "📥 Cloning repository..."git clone $REPO_URL $PROJECT_DIRfi# Move into project directorycd $PROJECT_DIR || exit# Step 2: Install Dependenciesecho "📦 Installing Composer dependencies..."composer install --no-interaction --prefer-distecho "📦 Installing NPM dependencies..."npm install && npm run build# Step 3: Set Up .env Fileif [ ! -f .env ]; thenecho "📝 Creating .env file..."cp .env.example .envelseecho "✅ .env file already exists."fi# Step 4: Update Environment Variablesecho "🔧 Updating .env configuration..."sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_NAME/" .envsed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USER/" .envsed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env# Step 5: Generate App Keyecho "🔑 Generating application key..."php artisan key:generate# Step 6: Run Migrations & Seederecho "📂 Running migrations..."php artisan migrate --seed --force# Step 7: Storage and Permissionsecho "🛠 Setting up storage and permissions..."php artisan storage:linkchmod -R 775 storage bootstrap/cachechown -R $USER:$USER storage bootstrap/cache# Step 8: Set Up Cron Jobecho "⏳ 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 Queueecho "⚡ 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_SERVERif [[ "$START_SERVER" == "y" || "$START_SERVER" == "Y" ]]; thenphp artisan serve --host=0.0.0.0 --port=8000 &echo "🚀 Laravel server started at http://127.0.0.1:8000"fiecho "🎉 Laravel setup completed successfully!"
#!/bin/bash# Welcome Messageecho "🚀 Laravel One-Click Setup Script with phpMyAdmin"# Ask for User Inputsread -p "📥 Enter Git Repository URL: " REPO_URLread -p "📂 Enter Project Directory Name: " PROJECT_DIRread -p "📊 Enter Database Name: " DB_NAMEread -p "👤 Enter Database Username: " DB_USERread -sp "🔑 Enter Database Password: " DB_PASSWORDecho ""PHP_VERSION="php" # Adjust for your PHP version (e.g., php8.1)USER=$(whoami)# Step 1: Clone Repositoryif [ -d "$PROJECT_DIR" ]; thenecho "⚠️ Directory $PROJECT_DIR already exists. Skipping clone..."elseecho "📥 Cloning repository..."git clone $REPO_URL $PROJECT_DIRfi# Move into project directorycd $PROJECT_DIR || exit# Step 2: Install Dependenciesecho "📦 Installing Composer dependencies..."composer install --no-interaction --prefer-distecho "📦 Installing NPM dependencies..."npm install && npm run build# Step 3: Set Up .env Fileif [ ! -f .env ]; thenecho "📝 Creating .env file..."cp .env.example .envelseecho "✅ .env file already exists."fi# Step 4: Update Environment Variablesecho "🔧 Updating .env configuration..."sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_NAME/" .envsed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USER/" .envsed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env# Step 5: Generate App Keyecho "🔑 Generating application key..."php artisan key:generate# Step 6: Run Migrations & Seederecho "📂 Running migrations..."php artisan migrate --seed --force# Step 7: Storage and Permissionsecho "🛠 Setting up storage and permissions..."php artisan storage:linkchmod -R 775 storage bootstrap/cachechown -R $USER:$USER storage bootstrap/cache# Step 8: Set Up Cron Jobecho "⏳ 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 Queueecho "⚡ Starting Laravel Queue..."nohup php artisan queue:work --daemon > storage/logs/queue.log 2>&1 &echo "✅ Laravel queue started!"# Step 10: Install phpMyAdminecho "📦 Installing phpMyAdmin..."mkdir -p public/phpmyadmincurl -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 Laravelecho "🌍 Setting up phpMyAdmin route..."cat <<EOT > routes/web.php<?phpuse Illuminate\Support\Facades\Route;Route::get('/phpmyadmin', function () {return redirect('/phpmyadmin/index.php');});EOTecho "✅ 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_SERVERif [[ "$START_SERVER" == "y" || "$START_SERVER" == "Y" ]]; thenphp artisan serve --host=0.0.0.0 --port=8000 &echo "🚀 Laravel server started at http://127.0.0.1:8000"fiecho "🎉 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: ****