Installation:
composer require omaralalwi/laravel-deployer
Publish the config file:
php artisan vendor:publish --provider="OmarAlalwi\Deployer\DeployerServiceProvider"
Configure .env:
Add your SSH credentials and server details to .env:
DEPLOYER_SSH_HOST=your_server_ip
DEPLOYER_SSH_USER=your_ssh_user
DEPLOYER_SSH_KEY_PATH=~/.ssh/id_rsa
DEPLOYER_REPO_URL=git@github.com:your/repo.git
DEPLOYER_REPO_BRANCH=main
First Deployment: Run the deploy command:
php artisan deployer:deploy
Verify the deployment via the web UI (if enabled) or SSH.
Configure Zero-Downtime:
In config/deployer.php, set:
'zero_downtime' => true,
'deploy_path' => '/var/www/your-app/releases',
'shared_path' => '/var/www/your-app/shared',
'current_path' => '/var/www/your-app/current',
Deploy:
php artisan deployer:deploy --zero-downtime
The package handles symlink swapping and PHP-FPM/Nginx restarts (if configured).
GitHub Actions Example:
name: Deploy Laravel
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-php@v2
with:
php-version: '8.2'
- run: composer install -o --no-dev
- run: php artisan deployer:deploy
env:
DEPLOYER_SSH_HOST: ${{ secrets.SSH_HOST }}
DEPLOYER_SSH_KEY: ${{ secrets.SSH_KEY }}
Environment-Specific Deployments:
Use --env flag or environment variables:
php artisan deployer:deploy --env=staging
Configure environments in config/deployer.php:
'environments' => [
'staging' => [
'branch' => 'staging',
'deploy_path' => '/var/www/staging-app',
],
'production' => [
'branch' => 'main',
'deploy_path' => '/var/www/prod-app',
],
],
Custom Commands:
Extend deployment with custom scripts in config/deployer.php:
'extra_commands' => [
'post_deploy' => [
'php artisan cache:clear',
'php artisan config:clear',
],
'post_rollback' => [
'php artisan cache:clear',
],
],
Node.js Support:
Configure Node.js dependencies in config/deployer.php:
'nodejs' => [
'install' => true,
'version' => '18',
'commands' => [
'npm install',
'npm run build',
],
],
PHP Restart: Add custom PHP restart logic (e.g., for PHP-FPM):
'php_restart' => [
'command' => 'sudo systemctl restart php8.2-fpm',
'enabled' => true,
],
SSH Key Issues:
DEPLOYER_SSH_KEY_PATH points to a valid key with proper permissions (chmod 600 ~/.ssh/id_rsa).ssh -T -i ~/.ssh/id_rsa your_ssh_user@your_server_ip
Permission Errors:
root unless necessary (disable php_restart if using sudo is restricted).deploy_path and shared_path:
sudo chown -R your_ssh_user:www-data /var/www/your-app
Zero-Downtime Quirks:
current_path symlink is writable.try_files directives in your server block point to the correct current_path.Verbose Output:
Enable debug mode in .env:
DEPLOYER_DEBUG=true
Or use the --verbose flag:
php artisan deployer:deploy --verbose
Dry Run: Test deployment without changes:
php artisan deployer:deploy --dry-run
Logs: Check server logs for errors:
tail -f /var/www/your-app/releases/*/storage/logs/laravel.log
Custom Deployment Scripts: Override default behavior by publishing and modifying:
php artisan vendor:publish --tag=deployer-scripts
Edit resources/views/deployer/scripts.blade.php for custom logic.
Web UI (Future Feature):
Monitor deployments via the planned UI (check TODO in the repo). For now, use:
php artisan deployer:list-releases
Branch-Specific Deployments: Dynamically set branches via environment variables or CLI:
php artisan deployer:deploy --branch=feature/x
Shared Paths:
Ensure shared_path includes files like .env and storage/logs:
'shared' => [
'storage/logs',
'storage/framework/sessions',
'storage/framework/cache',
'public/storage',
'.env',
],
PHP Version Mismatch:
If using multiple PHP versions, specify the correct path in config/deployer.php:
'php_path' => '/usr/bin/php8.2',
Composer Cache: Clear Composer cache on the server if deployments fail:
'extra_commands' => [
'pre_deploy' => [
'composer clear-cache',
],
],
How can I help you explore Laravel packages today?