Automate your Laravel deployment with Capistrano 3
You might have already put some websites online in the past. Probably you’ve used a FTP client and uploaded every single file manually. Or perhaps you always logged into your server via ssh and pulled the changes manually.
What if we can simplify this process as much as possible by automating it using my favorite deployment tool Capistrano which will automatically log into your server and deploy your system.
Hmmm, Let’s try it out.
I am making a couple of assumptions here.
- You have a remote server with ssh access from your machine.
- You have a fresh installation of Laravel 5.5 and it’s dependencies on remote and local servers like PHP, Nginx, Composer, MySql etc.
- Familiarity with Git and GitHub, or a GitHub-like alternative and SSH key pair set up.
- Capistrano installed on you local machine.
Now we have everything we need, so let’s setup our deployment settings. But first we have to create a folder on the remote server where all the files should be deployed to. Log into your server with SSH and create a folder.
$ sudo mkdir /var/www/laravel-capistrano
$ sudo chown -R username:group /var/www/laravel-capistrano
Now, Let’s Capify our laravel project.
# amritgc at developer in ~/Projects/github/laravel-capistrano
$ cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified
This command will create the basic files we need. After that your folder should look like this.
.
├── Capfile
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
└── lib
└── capistrano
└── tasks
Now we’ll just need to edit config/deploy.rb
which responsible for all the building steps.
In the namespace :deploy block of the deploy.rb
file you specify what actually should happen for each deploy. So there are three tasks.
- In the
composer:install
task we install all your Laravel dependencies, just like you’re used to it during development. - After that we have
laravel:fix_permission
which will fix permission ofstorage
andcache
folder. - Then, we have
laravel:configure_dot_env
task which will simply copy you Laravel’s production env configuration to the root of you project from a secret folder which is in our caseset :laravel_dotenv_file, ‘/var/www/secrets/.env’
Finally the production.rb file
This file is an environment that you want to deploy to. You can have staging.rb or development.rb.
# config/deploy/production.rbserver '172.19.0.6', user: 'amritgc', roles: %w{web app laravel composer}set :ssh_options, {
keys: %w(/home/amritgc/.ssh/id_rsa), // path to you ssh key
forward_agent: false,
auth_methods: %w(publickey)
}
And now finally, let’s deploy:
$ cap production deploy
So hopefully you’ve seen how to deploy you Laravel application with Capistrano.
View the source code for this article on GitHub.
Conclusion
You’ve just learned a very simple way of deploying your Laravel application to your server(s) with Capistrano. Once the configuration work is done, it just takes one command to deploy your latest version in seconds.
Happy Coding..