{"id":498,"date":"2011-09-20T20:30:23","date_gmt":"2011-09-21T03:30:23","guid":{"rendered":"http:\/\/blog.networkpresence.co\/?p=498"},"modified":"2011-10-17T07:15:51","modified_gmt":"2011-10-17T14:15:51","slug":"how-to-setup-ruby-on-rails-hosting-using-apache-from-development-to-production","status":"publish","type":"post","link":"http:\/\/blog.networkpresence.co\/?p=498","title":{"rendered":"How to setup Ruby on Rails Hosting using Apache, from Development to Production"},"content":{"rendered":"<p>In addition to the &#8216;development <a href=\"http:\/\/netpr.es\/rorvps1\" title=\"Network Presence Ruby on Rails Appliance VPS\" target=\"_blank\">Ruby on Rails&#8217; environment and Appliance VPS<\/a> product, we&#8217;ve recently worked through the software requirements and dependencies of a &#8216;production&#8217; Ruby on Rails hosting environment and VPS.<\/p>\n<p>The following technical information outlines the broad sysadmin actions required to bring a &#8216;development&#8217; Ruby on Rails application online within an Apache webserver &#038; seperate VPS\/system for hosting the application to the Internet in a &#8216;production&#8217; setting (as opposed to running the application within &#8220;rails server&#8221; for testing).<\/p>\n<p>Some assumptions that &#8216;complicate&#8217; this deployment (complications which are dealt with in the actions outlined below) are that the development Ruby on Rails environment is Ruby 1.9.x and Rails 3.0.9.<br \/>\nThis effectively means that most of the easily installable Operating System packages for (say) Debian or Ubuntu Linux aren&#8217;t going to work out, in that most Linux-based operating systems install Ruby 1.8.x and an older version of Rails &#038; RubyGems, along with an older version of the Apache module that&#8217;s commonly used to present Ruby software to the web, being the &#8220;<a href=\"http:\/\/www.modrails.com\" target=\"_blank\">Passenger<\/a>&#8221; module. Finally, we&#8217;ll assume in the following that you&#8217;re hosting the web application on a 64 bit Debian 6 (&#8220;Squeeze&#8221; by name) VPS.<\/p>\n<p>So to &#8220;productionise&#8221; a Ruby 1.9.x and Rails 3.0.9 developed web application means that much software must be installed from source code or from the distributions made available from the software&#8217;s site itself, rather than just Operating System packages.<\/p>\n<p>Based on the above requirements and assumptions, here&#8217;s the process for getting the relevant software onto a &#8216;vanilla&#8217; Debian 6 system, with all commands run as the &#8216;root&#8217; Linux user:<\/p>\n<p>a) With a &#8216;minimal&#8217; Debian 6 64 bit (x86_64) system, install the following Debian Packages after upgrading the Debian 6 system to the latest version of &#8216;base&#8217; packages ie: an <code>apt-get upgrade<\/code><\/p>\n<p><code>apt-get install ruby1.9.1 libruby1.9.1<br \/>\napt-get install build-essential<br \/>\napt-get install apache2-prefork-dev<br \/>\napt-get install libapr1-dev<br \/>\napt-get install libaprutil1-dev apache2<br \/>\napt-get install libxslt-dev libxml2-dev<br \/>\napt-get install curl libcurl4-openssl-dev<\/code><\/p>\n<p>Then either (or both) of the following depending on what DB environment is required by the Ruby on Rails application:<\/p>\n<p><code>apt-get install libsqlite3-dev sqlite3<br \/>\napt-get install mysql-server libmysqlclient16 libmysqlclient-dev mysql-client mysql-common<\/code><\/p>\n<p>If you&#8217;re installing the mysql-server package, it will ask you to specify the mySQL &#8216;root&#8217; user&#8217;s password.<\/p>\n<p>The above yields &#8216;ruby&#8217; as the binary\/executable &#8216;\/usr\/bin\/ruby1.9.1&#8217;, so to have the command line &#8216;ruby&#8217; command, you can create a Linux &#8216;symlink&#8217; of \/usr\/bin\/ruby1.9.1 to \/usr\/bin\/ruby<br \/>\nie:<br \/>\n<code>ln -s \/usr\/bin\/ruby1.9.1 \/usr\/bin\/ruby<\/code><\/p>\n<p>b) Download &#038; install RubyGems from <a href=\"http:\/\/rubyforge.org\/projects\/rubygems\/\" target=\"_blank\">http:\/\/rubyforge.org\/projects\/rubygems\/<\/a> to the system, extract that file, cd into that extracted directory &#038; run &#8216;<code>ruby setup.rb<\/code>&#8216;<\/p>\n<p><code>cd \/usr\/local\/src\/<br \/>\nwget http:\/\/rubyforge.org\/frs\/download.php\/75309\/rubygems-1.8.10.tgz<br \/>\ntar xzf rubygems-1.8.10.tgz<br \/>\ncd rubygems-1.8.10<br \/>\nruby setup.rb<\/code><\/p>\n<p>Then, as above, if you want the &#8216;gem&#8217; command-line executable, then do:<\/p>\n<p><code>ln -s \/usr\/bin\/gem1.9.1 \/usr\/bin\/gem<\/code><\/p>\n<p>c) Use Gem to install the following Gems:<br \/>\n(If you don&#8217;t need mysql, then you may not need to install it)<\/p>\n<p><code>gem install mysql<br \/>\ngem install fastthread<\/code><\/p>\n<p>That gives you a software base to bring your Ruby on Rails application development directory over from your development environment to this system and copy that development directory tree to the directory where you&#8217;d like it served from on this production server. eg: \/var\/www\/app\/<\/p>\n<p>Once you&#8217;ve done so &#038; we&#8217;ll assume that it&#8217;s been copied to the directory \/var\/www\/app, you then should ensure that your app has a running environment compatible with this production environment, rather than the development environment, which in the case of <a href=\"http:\/\/blog.networkpresence.co\/?p=429\">the dev. environment we refer to in our previous post<\/a> (being Ruby 1.9.x, RSpec, Rails 3.0.9, etc) means that all RSpec Tests should pass. Again, as per the development RoR environment outlined, this is accomplished with the following actions:<\/p>\n<p>a) Remove the &#8216;rake&#8217; directory from the development environment and check your &#8216;Gemfile&#8217; to be sure that it lists any specific Gem version numbers that you may want.<\/p>\n<p><code>cd \/var\/www\/app\/<br \/>\nrm -rf rake<br \/>\nvi Gemfile<\/code><\/p>\n<p>b) Then run the <code>bundle install<\/code> command to install the Ruby on Rails environment compatible with this production system and also (crucially) all the Gems that your application needs, and that may take some time as it downloads &#038; installs the relevant Gems listed in your Gemfile.<\/p>\n<p>Of course, if your Gemfile is small or non-existant, you can iterate with &#8216;gem install <gemname>&#8216; and usually this will involve a minimum of:<\/p>\n<p><code>gem install rake<br \/>\ngem install rails --version=3.0.9<br \/>\ngem install sqlite3<\/code><br \/>\n&#8230;<\/p>\n<p>c) If you&#8217;re using RSpec (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Test_driven_development\" target=\"_blank\">which we recommend<\/a>), then all tests should run now without error:<\/p>\n<p><code>cd \/var\/www\/app<br \/>\nbundle exec rspec spec\/<\/code><\/p>\n<p>This gets your application working on the production system&#8217;s software environment, so now&#8217;s the time to get everything installed for Apache to be able to serve your Ruby on Rails application&#8217;s content.<\/p>\n<p>As above, we&#8217;ve already installed Apache2 and its Development environment, so we now need to install the Passenger Gem and compile the Passenger Apache2 module itself. This is done with the following:<\/p>\n<p><code>gem install passenger<\/code><\/p>\n<p>Which among its actions, installs the following executable command:<\/p>\n<p><code>\/usr\/bin\/passenger-install-apache2-module<\/code><\/p>\n<p>Running &#8220;passenger-install-apache2-module&#8221; steps through the process of validating your software environment (that it&#8217;s able to build the Passenger module etc &#038; the common pre-req&#8217; software needed is installed initially above from Debian\/Ubuntu Linux Packages) and goes through the process of downloading the latest\/relevant Passenger Module&#8217;s code and building and installing it.<\/p>\n<p>The final output of &#8216;passenger-install-apache2-module&#8217; looks something like:<\/p>\n<blockquote><p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nThe Apache 2 module was successfully installed.<\/p>\n<p>Please edit your Apache configuration file, and add these lines:<\/p>\n<p>   LoadModule passenger_module \/usr\/lib\/ruby\/gems\/1.9.1\/gems\/passenger-3.0.9\/ext\/apache2\/mod_passenger.so<br \/>\n   PassengerRoot \/usr\/lib\/ruby\/gems\/1.9.1\/gems\/passenger-3.0.9<br \/>\n   PassengerRuby \/usr\/bin\/ruby1.9.1<\/p>\n<p>After you restart Apache, you are ready to deploy any number of Ruby on Rails<br \/>\napplications on Apache, without any further Ruby on Rails-specific<br \/>\nconfiguration!<\/p>\n<p>Deploying a Ruby on Rails application: an example<\/p>\n<p>Suppose you have a Rails application in \/somewhere. Add a virtual host to your<br \/>\nApache configuration file and set its DocumentRoot to \/somewhere\/public:<\/p>\n<p>   <VirtualHost *:80><br \/>\n      ServerName www.yourhost.com<br \/>\n      DocumentRoot \/somewhere\/public    # <-- be sure to point to 'public'!\n      <Directory \/somewhere\/public><br \/>\n         AllowOverride all              # <-- relax Apache security settings\n         Options -MultiViews            # <-- MultiViews must be turned off\n      <\/Directory><br \/>\n   <\/VirtualHost><\/p>\n<p>And that&#8217;s it! You may also want to check the Users Guide for security and<br \/>\noptimization tips, troubleshooting and other useful information:<\/p>\n<p>  \/usr\/lib\/ruby\/gems\/1.9.1\/gems\/passenger-3.0.9\/doc\/Users guide Apache.html<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p><\/blockquote>\n<p>These broad\/general instructions require the following actions in a Debian Apache2 context:<\/p>\n<p>a) Put the string &#8220;LoadModule passenger_module \/usr\/lib\/ruby\/gems\/1.9.1\/gems\/passenger-3.0.9\/ext\/apache2\/mod_passenger.so&#8221; into the file <code>\/etc\/apache2\/mods-available\/passenger.load<\/code><\/p>\n<p>b) Put the following lines into <code>\/etc\/apache2\/mods-available\/passenger.conf<\/code> file<\/p>\n<p><code>PassengerRoot \/usr\/lib\/ruby\/gems\/1.9.1\/gems\/passenger-3.0.9<br \/>\nPassengerRuby \/usr\/bin\/ruby1.9.1<br \/>\n# see all passenger module directives here: http:\/\/www.modrails.com\/documentation\/Users%20guide.html<br \/>\nPassengerDefaultUser www-data<br \/>\n<\/code><\/p>\n<p>*Note: Updated to correctly set the user that the Rails App runs as by Apache using &#8220;PassengerDefaultUser&#8221; directive, otherwise the Rails App runs as user &#8216;nobody&#8217;, not the user that the Apache is running as!<\/p>\n<p>c) Run (as root) to enable the configuration &#038; settings for the Apache2 Passenger Module in the Debian\/Ubuntu context:<\/p>\n<p><code>a2enmod passenger<\/code><\/p>\n<p>Now you&#8217;re ready to just configure Apache to serve that directory (\/var\/www\/app in our example here) and there&#8217;s many ways to do that, but we&#8217;ll just outline using the defaults of Debian&#8217;s Apache2 package&#8217;s installed files here.<\/p>\n<p>a) Load the following to the top of: <code>\/etc\/apache2\/sites-available\/default<\/code><\/p>\n<p><code><VirtualHost *:80><br \/>\n        ServerAdmin webmaster@YOURDOMAIN<\/p>\n<p>        ##DocumentRoot \/var\/www<br \/>\n      DocumentRoot \/var\/www\/app\/public<br \/>\n   # <-- be sure to point to 'public'!\n      <Directory \/var\/www\/app\/public><br \/>\n         AllowOverride all<br \/>\n             # <-- relax Apache security settings\n         Options -MultiViews\n           # <-- MultiViews must be turned off\n      <\/Directory><\/code><\/p>\n<p>b) Restart Apache with that config:<\/p>\n<p><code>\/etc\/init.d\/apache2 restart<\/code><\/p>\n<p>c) Check for any Apache startup errors in <code>\/var\/log\/apache2\/error.log<\/code> of which there should just be the usual Apache startup line mentioning the use of the Passenger module, like:<\/p>\n<p><code>[notice] Apache\/2.2.16 (Debian) Phusion_Passenger\/3.0.9 configured -- resuming normal operations<\/code><\/p>\n<p>Now, if you use a SQL Lite DB in your application during development, then you should move the sqlite3 DB to a &#8216;production&#8217; mode, which is done either &#8220;by the book&#8221; with the command:<\/p>\n<p><code>cd \/var\/www\/app<br \/>\nrake db:create RAILS_ENV=production<\/code><\/p>\n<p>Which will create an empty &#8216;production&#8217; DB.<\/p>\n<p>Or, you could just rename your &#8216;development&#8217; DB file to the &#8216;production&#8217; filename with:<\/p>\n<p><code>cd \/var\/www\/app<br \/>\ncp db\/development.sqlite3 db\/production.sqlite3<\/code><\/p>\n<p>This will preserve any DB contents from development into the production DB (which may or may not be what you want).<\/p>\n<p>If you use mySQL or such for your DB requirements, then now&#8217;s the time to ensure that your DB is available to your application on the production system (eg: use mysqldump or such to transfer the relevant DB over to the mysql server running on this production host).<\/p>\n<p>If you don&#8217;t do this, you may get the good &#8216;ole RoR Passenger error page of &#8220;Something went wrong&#8221; and if so, there may be nothing listed in the Apache error.log file, but you&#8217;ll see updated files in your app&#8217;s log\/ directory, so check <code>\/var\/www\/app\/log\/production.log<\/code><\/p>\n<p>That&#8217;s about it, this process has reliably put a number of customer Ruby 1.9.x on Rails 3.0.9 externally developed web applications online to the Internet via Apache webservers on Debian 6 Linux.<\/p>\n<p>As always, feel free to <a href=\"http:\/\/netpr.es\/contactus\" target=\"_blank\">contact us<\/a> as a customer if you have any queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In addition to the &#8216;development Ruby on Rails&#8217; environment and Appliance VPS product, we&#8217;ve recently worked through the software requirements and dependencies of a &#8216;production&#8217; Ruby on Rails hosting environment and VPS. The following technical information outlines the broad sysadmin &hellip; <a href=\"http:\/\/blog.networkpresence.co\/?p=498\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[94,95,93,92,96,78,9],"class_list":["post-498","post","type-post","status-publish","format-standard","hentry","category-network-presence","tag-apache","tag-passenger","tag-rails","tag-ruby","tag-sql","tag-sysadmin","tag-vps"],"_links":{"self":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=498"}],"version-history":[{"count":12,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/498\/revisions"}],"predecessor-version":[{"id":564,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/498\/revisions\/564"}],"wp:attachment":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}