Advertisement
  1. Code
  2. PHP
  3. Laravel

Deploying a Laravel Application Using Capistrano

Scroll to top
7 min read

So you've just built a fancy web application and you're planning to put it online. This can be done in many ways. In this article I'd like to cover one approach to deploy your backend system to your production server. We'll go through the following steps by example of a Laravel application but this can be applied to any other language or technology.

Update

This article was updated to Capistrano 3. More about the new version can be found on capistrano's website.

The Past

Perhaps you have already put some websites online in the past. Probably you've used a FTP client and uploaded the bits and bytes by hand. Or perhaps you always logged into your server via ssh and pulled the changes manually.

The Idea

Our goal is to simplify this process as much as possible. The idea is to use your code repository as a source for every deploy. The deployment tool, in our case capistrano, will automatically log into your server and build your system right out of your repository.

Software deployment is all of the activities that make a software system available for use. - Wikipedia

What You'll Need...

...on Your Remote Server

Your remote server needs to provide ssh access. It also should have installed all necessary dependencies for your project such as GIT, PHP, MySQL, Composer, ... Besides that, you don't need any extra software on your production server.

...on Your Local Machine

In order to install and use capistrano, you need at least Ruby 1.9 (if you don't have Ruby installed, I recommend installing it using rbenv). To install capistrano, you simply have to run:

1
2
$ gem install capistrano

So why capistrano, you may ask. As always, there are many ways to accomplish a task but in my case, capistrano always seemed to be the easiest and most flexible approach. You can configure it to all your needs and there are a lot of plugins out there which simplify your work again.

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH. It uses a simple DSL (borrowed in part from Rake) that allows you to define tasks, which may be applied to machines in certain roles. It also supports tunneling connections via some gateway machine to allow operations to be performed behind VPN's and firewalls.

Prepare

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. A common place is /var/www/. So let's do this:

1
2
$ sudo mkdir /var/www/my-app
3
$ sudo chown -R username:group /var/www/my-app

That's it. There is nothing more to do on the remote server, so you can close the ssh connection and move on. Go into your project (or any other folder, that doesn't matter right now) and run:

1
2
$ cd my-project
3
$ cap install

This command will create the basic files we need. After that your folder should look like this.

1
2
.
3
├── Capfile
4
├── config
5
│   ├── deploy
6
│   │   ├── production.rb
7
│   │   └── staging.rb
8
│   └── deploy.rb
9
└── lib
10
    └── capistrano
11
        └── tasks

The Capfile is like the mount point for capistrano but for now we'll just need to edit config/deploy.rb and config/deploy/production.rb. The first file is responsible for all the building steps, the second file represents a "stage". You can have several stages like production, staging, testing... In each stage-configuration file you can specify your server(s). Let's open those two files in your favourite text editor and replace the content with the following snippets. We'll go through the code afterwards.

We'll start with config/deploy/production.rb:

1
2
role :app, %w{username@123.456.789.123} # EDIT your ssh username and server ip address 

3
4
set :ssh_options, {
5
    auth_methods: %w(password),
6
    password: "" # EDIT your ssh password

7
}

Next we'll modify config/deploy.rb:

1
2
set :application, "Your app name"  # EDIT your app name

3
set :repo_url,  "https://github.com/laravel/laravel.git" # EDIT your git repository

4
set :deploy_to, "/var/www/my-app" # EDIT folder where files should be deployed to

5
6
namespace :deploy do
7
    
8
    desc "Build"
9
    after :updated, :build do
10
        on roles(:app) do
11
            within release_path  do
12
                execute :composer, "install --no-dev --quiet" # install dependencies

13
                execute :chmod, "u+x artisan" # make artisan executable

14
            end
15
        end
16
    end
17
18
    desc "Restart"
19
    task :restart do
20
        on roles(:app) do
21
            within release_path  do
22
                execute :chmod, "-R 777 app/storage/cache"
