Streamlining PHP Deployment: A Guide to Laravel Forge
Overview
Prerequisites
To follow this workflow, you should have a baseline understanding of
Key Libraries & Tools
- Laravel Forge: The primary platform for server management and deployment.
- DigitalOcean / Hetzner: Cloud infrastructure providers where your physical servers reside.
- Artisan: Laravel's built-in command-line interface for managing application state.
- Zero Downtime Deployment: A technique that ensures your site stays live while a new version is being pulled and built.
Code Walkthrough
Deploying an application involves connecting your repository and triggering a deployment script. Most Forge deployments rely on a shell script similar to the following:
cd /home/forge/forge-demo.com
git pull origin production
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "" | sudo -S service php8.2-fpm reload
if [ -f artisan ]; then
php artisan migrate --force
fi
This script ensures the latest code is pulled, dependencies are updated, and database migrations are executed automatically. Forge handles the sudo permissions and environment variables, so you don't have to manage SSH keys manually for every small change.
Syntax Notes
When interacting with the server via the Forge web terminal, you will frequently use the artisan command. In php artisan [command] pattern. For example:
php artisan list
This confirms the environment is active. Forge also uses a "Command Palette" (triggered by Cmd+P or Ctrl+P) which provides a searchable interface for server-wide actions, mirroring the developer experience found in modern IDEs.
Tips & Gotchas
Always leave Zero Downtime Deployments enabled. This feature creates a symlink to a new release folder, ensuring that if a build fails during composer install, your live site continues to serve the old version. Another best practice is to use the onforge.com subdomains for staging environments before pointing your primary DNS to the server. This allows you to verify SSL certificates and database connections in a production-identical environment before the public launch.
