Deploying Rails Applications

Deploying Rails Applications

What readers are saying about Deploying Rails Applications Deploying Rails Applications is a fantastic and vastly important book. Many thanks! Stan Kaufman Principal, The Epimetrics Group LLC I’ve used the section on setting up a virtual private server to get up and running on three different VPS instances in less than thirty min- utes each. Your book has saved me days of time preparing servers, letting me focus instead on writing code. Your book also has the best Capistrano tutorial I’ve ever read. It’s no longer a mystery, and I’m now writing custom deployment tasks. I can’t wait to get my final copy! Barry Ezell CTO, Balance Engines LLC Prior to buying this book, I had to spend hours scouring the Web to find this kind of information. Having it all in one place (and correct!) helped me deliver a successful Rails project. Thank you! Eric Kramer Programmer, Nationwide Children’s Hospital Deploying Rails Applications A Step-by-Step Guide Ezra Zygmuntowicz Bruce A. Tate Clinton Begin with Geoffrey Grosenbach Brian Hogan The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Ezra Zygmuntowicz, Bruce A. Tate, and Clinton Begin. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 0-9787392-0-5 ISBN-13: 978-09787392-0-1 Printed on acid-free paper with 50% recycled, 15% post-consumer content. Contents 1 Introduction 8 1.1 TheLay oftheLand ..................... 11 1.2 Finding a Home ....................... 13 1.3 Conventions ......................... 17 1.4 Acknowledgments ...................... 17 2 Refining Applications for Production 20 2.1 TheLay oftheLand ..................... 21 2.2 Source Code Management ................. 22 2.3 Subversion Tips ....................... 29 2.4 Stabilizing Your Applications ............... 31 2.5 Active Record Migrations .................. 34 2.6 Application Issues for Deployment ............ 38 3 Shared Hosts 44 3.1 TheLay oftheLand ..................... 44 3.2 Choosing a Shared Host .................. 46 3.3 Setting Up Your Domain and DNS ............ 49 3.4 Configuring Your Server .................. 51 3.5 Server Setup: Create a Database ............. 52 3.6 Installing Your Application ................. 53 3.7 Configuring Your Web Server ............... 56 3.8 Application Setup: Rails Config Files ........... 60 3.9 The Well-Behaved Application ............... 63 3.10 Troubleshooting Checklist ................. 64 3.11 Conclusion .......................... 71 4 Virtual and Dedicated Hosts 72 4.1 TheLay oftheLand ..................... 72 4.2 Virtual Private Servers ................... 75 4.3 Dedicated Servers ...................... 77 4.4 SettingUp Shop ....................... 78 4.5 Conclusion .......................... 90 CONTENTS 6 5 Capistrano 92 5.1 TheLay oftheLand ..................... 93 5.2 How It Works ......................... 95 5.3 Local and Remote Setup for Rails ............. 97 5.4 Standard Recipes ...................... 107 5.5 Writing Tasks ........................ 108 5.6 A Little Extra Flavor ..................... 118 5.7 Troubleshooting ....................... 121 5.8 Conclusion .......................... 123 6 Managing Your Mongrels 124 6.1 TheLay oftheLand ..................... 124 6.2 Training Your Mongrels .................. 124 6.3 Configuring the Watchdog ................. 131 6.4 Keeping FastCGI Under Control .............. 136 6.5 Building in Error Notification ............... 138 6.6 Heartbeat ........................... 142 6.7 Conclusion .......................... 143 7 Scaling Out 144 7.1 TheLay oftheLand ..................... 144 7.2 Scaling Out with Clustering ................ 145 7.3 Mirror Images ........................ 150 7.4 Domain Names and Hosts ................. 151 7.5 Deploying to Multiple Hosts ................ 154 7.6 Apache ............................ 159 7.7 nginx, from Russia with Love ............... 172 7.8 Clustering MySQL ...................... 179 7.9 Summary ........................... 191 8 Deploying on Windows 192 8.1 Setting Up the Server .................... 192 8.2 Mongrel ............................ 196 8.3 Mongrel and Pen ...................... 201 8.4 Using Apache 2.2 and Mongrel .............. 204 8.5 IIS Integration ........................ 209 8.6 Reverse Proxy and URLs .................. 211 8.7 Strategies for Hosting Multiple Applications . 213 8.8 Load-Testing Your Applications .............. 218 8.9 Final Thoughts ....................... 219 8.10 Developing on Windows and Deploying Somewhere Else 220 8.11 Wrapping Up ......................... 223 CONTENTS 7 9 Performance 224 9.1 TheLay oftheLand ..................... 224 9.2 Initial Benchmarks: How Many Mongrels? ........ 228 9.3 Profiling and Bottlenecks .................. 232 9.4 Common Bottlenecks .................... 237 9.5 Caching ............................ 242 9.6 Conclusion .......................... 252 10 Frontiers 254 10.1 Yarv .............................. 254 10.2 Rubinius ........................... 254 10.3 JRuby ............................. 256 10.4 IronRuby ........................... 256 10.5 Wrapping Up ......................... 257 A An Example nginx Configuration 258 B Bibliography 260 Index 261 Chapter 1 Introduction Building Rails apps brings the joy back into development. But I, Ezra, have a confession to make. There was a brief moment that I didn’t like Rails at all. I’d just graduated from the five-minute tutorial to developing my first real Rails application. The helpers, plug-ins, and generators reduced the amount of code I needed to write. The logical organization and lay- out of the files let me painlessly find what I needed, and the domain- specific languages in Active Record let me express my ideas with sim- plicity and power. The framework bowed to my will, and aside from a few trivial mistakes, I finished the app. Pure joy washed over me. But then, it was time to deploy. Deployment means moving your appli- cation from a development environment into a home that your cus- tomers can visit. For a web application, that process involves choosing a host, setting up a web server and database, and moving all your files to the right places with the right permissions. I quickly discovered that after the joy of development, deployment was a real drag. All those waves of euphoria completely disintegrated against the endless stream of crash logs, Rails error pages, and futile install scripts. I spent hours wading through the Rails wikis, blogs, and books for answers, but each one gave me a mere fragment of what I needed. Much of the information I found was contradictory or flat-out wrong. Deployment also involves making the best possible environment for your customers, once you’ve settled into your new home. There, too, I failed miserably. When I finally made my site work, it was too slow. Stumbling through page caching seemed to make no difference, and CHAPTER 1. INTRODUCTION 9 my end users watched the spinning (lack of) progress indicator in frus- tration. I struggled to fix memory leaks, broken database migrations, and worthless server configurations until eventually my site purred in appreciation. Then came success, which means more visitors, followed by more failure. I screamed some choice words that would make a sailor’s dead parrot blush. No, at that moment, I really didn’t like Rails. I’m not going to sugarcoat it. If you don’t know what you’re doing, Rails deployment can stretch the limits of your patience, even endurance. What’s worse, Rails deployment suffers especially in areas where Rails development is easy: • You can always find plenty of Rails development documentation, but when it’s time to deploy, you can often find only a fraction of what you need. People just seem to write more about development than deployment. • You can choose your development platform, but you can’t always choose your deployment platform. Most hosts with Rails support run some variant of Linux; others run FreeBSD or Solaris. And the software stack for different hosts can vary wildly, as can applica- tion requirements. • When your development application breaks, you can find moun- tains of information through breakpointing, rich development logs, and the console. In production, when things go south, there are fewer sources of information, more users, and more variables. You might encounter a problem with the operating system, your application server, system resources, plug-ins,

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    267 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us