23
                execute :chmod, "-R 777 app/storage/logs"
24
                execute :chmod, "-R 777 app/storage/meta"
25
                execute :chmod, "-R 777 app/storage/sessions"
26
                execute :chmod, "-R 777 app/storage/views"
27
            end
28
        end
29
    end
30
31
end

You now have to put your data in every line with an #EDIT comment (ip address, git repo, ssh user, password, etc). The :deploy_to variable should be the folder we just created. Your webserver (Apache, Nginx, ...) should point to /var/www/my-app/current/public.

In the namespace :deploy block of the deploy.rb file you specify what actually should happen for each deploy. So there are two tasks. In the build task we install all your PHP dependencies, just like you're used to it during development. After that we make the artisan file executable in order to use it for migrations. In the restart task we fix the permissions for the storage folders.

All those tasks are invoked in the following order. You can hook into each task if you need to but for now we stick with our simple configuration.

1
2
deploy:starting    - start a deployment, make sure everything is ready
3
deploy:started     - started hook (for custom tasks)
4
deploy:updating    - update server(s) with a new release
5
deploy:updated     - updated hook
6
deploy:publishing  - publish the new release
7
deploy:published   - published hook
8
deploy:finishing   - finish the deployment, clean up everything
9
deploy:finished    - finished hook

Every deploy is stored in /var/www/my-app/releases/. The built-in task deploy:publishing creates a symbolic link of the recent deploy to the current folder. This way you can keep older releases and switch versions without going offline for a second. When this task ran, your newest version is online.

You can easily add your own tasks if your build process requires some extra steps. For more detailed information, I recommend you reading the official documentation.

After these basic configurations steps we are prepared for our first deploy.

Fire!

So that's the moment you were waiting for. The hardest part is done. For now on every time you want to deliver your application updates, you just have to run the following magical command. Capistrano will read your config/deploy.rb config/deploy/production.rb files and run each task. If a task fails, the deploy will stop and the old version is still online.

1
2
$ cap production deploy

You will see a bunch of text output and after little time (depending on your server) everything should be complete. That was easy, wasn't it?

Note: For now we just setup our production stage, but you could replace production with another stage, for example your testing server and run $ cap staging deploy.

Further Thoughts

Security

Perhaps you might be a little worried if you have to put your plaintext password in the configuration file. I just choosed that way to make the demonstration as straight forward as possible, but in the real world you might want to use a SSH key. You can import one like this:

1
2
set :ssh_options, {
3
    keys: %w("/path/to/your/key.pem"), # EDIT your ssh key

4
    auth_methods: %w(publickey)
5
}

Database

For now we have just focused on deploying the actual files to their new home but in many scenarios you might also do something with your database. Laravel has a perfect tool for that: migrations. You could just add a new step where you run these migrations. After that our build task might look like this:

1
2
desc "Build"
3
after :updated, :build do
4
    on roles(:app) do
5
        within release_path  do
6
            execute :composer, "install --no-dev --quiet" # install dependencies

7
            execute :chmod, "u+x artisan" # make artisan executable

8
            execute :php, "artisan migrate" # run migrations

9
        end
10
    end
11
end

You also have to add this task in the transaction block of the update task. Now everytime you deploy, the database will be updated to your latest migrations.

Rollback

Sometimes you deploy a non-working version of your application and you need to undo these changes. Capistrano has a built-in feature for that called "rollback". Just run:

1
2
$ cap production deploy:rollback

Conclusion

You've just learned a very simple way of deploying your application to your production server(s) with Capistrano. Once the configuration work is done, it just takes one command to deploy your latest version in seconds. But as mentioned earlier, this is not the only way to do this.

You should also check out the task runner grunt which suits perfectly for building and deploying JavaScript applications. A complete different approach takes docker which acts like a lightweight VM. The idea here is to deploy your whole environment as a virtual machine. Check them out!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.