How to sell online books

How to Deploy Rails Applications

Updated in 2021, learn how to make deploying a Rails application to your own VPS as easy as deploying to Heroku. Includes full sample code for Chef & Capistrano, regularly updated and tested on Linode & Digital Ocean. "Quickstart" chapters allow you to have a server running a Rails application within an hour.

    eBook includes PDF, ePub and Kindle versions

    Whats Inside?

    An introduction to the structure of the book, the philosophy behind deploying web applications robustly and which parts it's essential to read depending on your goals.

    A brief look at why using a tool to automate the setting up of servers is important and some high level terminology we'll encounter when using this books automation tool of choice; Chef.

    A deeper dive into Chef and Chef Solo, the terminology associated with it and how to create a Chef project from scratch. Think `rails new` but for server configurations.

    First a look at the concept of a "node definition", a json file which defines everything about a server to be created. Then a look at how Chef allows us to create re-usable pieces of functionality which can then be applied to multiple servers or re-used in future projects using "roles".

    For Rails we have bundler for making sure the correct dependencies are installed, for Chef we have Berkshelf which performs exactly the same function. With Berkshelf we can make sure we have all of the required "cookbooks" for setting up a server, making it easy to mix leveraging community written cookbooks with functionality we've developed ourselves.

    Starting off with a look at some common security gotchas when deploying to a fresh VPS. Then some more detail around how to lock down SSH access, manage firewall rules and automatically set up users and public keys.

    When something crashes, in a perfect world, it will fix itself. This chapter covers how to use Monit to automatically monitor the health of services and restart them when they fail. In the event this fails, how to have Monit alert us via email so we can intervene and save the day.

    How to setup PostgreSQL, manage the different types of authentication, ensure we have the desired version and manage importing and exporting data.

    A quick look at when MongoDB is an appropriate data store then how to ensure we have the correct version installed and workaround any incompatibilities in community cookbooks for newer versions. Finally a quick introduction to importing and exporting databases.

    Like chapter 4, a chapter for those of us who prefer to read the instructions later. This chapter covers the minimum steps need to deploy an existing Rails application to our fresh new server.

    Our goal is to automate everything about interacting with our server. Eventually there will be something we need to do which no-one else has automated in a way we can re-use. This chapter covers writing custom Capistrano tasks. Once we've mastered this we can automate almost every interaction with our server imaginable.

    A deeper look at Nginx virtualhosts which control where requests are routed after they reach NGinx. Then a look at how to setup SSL and manage certificate rotation.

    It's an unfortunate fact that eventually a server will fail. In this chapter we setup automatic database backups to Amazon S3 (or any other number of destinations) to ensure that in the event of a complete server failure, our data is recoverable.

    An overview of the components which will make up our production Rails configuration, their purpose and a high level look at the rationale between choosing each one.

    A chapter for the "read the instructions afterwards" types among us. This chapter provides step by step instructions for setting up a fully working, reproducible Rails server using the books sample code and a few simple commands.

    A detailed guide to how to create custom Chef cookbooks. Cookbooks are like gems but for re-usable bits of functionality on a server. We'll begin with the commands we'd type in by hand to install a piece of software on the server and convert this into the simplest possible cookbook to automate the process. We'll then iterate on this to take advantage of some of Chefs more powerful features.

    A more detailed introduction to the sample configuration which was used in Chapter 4's quick-start to setup a fully working server in just a few minutes.

    The bare bones components needed on any server, how - and when - to setup automatic package updates, how to have the systems time keep automatically updated and why so many people get caught out by locales.

    Making sure we have some basic packages and development headers required for installing common gems then a more detailed look at how to install Ruby and make sure we have the correct version available to our Rails application.

    Nginx will be the first port of call when a request comes in. How to setup virtual hosts, serve static assets and a look at why we need Nginx at all.

    How to setup MySQL, manage the different types of authentication and ensure we have the required components installed for the MySQL gem. Finally a quick introduction to importing and exporting data.

    A brief chapter on the two simplest parts of our stack. How to install Redis and Memcached, configuring whether or not they're bound to localhost and managing the maximum size they can reach.

    A much more detailed introduction to how to deploy to our new server with Capistrano 3 whilst using no none-core Capistrano gems. Includes a brief look at the difference between versions 2 and 3 and a detailed section on managing server specific configuration files.

    How to configure deployment so that when we deploy, there's a seamless switch from one version to the next, with no gap while the new version starts. Includes a detailed section on causes of potential problems when setting this up and how to troubleshoot if it doesn't work reliably.

    Sidekiq is one of the most popular and efficient background job processing libraries in the Rails ecosystem. Here we cover how to integrate starting and stopping Sidekiq workers as part of the deployment process and how to setup monitoring so they'll be automatically restarted in the event of a failure.

    "Ben's book 'Reliably Deploying Rails Applications' helped me setting up my Rails production app comuvo.com in no time. While there's a lot of fragmented and potentially misleading information about Chef and Capistrano out there, I feel his book is a proper and complete guide through the process of provisioning a server and deploying your app."

    Jonas Jäger

    Founder, Comuvo.com

    One of the few books on deploying Rails applications containing sample code which worked out of the box. I purchased through Leanpub so I get notified of revised versions and the book + sample code seem to be updated every few months to keep pace with the latest stable releases of both Rails and tools.

    Katherine Cook

    Rails Developer

    About Ben Dixon

    author-photo

    I've been developing web applications for over fifteen years, over the last few years specialising in Rails development and deployment as well as Elixir and Kubernetes.

    I'm co-founder & CTO of Catapult a venture backed global SaaS platform for maximising worker engagement. Previously I was the technical lead at a health and fitness startup who provide the timetabling for many of the UK's leisure operators as well as producing a globally recognised iOS app (Speedo Fit) for swimmers.

    As part of these projects I've dealt with everything from the usual rapid growth from 10's of requests per minute to 10's per second to more unusual challenges such as expanding infrastructure into China and debugging obscure indexing issues.

    I spoke about deploying Rails applications at Railsconf Chicago and integrating Docker with Rails in Atlanta. I also spoke at Refresh! conf in Tallinn about common mistakes people make when deploying and scaling web applications.