Why not use symlinks for releases? Below is an example of a deployment process that I've used on Laravel applications with Envoy. Aside from the PHP variable notation, it would be straightforward to substitute a purely bash/shell script if you are not using Envoy. Essentially, having a script automates the deployment, and using symlinks can make the update nearly instantaneous. Additional benefits include previous releases existing for the unfortunate time when a rollback is necessary.
Note: The below script makes some basic assumptions:
- Your
.env
file is in the $root_dir
(ex: /var/www/my-website/.env
).
Your vhost points to the site/public
directory within the $root_dir
(ex: /var/www/my-website/site/public
). However, if you can not update the vhost, you can simply add the following to number 4 below in the empty line:
ln -nfs {{ $app_dir }}/public {{ $root_dir }}/public ;
sudo chgrp -h www-data {{ $root_dir }}/public;
You have added SSH keys to pull from Git repo
- (optional) nodejs is installed
Here are the relevant example variables for the script:
$repo = '[email protected]:myusername/my-repo.git';
$root_dir = '/var/www/my-website';
$release_dir = '/var/www/my-website/releases';
$app_dir = '/var/www/my-website/site';
$release = 'release_' . date('YmdHis');
$branch = 'master';
Here is the gist of the deployment process with code:
Fetch the updated code into a new release directory:
@task('fetch_repo')
[ -d {{ $release_dir }} ] || mkdir {{ $release_dir }};
cd {{ $release_dir }};
git clone {{ $repo }} -b {{ $branch }} {{ $release }};
@endtask
Install the dependencies by running composer:
@task('run_composer')
cd {{ $release_dir }}/{{ $release }};
composer install;
@endtask
(optional) If we are using asset precompiler like Elixir, we will want to fetch npm dependencies, reset permissions, and run gulp:
@task('npm_install')
cd {{ $release_dir }}/{{ $release }};
sudo npm install;
@endtask
@task('update_permissions')
cd {{ $release_dir }};
sudo chgrp -R www-data {{ $release }};
sudo chmod -R ug+rwx {{ $release }};
@endtask
@task('compile_assets')
cd {{ $release_dir }}/{{ $release }};
gulp --production;
@endtask
Update symlinks
@task('update_symlinks')
ln -nfs {{ $root_dir }}/.env {{ $release_dir }}/{{ $release }}/.env;
ln -nfs {{ $release_dir }}/{{ $release }} {{ $app_dir }};
sudo chgrp -h www-data {{ $app_dir }};
sudo service php5-fpm restart;
@endtask
(Optional) Prune old release folders (30+ days old) so we don't fill up the server.
@task('prune_old')
sudo find {{ $release_dir }} -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;
@endtask
Note: Restarting the php5-fpm
service clears the cache that ensures the new symlink is followed.
I found it somewhat difficult to find deployment script examples (like the aforementioned) when I initially began developing with Laravel, so hopefully this will help alleviate some searching.
php artisan down
andup
? – user2094178