Laravel Artisan Commands via SSH: Complete Guide
Artisan is the command-line interface of Laravel that automates common tasks. In this guide, you will learn all the important Artisan commands that you can use via SSH on your hosting.
What is Artisan
Artisan is built into Laravel and offers:
- Migrations and database management
- Cache management
- Code generation (make commands)
- Queue and job management
- Maintenance mode
- Much more via custom commands
Connecting via SSH
Before you can use Artisan, connect via SSH.
Connect
ssh username@yourdomain.nl
# Or with specific port
ssh -p 7777 username@server.theory7.net
Navigate to your project
cd ~/domains/yourdomain.nl/project
Test Artisan
php artisan --version
# Output: Laravel Framework 11.x.x
php artisan list
# Shows all available commands
Cache Commands
Cache management is essential for performance.
Clear all caches
# Clear all caches at once
php artisan optimize:clear
This combines:
- config:clear
- route:clear
- view:clear
- cache:clear
Configuration cache
# Cache configuration
php artisan config:cache
# Clear configuration cache
php artisan config:clear
Note: After config:cache, .env is no longer read directly. Changes require caching again.
Route cache
# Cache routes
php artisan route:cache
# Clear route cache
php artisan route:clear
Significantly speeds up route resolution.
View cache
# Pre-compile all Blade views
php artisan view:cache
# Clear view cache
php artisan view:clear
Application cache
# Clear application cache
php artisan cache:clear
# With specific store
php artisan cache:clear --store=file
Production optimization
# Cache everything for production
php artisan optimize
# Or extensively
php artisan config:cache
php artisan route:cache
php artisan view:cache
Database Commands
Manage your database structure and data.
Migrations
# Run pending migrations
php artisan migrate
# With force for production
php artisan migrate --force
# Rollback last batch
php artisan migrate:rollback
# Rollback specific number
php artisan migrate:rollback --step=3
# Reset all migrations
php artisan migrate:reset
# Reset and re-run (destructive!)
php artisan migrate:fresh
Database seeding
# Run all seeders
php artisan db:seed
# Specific seeder
php artisan db:seed --class=UserSeeder
# Fresh migrate with seeding
php artisan migrate:fresh --seed
Migration status
# View migration status
php artisan migrate:status
Schema dump
# Dump schema to SQL
php artisan schema:dump
# With pruning of old migrations
php artisan schema:dump --prune
Make Commands
Quickly generate code with make commands.
Controllers
# Basic controller
php artisan make:controller UserController
# Resource controller (CRUD)
php artisan make:controller PostController --resource
# API controller
php artisan make:controller Api/ProductController --api
Models
# Basic model
php artisan make:model Post
# With migration
php artisan make:model Post -m
# With everything (migration, factory, seeder, controller)
php artisan make:model Post --all
Migrations
# New migration
php artisan make:migration create_posts_table
# Modification to existing table
php artisan make:migration add_status_to_posts_table --table=posts
Other resources
# Request validation
php artisan make:request StorePostRequest
# Middleware
php artisan make:middleware AuthenticateAdmin
# Event and listener
php artisan make:event OrderShipped
php artisan make:listener SendOrderNotification
# Job
php artisan make:job ProcessPodcast
# Mail
php artisan make:mail OrderConfirmation
# Notification
php artisan make:notification InvoicePaid
Queue Commands
Manage background jobs.
Queue worker
# Start queue worker
php artisan queue:work
# With specific connection
php artisan queue:work database
# With timeout and memory limit
php artisan queue:work --timeout=60 --memory=128
# Process only N jobs
php artisan queue:work --once
php artisan queue:work --max-jobs=100
Queue management
# View failed jobs
php artisan queue:failed
# Retry failed job
php artisan queue:retry {id}
php artisan queue:retry all
# Remove failed job
php artisan queue:forget {id}
php artisan queue:flush # All failed jobs
# Restart workers (after code change)
php artisan queue:restart
Maintenance Mode
Temporarily take your site offline.
Activate
# Basic maintenance mode
php artisan down
# With custom message
php artisan down --message="We'll be back soon!"
# With retry header (seconds)
php artisan down --retry=60
# Allow specific IPs
php artisan down --allow=123.456.789.0
# With secret bypass URL
php artisan down --secret="bypass-token"
With the secret, you can view the site via:
https://yourdomain.nl/bypass-token
Deactivate
php artisan up
Debugging Commands
Useful commands for debugging.
View routes
# List all routes
php artisan route:list
# Filter by name
php artisan route:list --name=admin
# Filter by method
php artisan route:list --method=GET
Environment info
# Show current environment
php artisan env
# Encrypt .env values
php artisan env:encrypt
Tinker (REPL)
# Start interactive shell
php artisan tinker
# In tinker:
> User::count()
> User::find(1)
> App::environment()
Schedule Commands
Manage scheduled tasks.
Run schedule
# Manually run scheduler
php artisan schedule:run
# View scheduled tasks
php artisan schedule:list
# Test specific command
php artisan schedule:test
Schedule in cronjob
Add to crontab:
* * * * * cd /path/to/project && php artisan schedule:run >> /dev/null 2>&1
Storage Commands
Manage storage and files.
Storage link
# Create public storage link
php artisan storage:link
# Verify links
ls -la public/storage
Storage cleanup
Custom command or manually:
# Remove temp files older than X days
find storage/app/temp -mtime +7 -delete
Deployment Commands
Typical deployment workflow:
# 1. Activate maintenance
php artisan down
# 2. Update dependencies (if needed)
composer install --no-dev --optimize-autoloader
# 3. Migrations
php artisan migrate --force
# 4. Build cache
php artisan config:cache
php artisan route:cache
php artisan view:cache
# 5. Restart queue workers
php artisan queue:restart
# 6. Deactivate maintenance
php artisan up
One-liner for deployment
php artisan down && composer install --no-dev --optimize-autoloader && php artisan migrate --force && php artisan optimize && php artisan up
Custom Artisan Commands
Create your own commands:
php artisan make:command SendWeeklyReport
This creates `app/Console/Commands/SendWeeklyReport.php`.
Related Articles
- Laravel .env and storage configuration
- Laravel with Composer via SSH
- Installing Symfony on hosting
- More information about Laravel hosting at Theory7
Need Help?
We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help.
0 van 0 vonden dit nuttig