Skip to content

Instantly share code, notes, and snippets.

@yohanesgultom
Last active May 30, 2021 10:38
Show Gist options
  • Save yohanesgultom/1de1e9dd99652b17fddf897049d6b24c to your computer and use it in GitHub Desktop.
Save yohanesgultom/1de1e9dd99652b17fddf897049d6b24c to your computer and use it in GitHub Desktop.
Random PHP scripts
Random PHP scripts
<?php
// 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 (https://laravel.com/docs/master/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 https://docs.gitlab.com/ee/ci/examples/laravel_with_gitlab_and_envoy/
Running command: envoy run deploy
Prerequisites:
✓ Laravel prerequisites are installed on @servers https://laravel.com/docs/master/installation#server-requirements
✓ $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 https://docs.gitlab.com/ee/api/deploy_keys.html (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 https://laravel.com/docs/master/passport
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
Author: yohanes.gultom@gmail.com
--}}
{{-- deployment server user and ip address --}}
@servers(['web' => 'user@ipaddress'])
@setup
$repository = 'git@gitlab.com:username/myapp.git'; {{-- 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 --}}
@endsetup
@story('deploy')
clone_repository
{{-- setup_supervisor --}} {{-- Optional. Requires file named like 'myapp-worker.conf' in repo root --}}
run_composer
set_permissions
update_symlinks
{{-- 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' --}}
clear_cache
clean_old_releases {{-- Limit number of releases stored in server --}}
@endstory
@task('clone_repository')
echo 'Cloning repository'
[ -d {{ $releases_dir }} ] || mkdir -p {{ $releases_dir }}
git clone --depth 1 {{ $repository }} {{ $new_release_dir }}
@endtask
{{-- Optional. Copy worker conf file from repo to default Debian/Ubuntu Supervisord directory --}}
@task('setup_supervisor')
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
@endtask
@task('run_composer')
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 --}}
@endtask
{{-- Allow web server user (assumed www-data) to modify bootstrap and storage directories (usually for caches and user files) --}}
@task('set_permissions')
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
@endtask
@task('update_symlinks')
{{-- 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) https://laravel.com/docs/master/filesystem --}}
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 }}
@endtask
{{-- Drops dan recreates database. Only for non-production https://laravel.com/docs/5.8/migrations#running-migrations --}}
@task('refresh_database')
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 --}}
@endtask
{{-- Migrates database. Only for non-production https://laravel.com/docs/5.8/migrations#running-migrations --}}
@task('migrate_database')
echo "Migrate database"
cd {{ $new_release_dir }}
php artisan migrate
@endtask
{{-- Clear all caches. Useful for production --}}
@task('clear_cache')
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 --}}
@endtask
{{-- This will list our releases by modification time and delete all but the 3 most recent (depends on $release date format). --}}
@task('clean_old_releases')
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;
else
echo "No releases found for purging at this time";
fi
@endtask
<?php
// 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 '127.0.0.1' | 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 https://laravel.com/docs/master/queues#supervisor-configuration
[program:myapp-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/user/myapp/current/artisan queue:work --sleep=3 --tries=3 --timeout=60 ; Modify according to your static artisan path
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
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