Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Random PHP scripts
Random PHP scripts
// find function declaration
$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
// find class declaration
$reflClass = new ReflectionClass('class_name');
print $reflClass->getFileName();
A commented Laravel Envoy ( script that deploy from private GitLab repo
Tested on Laravel 5.8 and Laravel Envoy 1.5 and Ubuntu 16.04 LTS
Inspired by
Running command: envoy run deploy
✓ Laravel prerequisites are installed on @servers
✓ $releases_dir, $app_dir, $data_dir are available at @servers
✓ @servers user is a sudoer that requires NO password
✓ @servers runs web server (apache/nginx) using www-data
✓ @servers SSH public key has been registred as Deploy Keys in GitLab (required unless using public repo)
✓ @servers has registered GitLab as known server (do git clone from there once)
✓ (optional) Supervisord is installed. Needed only queue worker setup is activated
✓ (optional) Laravel Passport is one of dependency
Optional features (activate by uncomment corresponding parts):
✓ Supervisord worker: setup, update, refresh worker cache
✓ Laravel Passport: setup (generate secret keys)
✓ Migrate database: migrate or fresh (drop/create all)
✓ Temporary directory for generated files
{{-- deployment server user and ip address --}}
@servers(['web' => 'user@ipaddress'])
$repository = ''; {{-- GitLab repo --}}
$releases_dir = '$HOME/myapp'; {{-- stored cloned repositories --}}
$app_dir = '/var/www/myapp'; {{-- web server (apache/nginx) symlink --}}
$data_dir = '$HOME/myapp-data'; {{-- shared directory to store app config (.env) and data (storage/*, tmp/*) --}}
$release = date('YmdHis'); {{-- source code version tag/identifier eg. 20190328235959. WARNING This date format is required by clean_old_releases task --}}
$new_release_dir = $releases_dir .'/'. $release; {{-- current release path eg. $HOME/myapp/20190328235959 --}}
{{-- setup_supervisor --}} {{-- Optional. Requires file named like 'myapp-worker.conf' in repo root --}}
{{-- refresh_database --}} {{-- Optional. Requires APP_ENV (in .env) to be other than 'production' --}}
{{-- migrate_database --}} {{-- Optional. Requires APP_ENV (in .env) to be other than 'production' --}}
clean_old_releases {{-- Limit number of releases stored in server --}}
echo 'Cloning repository'
[ -d {{ $releases_dir }} ] || mkdir -p {{ $releases_dir }}
git clone --depth 1 {{ $repository }} {{ $new_release_dir }}
{{-- Optional. Copy worker conf file from repo to default Debian/Ubuntu Supervisord directory --}}
echo 'Setup supervisor'
sudo cp {{ $new_release_dir }}/*-worker.conf /etc/supervisor/conf.d/ {{-- You may need to change this if you have different Supervisord installation --}}
sudo supervisorctl update
echo "Starting deployment ({{ $release }})"
cd {{ $new_release_dir }}
composer install --prefer-dist --no-scripts -q -o {{-- Include dev dependency in case dev process is required (eg. DB seeding with Faker, phpunit testing) --}}
{{-- composer install --prefer-dist --no-scripts -q -o --no-dev --}} {{-- Use this instead if no dev process is required --}}
{{-- Allow web server user (assumed www-data) to modify bootstrap and storage directories (usually for caches and user files) --}}
echo "Setting permissions"
sudo chown -R www-data:www-data {{ $new_release_dir }}/bootstrap
sudo chown -R www-data:www-data {{ $new_release_dir }}/storage
{{-- Create symlink for storage --}}
echo "Linking storage directory"
rm -rf {{ $new_release_dir }}/storage
ln -nfs {{ $data_dir }}/storage {{ $new_release_dir }}/storage
{{-- Create symlink for public storage (to store user public/insecure uploaded files such as profile images) --}}
echo "Linking public storage directory"
rm -rf {{ $new_release_dir }}/public/storage
ln -nfs {{ $data_dir }}/storage/app/public {{ $new_release_dir }}/public/storage
{{-- Create symlink for .env file so it won't be overwritten --}}
echo 'Linking .env file'
ln -nfs {{ $data_dir }}/.env {{ $new_release_dir }}/.env
{{-- Optional. Uncomment this if you require '/tmp' directory to store temporary files (eg. report, invoice, generated images --}}
echo 'Linking tmp dir'
rm -Rf {{ $new_release_dir }}/tmp
ln -nfs {{ $data_dir }}/tmp {{ $new_release_dir }}/tmp
echo 'Linking current release'
sudo ln -nfs {{ $new_release_dir }} {{ $releases_dir }}/current
sudo ln -nfs {{ $new_release_dir }}/public {{ $app_dir }}
{{-- Drops dan recreates database. Only for non-production --}}
echo "Refreshing database"
rm -Rf {{ $data_dir }}/storage/app/public/*
cd {{ $new_release_dir }}
php artisan migrate:fresh --seed {{-- Drop all tables, recreates tables and runs seeder --}}
{{-- php artisan passport:install --}} {{-- Optional. Only if using passport --}}
{{-- php artisan queue:flush --}} {{-- Optional. Only if using queue --}}
{{-- Migrates database. Only for non-production --}}
echo "Migrate database"
cd {{ $new_release_dir }}
php artisan migrate
{{-- Clear all caches. Useful for production --}}
echo "Clearing cache"
cd {{ $new_release_dir }}
sudo -u www-data php artisan route:clear
sudo -u www-data php artisan view:clear
sudo -u www-data php artisan cache:clear
{{-- sudo -u www-data php artisan queue:restart --}} {{-- Optional. If queue is used --}}
sudo -u www-data php artisan config:cache
{{-- echo "Restarting supervisor worker"
sudo supervisorctl restart all --}} {{-- Optional. If Supervisord is used --}}
{{-- This will list our releases by modification time and delete all but the 3 most recent (depends on $release date format). --}}
purging=$(ls -dt {{ $releases_dir }}/* | tail -n +3); {{-- +3 means keep 3 releases (including current). If you do not want to keep backup, change the value to +1 --}}
if [ "$purging" != "" ]; then
echo Purging old releases: $purging;
rm -rf $purging;
echo "No releases found for purging at this time";
// Get LAN IP address of the hosting server
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$ipconfig = shell_exec("ipconfig");
} else {
$ip = shell_exec("/sbin/ifconfig | grep 'inet '| grep -v '' | cut -d: -f2 | awk '{ print $1}'");
// prepend address to protocol and app name
$appname = "moodle";
$url = "http://$ip/$appname";
echo "Application address: <a href='$url'>$url</a>";
; Example of Laravel Supervisord queue worker
command=php /home/user/myapp/current/artisan queue:work --sleep=3 --tries=3 --timeout=60 ; Modify according to your static artisan path
stdout_logfile=/home/user/myapp/current/storage/logs/supervisord.log ; Modify according to your static log path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.