170 likes | 295 Views
Rails Hosting and Deployment. Action Items. Choosing a Linux variety for your server Selecting a hosting solution for your server Building your server Installing and configuring your web server Clustering and load balancing your application servers Monitoring your application server
E N D
Action Items • Choosing a Linux variety for your server • Selecting a hosting solution for your server • Building your server • Installing and configuring your web server • Clustering and load balancing your application servers • Monitoring your application server • Automating deployment • Deploy!
Linux Varieties • Choosing a Linux variety with a great community support around Ruby and Rails is important • Debian based and Red Hat Linux are of some the most common choices amongst the Rails community • Debian based and Red Hat Linux have excellent package managers that ease server setup without having to compile necessary libraries from source • Forums are abundant with information and troubleshooting advice • Other Linux varieties include CentOS, Gentoo, SUSE, Mandrake • All working examples will be with Ubuntu
Hosting Solutions • Virtualized server solutions are a very attractive and cost effective way to host your Rails app • Check out Slicehost and EngineYard • Amazon Elastic Cloud Computing (EC2) as an option has the best scalability for firing up additional server instances as needed in an affordable way • Ubuntu has an excellent forum focused on EC2 setup • Wide variety of Linux distributions are available in AMI form (Amazon Machine Image) • Dedicated servers are still the most common and familiar way to go
Server Setup Part 1 Ruby and Database(s) sudo apt-get install build-essential sudo apt-get install ruby rirdoc ruby1.8-dev irb1.8 libnet-daemon-perllibplrpc-perl libreadline-ruby1.8 libruby1.8 rdoc1.8 ri1.8 ruby1.8 irblibopenssl-ruby libopenssl-ruby1.8 psmisc sudo apt-get install mysql-server libmysql-ruby libdbd-mysql-perllibdbi-perl libmysql-ruby1.8 libmysqlclient15off libmysqlclient-dev mysql-client-5.0 mysql-common mysql-server-5.0 sudo apt-get install libsqlite3-dev sqlite3 libsqlite3-ruby sudo apt-get install openssllibssl-dev
Server Setup Part 2 Gems • Unpack your gems into your application to ensure that none are forgotten, only those with C bindings need to be installed on the server • environment.rb should define those that are specific to application and throw an error if not available • rake -T wget http://rubyforge.org/frs/download.php/56227/rubygems-1.3.3.tgz sudoln -s /usr/bin/gem1.8 /usr/local/bin/gem sudoln -s /usr/bin/ruby1.8 /usr/local/bin/ruby sudoln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc sudoln -s /usr/bin/ri1.8 /usr/local/bin/ri sudoln -s /usr/bin/irb1.8 /usr/local/bin/irb sudo gem install rails --no-rdoc --no-ri sudo gem install mongrel --no-rdoc --no-ri sudo gem install mongrel_cluster --no-rdoc --no-ri sudo gem install mysql --no-rdoc --no-ri sudo gem install sqlite3-ruby --no-rdoc --no-ri
Server Setup Part 3 Web Server • Many options are available for web servers including • Apache • LightHttp • Nginx • We are going to setup Nginx which has become the preferred web server for Rails applications sudo aptitude install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base Build it from source wgethttp://sysoev.ru/nginx/nginx-0.6.36.tar.gz ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module make sudo make install
Server Setup Part 4 SSH / SCP / SFTP enabled We need to ensure SSH is enabled on the target deployment server(s) sudo apt-getopenssh-server openssh-client
Web Server Configuration sudo vi /etc/init.d/nginx Article for creating the nginx start/stop script: http://articles.slicehost.com/2009/3/4/ubuntu-intrepid-adding-an-nginx-init-script Try: sudo /etc/init.d/nginx start sudo /etc/init.d/nginx stop sudo /etc/init.d/nginx restart
More Web Server Configuration • Debian like folder configuration with symlinks => sites-available and sites-enabled • Document root and GZIP’ing your static files • Re-write rules • SSL certificates • Load balancing your application servers with nginx upstream
Mongrel Clustering • Create a mongrel directory under your config folder • Each environment that your application deploys to should have a “mongrel cluster” YAML • Configure • mongrel_railscluster::configure • Start up • mongrel_railscluster::start -C /home/railsuser/demo/current/config/mongrel/production.yml • Shut down • mongrel_railscluster::stop -C /home/railsuser/demo/current/config/mongrel/production.yml
Mongrel Load Balancing with Nginx • Nginx provides a load balancing module called “upstream” • Works in a round robin distribution • Pros and cons • Offset with additional software called haproxy or load balancer hardware upstream demo_cluster { server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; server 127.0.0.1:3003; }
Monitoring your Application • monit is a very popular and configurable monitoring solution • sudo apt-get install monit • sudo vi /etc/monit/monitrc • God is alternative monitoring solution monit example confiiguration: check process app-mongrel-5200 with pidfile /home/railsuser/demo/shared/tmp/mongrel.3000.pid start program = "/usr/bin/mongrel_railscluster::start -C /home/railsuser/demo/current/config/mongrel/production.yml --clean --only 3000" as uidrailsuser and gidrailsuser stop program = "/usr/bin/mongrel_railscluster::stop -C /home/railsuser/demo/current/config/mongrel/production.yml --clean --only 3000" as uidrailsuser and gidrailsuser if totalmem is greater than 100.0 MB for 2 cycles then restart if failed port 5200 protocol http # check for response with timeout 10 seconds then restart if cpu is greater than 80% for 3 cycles then restart if cpu is greater than 50% for 2 cycles then alert group app-mongrel
Deployment • Capistrano is still the most widely used Rails deployment framework • Vlad the Deployer is rising in usage and is very similar to Capistrano • For Jruby projects that need to bundled in a WAR, Warbler is an excellent tool • You can get creative with traditional tools like Ant, Maven, and Makefiles but not recommended
Capistrano Installation Execute this on the server(s) your deploying from: sudo gem install capistrano Other gem dependencies include: sudo gem install net-scp sudo gem install net-ssh sudo gem install net-ssh-gateway sudo gem install net-sftp Execute this at the root of your Rails stack: capify
Capistrano Tasks Default commands are usually enough for the simplest Rails Applications Need a new script called script/spin Custom tasks need to be created for: monit, various deployment environments (test, staging, etc) cap deploy:setup cap deploy:check cap deploy:update cap deploy:start, stop, restart cap deploy