Hosting a Rails App


Published on

Presentation for CleRB 06-05-2013

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Hosting a Rails App

  1. 1. 06.04.2013Hosting A Rails App
  2. 2. Why am I presenting this?• Level Seven• Support a bunch ofproduction rails apps• Biggest has between 20-30 boxes of various types• Still learning
  3. 3. We’re Hiring!• Ya i know, who isn’t?• Full time ruby on rails• Happy to help someone pickup their first real rails gig• Also we like contractors andindependents
  4. 4. Hosting Options• Heroku• Great for new/youngprojects• Easy to manage• Gets expensive quick• Pretty inflexible, very littlecontrol• AWS• More complicated setup• Can also get expensive buteasier to control (YMMV)• Awesome set of tools that allplay well together• You need to know or be asysadmin
  5. 5. Free Micro Instance• Everything in this presentation you can try out for yourself for free• Amazon Free Tier• Free Micro EC2 Instance, 1 static IP, 30 gig EBS space and a loadbalancer plus a bunch of other stuff•• Any code / scripts will be on github and these slides will be onslideshare. I’ll tweet their location after the talk.• I have a bunch of hidden slides that screenshot the amazon setupprocess too
  6. 6. Step 0: Assumptions & Spinup• We’ll be using an Ubuntu 12.01 instance. It’s a default in AWS.• This guide is really Amazon stack heavy. I’ve worked with it, herokuand rackspace (a little) so if you have questions I’m happy to try andhelp.• You should setup an elastic IP on your server right away for easy ofuse, but I’ll talk about it later on.
  7. 7. Step 0: Assumptions & Spinup• First spinup a new instance on EC2 using Ubuntu 13.04• Ideally add a 2nd EBS volume with additional storage space• Associate an Elastic IP with that box• Setup a new Host record in ~/.ssh/config for the new serverHost ClerbDemoHostName ~/.ssh/AWSJosh.pemForwardAgent yes• That last line will be important later
  8. 8. Step 1 - Dependencies• Install the various libraries and dev tools you’ll need to install the restof the server> sudo apt-get -y update #update apt to the latest packages> sudo apt-get -y install build-essential zlib1g-dev libssl-devlibreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core python-software-properties libxslt-dev libxml2-dev imagemagick pdftkopenjdk-7-jre-headless• Install NodeJS for Asset Pipeline> sudo apt-add-repository -y ppa:chris-lea/node.js> sudo apt-get -y update> sudo apt-get -y install nodejs
  9. 9. Step 1 - Dependencies• Make your life better by disabling rdoc and ri installation for gems> sudo echo “gem: --no-ri --no-rdoc” >> ~/.gemrc> echo “gem: --no-ri --no-rdoc” >> ~/.gemrc• Set your timezone (optional, maybe you want UTC)>sudo dpkg-reconfigure tzdata #We use America/New York
  10. 10. Step 2 - Ruby• 2 Ways to install, from source or via a system-wide RVM install• RVM is really only useful if you plan on having multiple rubies• Passenger 4 supports this and may result in me changing mydefaults pretty soon• For now we’ll go with source> wget> tar -xzvf ruby-2.0.0-p0.tar.gz> cd ruby-2.0.0-p0> ./configure> make> sudo make install• dd
  11. 11. Step 3 - Web Server• Lots of choices. Apache or Nginx w/ Passenger, Thin, Unicorn, etc.• We’ll go with Apache w/ Passenger• I REALLY like nginx but I haven’t used it enough yet> sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev> sudo gem install passenger> sudo passenger-install-apache2-module# Follow instructions on screen
  12. 12. Step 3 - Web Server• Setup the modules you’ll need> sudo a2enmod ssl> sudo a2enmod rewrite> sudo a2enmod headers> sudo a2enmod expires> sudo a2enmod passenger• We’ll need a virtual host for the site but we’ll do that later once wehave the code out there for the first time• I like to setup the folder paths now though> sudo mkdir /srv/www> sudo chown ubuntu /srv/www> sudo chgrp ubuntu /srv/www
  13. 13. Step 4 - Database• Tons of options, popular ones include Postgres, MySQL for RDBMSor MongoDB as NoSQL Alternative• If you like MySQL you could use Amazon’s RDS Service• We’ll go with PSQL for now> sudo touch /etc/apt/sources.list.d/pgdg.list> sudo sh -c echo "deb main" >> /etc/apt/sources.list.d/pgdg.list>wget --quiet -O - | sudo apt-keyadd -> sudo apt-get -y update> sudo apt-get -y install postgresql-9.2• If your using a different DB and App server you have to modify thepostgres config to allow remote connections
  14. 14. Step 5 - DNS & IP Addresses• For AWS your server will need an elastic IP• You’ll need to setup that address in DNS, lots of options here:• I recommend either Amazon’s Route 53 or DNSimple• Both have great APIs, allow a lot of flexibility and aren’t Godaddy• Setup an A Record to point to the IP address you got from amazon
  15. 15. Deployment• Most rails apps on custom servers use capistrano•• In short add capistano to your gemfile, bundle install, then:> capify .• Update the config/deploy.rb with your server info and path you wantthe code
  16. 16. Deployment• cap deploy:setup to create folder structure• cap deploy:check to verify• At this point you should make sure you database.yml file is on theserver in the proper place• cap deploy• cap deploy:migrations to deploy and run all migrations
  17. 17. Deployment• You need a custom cap task for running asset pipeline generation• You can support multiple environments usingcapistrano/ext/multistage• I like to build a cap task to copy the real db config into my projectpost-deploy and not store production un/pw info in git
  18. 18. Stuff I Forgot Earlier• You need to create a vhost in /etc/apache2/sites-available• Also make sure your security group allows traffic on port 80
  19. 19. Stuff I didn’t cover• Failover including load balancing• Caching solutions like Varnish or advanced nginx caching• Proxy server configurations• Database replication or failover• Backup• Lots of other stuff...
  20. 20. References• My Demo App•• The Server Setup Script•• These Slides• Some crazy slideshare url
  21. 21. Questions & Contact• @JoshReedSchramm•