Zero Downtime Laravel Forge Deploys
So you’re a Laravel developer and of course you are using Forge to manage your servers. You enjoy how simple Forge makes it to deploy your applications but you may notice a few seconds of downtime when your app is updating packages etc. To solve this you could use Envoyer, from Taylor Otwell or hack together you own solution.
After some searching I came across a gist from rap2hpoutre. It works by making a copy of your existing application and updating the copy of your app(git, composer, migrations, etc). Then it moves your current application to a new backup folder and replaces your live site with the newly updated files. This enables you to revert back if something goes wrong. Make sure you rename the folders so they are unique per project if you are hosting multiple apps on the same server.
# stop script on error signal
# remove old deployment folders
if [ -d "/home/forge/weather-deploy" ]; then
rm -R /home/forge/weather-deploy
if [ -d "/home/forge/weather-backup" ]; then
rm -R /home/forge/weather-backup
cp -R /home/forge/weather.timleland.com /home/forge/weather-deploy
git pull origin master
composer install --no-interaction --prefer-dist --optimize-autoloader
php artisan cache:clear
php artisan route:cache
php artisan view:clear
php artisan migrate --force
php artisan opcache:clear
# Switch (downtime for microseconds)
mv /home/forge/weather.timleland.com /home/forge/weather-backup
mv /home/forge/weather-deploy /home/forge/weather.timleland.com
After updating the script for my projects and saving it as my deploy script on Forge, I was able to deploy my Weather app with virtually zero downtime. Its great to know I can deploy at anytime without upsetting users.
Let me know your thoughts and how this could be improved in the comments below.
Originally published at Tim Leland.