Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel deploy script
# Change to the project directory
cd /home/forge/domain.com
# Turn on maintenance mode
php artisan down || true
# Pull the latest changes from the git repository
# git reset --hard
# git clean -df
git pull origin master
# Install/update composer dependecies
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
# Restart FPM
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
# Run database migrations
php artisan migrate --force
# Clear caches
php artisan cache:clear
# Clear expired password reset tokens
php artisan auth:clear-resets
# Clear and cache routes
php artisan route:cache
# Clear and cache config
php artisan config:cache
# Clear and cache views
php artisan view:cache
# Install node modules
# npm ci
# Build assets using Laravel Mix
# npm run production
# Turn off maintenance mode
php artisan up
@OlahTamas

This comment has been minimized.

Copy link

@OlahTamas OlahTamas commented Jun 13, 2018

Hi, a small suggestion from personal experience: drop in a 'php artisan queue:restart' somewhere, if you use queue workers.

@tvbeek

This comment has been minimized.

Copy link

@tvbeek tvbeek commented Jun 14, 2018

If you add --no-dev it will not install the dev requirements (Like phpunit, faker and other packages that you don't need on production)

The composer command is then:
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev

@reindert-vetter

This comment has been minimized.

Copy link

@reindert-vetter reindert-vetter commented Jun 14, 2018

You better use this library. Then you do not have any downtime.
https://github.com/deployphp/deployer/blob/master/README.md

@BenSampo

This comment has been minimized.

Copy link
Owner Author

@BenSampo BenSampo commented Jun 14, 2018

@tvbeek, I've added --no-dev as per your suggestion, thanks!

@OlahTamas thanks for your suggestion too. I'm not going to add it because I think it's something you'll do as part of setting up queue workers for your application. It's per-project thing.

@freekmurze

This comment has been minimized.

Copy link

@freekmurze freekmurze commented Jun 15, 2018

That's a cool script! It's clear, it's simple, it's well made, it does the job 👍

The only downside is that there is some downtime involved for the users of the site.

For those interested I've made a more complex deploy script. That complexity buys you a near zero downtime deploy. The application is built in a separate directory and where it's ready a symlink to the public folder is set.

@jannejava

This comment has been minimized.

Copy link

@jannejava jannejava commented Jun 16, 2018

Good job! Reloading PHP FPM service can also be a good idea if you are using OPCache.

@lorisleiva

This comment has been minimized.

Copy link

@lorisleiva lorisleiva commented Jun 18, 2018

🔥 Cool script :) I am using something very similar on my package Laravel Deployer (a wrapper around DeployerPhp for Laravel applications that lets you deploy with zero downtime via a simple php artisan deploy).

@peterjaap

This comment has been minimized.

Copy link

@peterjaap peterjaap commented Jun 20, 2018

Extra tip; require https://github.com/erjanmx/laravel-migrate-check into your project and use php artisan migrate:check to see whether there are migrations to be run. If it returns a zero exit code, there are no migrations and you don't have to pull your site down.

@vahidalvandi

This comment has been minimized.

Copy link

@vahidalvandi vahidalvandi commented Jun 22, 2019

how i can use this? manual ??

@BenSampo

This comment has been minimized.

Copy link
Owner Author

@BenSampo BenSampo commented Jun 22, 2019

@vahidalvandi

This comment has been minimized.

Copy link

@vahidalvandi vahidalvandi commented Jun 25, 2019

add this line for ignore git history

git clone --depth 1 git@****.com:xxx/xxx.git .

@lukio3

This comment has been minimized.

Copy link

@lukio3 lukio3 commented Jul 17, 2019

Nice script! A suggestion:

  • Don’t run npm install on prod, run npm ci instead: Source
@fbc

This comment has been minimized.

Copy link

@fbc fbc commented Apr 2, 2020

I think you are also forgetting to dump the autoload and run new seeders.

@underdpt

This comment has been minimized.

Copy link

@underdpt underdpt commented Jul 24, 2020

This is great. What I'm missing is maybe a check on every command, and if it fails don't continue deployment.

@npostman

This comment has been minimized.

Copy link

@npostman npostman commented Aug 28, 2020

Great boilerplate deploy script. I've forked it and made a couple of changes. One of them can be really helpful:

# Turn on maintenance mode
php artisan down || true

If the app is already down, the artisan command exits with non-zero return, and the deploy script stops. This addition makes sure the deploy script keeps going.

Images you find a bug in your app, you manually php artisan down on the server, fix the bug and deploy the new code. Without this change, the deploy script breaks. 'Application is already down'

Additionally, you can take out all the 'clear' commands in the cache lines, as the php artisan route:cache commands first clears it.

And use npm ci instead of install, as @lukio3 mentions.

Thanks for getting this together.

@BenSampo

This comment has been minimized.

Copy link
Owner Author

@BenSampo BenSampo commented Aug 28, 2020

All good calls @npostman, I've updated.

Nice trick with using php artisan down || true - I've run into the Application is already down error myself.

@npostman

This comment has been minimized.

Copy link

@npostman npostman commented Aug 28, 2020

@BenSampo I’m not sure if you want to call php artisan cache:clear. People can cache all sorts of stuff you don’t want to clear out.
I meant to say that if you call php artisan route:cache it first clears out the route cache before caching it again.

@arspyus

This comment has been minimized.

Copy link

@arspyus arspyus commented Nov 27, 2020

@BenSampo I think it will be also good to add umask 022 before pulling from repo, to get all files with correct permissions in any environment.

@hassanalami

This comment has been minimized.

Copy link

@hassanalami hassanalami commented Feb 22, 2021

i think you should add npx browserslist@latest --update-db
To avoid this warning:

Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment