{"id":160,"date":"2008-03-27T10:46:00","date_gmt":"2008-03-27T18:46:00","guid":{"rendered":"http:\/\/onehub.com\/blog\/posts\/setting-up-a-rails-stack-with-nginx-and-clustered-mongrels-2"},"modified":"2008-03-27T10:46:00","modified_gmt":"2008-03-27T18:46:00","slug":"setting-up-a-rails-stack-with-nginx-and-clustered-mongrels-2","status":"publish","type":"post","link":"https:\/\/www.onehub.com\/blog\/2008\/03\/27\/setting-up-a-rails-stack-with-nginx-and-clustered-mongrels-2\/","title":{"rendered":"Setting up a rails stack with nginx and clustered mongrels"},"content":{"rendered":"<p><strong>Linux, <a href=\"http:\/\/mysql.com\/\">MySQL<\/a>, <a href=\"http:\/\/nginx.net\/\">nginx<\/a>, <a href=\"http:\/\/rubyforge.org\/projects\/mongrel\/\">Mongrel<\/a> , <a href=\"http:\/\/rubyonrails.org\/\">Rails<\/a>, and friends.<\/strong><\/p>\n<h4>Big Picture:<\/h4>\n<p>An ideal setup uses two different servers or virtual machines to split up the load and facilitate scaling. The first machine runs nginx serving static content and acting as a reverse proxy passing dynamic requests to a pool of mongrels running the rails application. The second machine is a dedicated MySQL database server. Even if you only have one physical box, two virtual machines (Xen based) is preferred, it is easier to scale and doesn\u2019t require as much memory context switching. The hosting provider we picked to run our service, Engine Yard, uses a similar setup based on Gentoo and Xen.<\/p>\n<h4>Linux Setup:<\/h4>\n<p>All of the tools and applications used in this stack are actively developed and available in most package management systems. This document will assume you use Debian\/GNU Linux, but will work equally well on Ubuntu. If choose to use Fedora, Gentoo or another distribution, you will need substitute commands for your package-management tool.<\/p>\n<p>Installing and configuring Linux is outside the scope of this document, but you should strive for a minimalist system based on a recent release of the 2.6 kernel. Debian and Ubuntu both offer a number of \u201cpre-made\u201d configurations in their installers, just avoid installing anything more than the base system as it will just add more software that you will have to disable or remove later.<\/p>\n<h4>Conventions:<\/h4>\n<p><code>$<\/code> indicates a standard shell prompt.<\/p>\n<p><code>#<\/code> indicates the command needs to be run as the root user, either by switching to the root user account or assuming root privileges with a tool like <code>sudo<\/code>.<\/p>\n<h4>Tools:<\/h4>\n<p>While installing and managing a Linux server, you will often need the power of the root account. However, because the root user is so powerful, it dangerous to use all the time. A better solution is to use a standard user account and only assume the privileges of the root user when necessary. Sudo was designed to do exactly this.<\/p>\n<p><code># apt-get install sudo<\/code><\/p>\n<p>Once the package is installed, the list of users able to assume root powers is controlled by <code>\/etc\/sudoers<\/code>. This file must be edited with the <code>visudo<\/code> command, which will open your default editor (or vi if your shell does not have an editor set).<\/p>\n<p><code># visudo<\/code><\/p>\n<p>If you are unfamiliar with vi, press \u2018i\u2019 to put it in insert mode, then add a line to enable your user.<\/p>\n<p><code>username ALL=(ALL) ALL<\/code><\/p>\n<p>You can read more on the sudo package to better understand this configuration file. Once you have completed your edit, press escape (ESC) to exit editor mode. To save the file type \u2019:\u2019 to enter command mode and then type \u2018w\u2019 and \u2018q\u2019 (meaning write and quit). The file should be properly saved and your user will now be able to assume root privileges.<\/p>\n<h4>Database Server:<\/h4>\n<p>MySQL is our database of choice, it is popular in the Rails community, proven to scale, and fast (especially in a read heavy environment).<\/p>\n<p>To install, use the mysql-server package.<\/p>\n<p><code># apt-get install mysql-server<\/code><\/p>\n<p>Your package manager should handle all the dependencies, including the mysql-client package, which we will use to improve our Ruby to MySQL performance. The installer may prompt you for a few questions, aim to accept the defaults and enter in information specific to your server.<\/p>\n<h4>Database Configuration:<\/h4>\n<p>The configuration file for MySQL is usually located in <code>\/etc\/mysql as my.cnf<\/code>. Most MySQL packages also include a number of other simple configurations to help MySQL perform better. Take a look at <code>my-large.cnf<\/code> and <code>my-small.cnf<\/code> in <code>\/usr\/share\/mysql<\/code> and update your <code>\/etc\/mysql\/my.cnf<\/code> to reflect your hardware or virtual machine\u2019s configuration. MySQL tuning is a complex subject, but some basic configuration can go a long way to helping the application to perform well.<\/p>\n<p>Before adding any databases, it is also important to instruct MySQL to use more international character encodings and collations. Storing the data in this way will make internationalization a simpler task in the future.<\/p>\n<p>In <code>\/etc\/mysql\/my.cnf<\/code>:<\/p>\n<pre><code>[client]\ndefault-character-set = utf8[server]\ndefault-character-set = utf8\ndefault-collation = utf8_general_ci<\/code><\/pre>\n<p>These directives instruct MySQL to use <span class=\"caps\">UTF<\/span>-8 for everything. If you have not already, restart your database server.<\/p>\n<p><code># \/etc\/init.d\/mysql restart<\/code><\/p>\n<p>You can now add the database to the server.<\/p>\n<p><code>$ mysqladmin -u root create database_name<\/code><\/p>\n<p>Consult the MySQL documentation to change the root user\u2019s password and to add a user with privileges on the specific database. It may be simpler to use a MySQL administration <span class=\"caps\">GUI<\/span> to achieve these tasks.<\/p>\n<h4>Application Server<\/h4>\n<p>The application server requires the most complex setup.<\/p>\n<h5>Ruby &amp; Ruby Gems<\/h5>\n<p>To start, you will need <a href=\"http:\/\/www.ruby-lang.org\/\">Ruby<\/a> and <a href=\"http:\/\/www.ruby-lang.org\/\">Ruby Gems<\/a>.<\/p>\n<p><code># apt-get install ruby rubygems<\/code><\/p>\n<p><code>ruby<\/code> is a transitional package, currently it will install Ruby 1.8.6 (as of March 20, 2008), unless you know of a specific feature or bug to avoid, it is simplest to let the the package-management system handle which version you get. <a href=\"http:\/\/rubyforge.org\/projects\/rubygems\/\">Ruby Gems<\/a> is a package management system for system for third party ruby libraries, it functions much like <a href=\"http:\/\/www.cpan.org\/\"><span class=\"caps\">CPAN<\/span><\/a> for <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> or <a href=\"http:\/\/www.perl.com\/\"><span class=\"caps\">PEAR<\/span><\/a> for <a href=\"http:\/\/php.net\/\"><span class=\"caps\">PHP<\/span><\/a>. We\u2019re going to want a number of gems, but first lets update gems itself, from within itself\u2014how meta.<\/p>\n<p><code># gem update \u2014system<\/code><\/p>\n<p>Ruby gems should now download and recompile itself. Once that has completed, you can install the gems we\u2019ll need. The first one is a bit trickier than the others.<\/p>\n<p><code># gem install mysql<\/code><\/p>\n<p>The <code>mysql<\/code> gem provides fast C bindings between Ruby and MySQL, if you aren\u2019t already using these on your local machine you should give them a spin, the performance increase is well worth the minimal amount of effort required to install them. If you have your MySQL installed in a non-standard location, you may have to pass in <code>mysql_config<\/code>.<\/p>\n<p><code># gem install mysql \u2013\u2013 \u2013\u2013with-mysql-config=\/your\/path\/to\/mysql_config<\/code><\/p>\n<p>With <code>mysql<\/code> all setup and out of the way, its easy to install everything else you need.<\/p>\n<p><code># gem install rails mongrel mongrel_cluster<\/code><\/p>\n<p><code>rails<\/code> is optional, you may choose to freeze rails inside your application, which would override this gem. <code>mongrel<\/code> and <code>mongrel_cluster<\/code> are the gems that provide us with our application server. Before diving into mongrel, there are a few more gems you may want that are entirely optional.<\/p>\n<p><code># gem install tmail swiftiply<\/code><\/p>\n<p><code>tmail<\/code> is an e-mail library, and is the same library that the <code>ActiveMailer<\/code> component of Rails uses. Rails 2.0 now looks for this gem before loading its own bundled copy; by installing the gem you can get extra features and bug fixes that may have been released.<\/p>\n<p><code>swiftiply<\/code> is a gem that patches mongrel to use the event based network programming model, commonly called \u201cevented mongrels\u201d. You can find more on <a href=\"http:\/\/brainspl.at\/articles\/2007\/05\/12\/event-driven-mongrel-and-swiftiply-proxy\">Ezra Zygmuntowicz\u2019s Brainspl.at<\/a> and <a href=\"http:\/\/journal.andrewloe.com\/\">my journal<\/a> where I did some <a href=\"http:\/\/journal.andrewloe.com\/2007\/05\/22\/mongrel-vs-evented-mongrel\/\">elementary benchmarking<\/a>.<\/p>\n<h4>Mongrel<\/h4>\n<p><a href=\"http:\/\/rubyforge.org\/projects\/mongrel\/\">Mongrel<\/a> will serve as our application server, containing an instance of the Ruby interpreter and our Rails application.<\/p>\n<blockquote><p>Mongrel is a fast <span class=\"caps\">HTTP<\/span> library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain <span class=\"caps\">HTTP<\/span> rather than FastCGI or <span class=\"caps\">SCGI<\/span>.<\/p><\/blockquote>\n<p>From inside your rails application you can start a mongrel server and interact with your application. By default, rails will start in development mode.<\/p>\n<p><code>$ mongrel_rails start<\/code><\/p>\n<p>A daemon with the application should now be running on port 3000. <code>ctrl+c<\/code> to stop the server, lets move on to mongrel_cluster<\/p>\n<h4>Mongrel Cluster<\/h4>\n<p>Mongrel Cluster will manage a pool of mongrels that nginx will reverse proxy requests to.<\/p>\n<blockquote><p>Mongrel_cluster is a GemPlugin that wrappers the mongrel <span class=\"caps\">HTTP<\/span> server and simplifies the deployment of webapps using a cluster of mongrel servers. Mongrel_cluster will conveniently configure and control several mongrel servers, or groups of mongrel servers, which are then load balanced using a reverse proxy solution.<\/p><\/blockquote>\n<p>The <a href=\"http:\/\/rubyforge.org\/projects\/mongrel\/\">wiki<\/a> has more extensive documentation, right now we will only look at the basics.<\/p>\n<p><code># mongrel_rails cluster::configure -e production -p 8000 -N 3 -c \/path\/to\/your\/application -a 127.0.0.1 \u2014user mongrel \u2014group mongrel<\/code><\/p>\n<p>This line creates the file <code>application\/path\/config\/mongrel_cluster.yml<\/code>. Most of the switches are self explanatory, <code>-a 127.0.0.1<\/code> configures the IP that the server binds to, by making it local you make it impossible to hit a mongrel directly without passing through the proxy. <code>-p<\/code> and <code>-N<\/code> configure the start port and the number of mongrels to start, so 8000, 8001, and 8002 in this case. <code>\u2014user<\/code> and <code>\u2014group<\/code> are the system user and group the servers will run under; because the mongrels run on an unprivileged port, they may run as an unprivileged user to improve security. <code>-e<\/code> is the environment our rails application will run in and <code>-c<\/code> is the path to our application (mongrel cluster will look for its configuration file at this path + <code>config\/mongrel_cluster.yml<\/code>, you may use <code>-C<\/code> if you want to directly specify a <span class=\"caps\">YAML<\/span> config file).<\/p>\n<pre><code>\u2014-\nuser: mongrel\ngroup: mongrel\ncwd: \/path\/to\/my\/application\naddress: 127.0.0.1\nport: \"8000\"\nservers: 3\nenvironment: production\npid_file: log\/mongrel.pid\n<\/code><\/pre>\n<h4>Evented Mongrel<\/h4>\n<p>To use evented mongrels, you just need to pass an environmental variable <code>EVENT=1<\/code> while starting your server. With <code>mongrel_cluster<\/code> that will look like this:<\/p>\n<p><code># EVENT=1 mongrel_rails cluster::start<\/code><\/p>\n<p>Don\u2019t forget to pass it even if you are just restarting the servers! At the top of the mongrel.log you should see it notify you that it is using events.<\/p>\n<h4>Web Server<\/h4>\n<p>Currently our application server and web server are on the same virtual machine. In the future, we may choose to scale by moving the web server and static content to another virtual machine (or perhaps a <span class=\"caps\">CDN<\/span> like Akamai) and have pure application servers. For now, we will keep things simple.<\/p>\n<h4>nginx<\/h4>\n<p><a href=\"http:\/\/nginx.net\/\">nginx<\/a> is a very fast proxy that we will also use to serve our static content.<\/p>\n<p><code># apt-get install nginx<\/code><\/p>\n<p>Configuration is very straight forward, everything is done in <code>\/etc\/nginx\/nginx.conf<\/code>. The following configuration is based on \u201cEzra Zygmuntowicz\u2019s Brainspl.at\u201d <a href=\"http:\/\/brainspl.at\/nginx.conf.txt\">nginx.conf<\/a>.<\/p>\n<pre><code># user and group to run as\nuser  www-data www-data;# number of nginx workers\nworker_processes  6;# pid of nginx master process\npid \/var\/run\/nginx.pid;# Number of worker connections. 1024 is a good default\nevents {\n  worker_connections 1024;\n}# start the http module where we config http access.\nhttp {\n  # pull in mime-types. You can break out your config\n  # into as many include's as you want to make it cleaner\n  include \/etc\/nginx\/mime.types;  # set a default type for the rare situation that\n  # nothing matches from the mimie-type include\n  default_type  application\/octet-stream;  # configure log format\n  log_format main '$remote_addr - $remote_user [$time_local] '\n                  '\"$request\" $status  $body_bytes_sent \"$http_referer\" '\n                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';  # main access log\n  access_log  \/var\/log\/nginx_access.log  main;  # main error log\n  error_log  \/var\/log\/nginx_error.log debug;  # no sendfile on OSX\n  sendfile on;  # These are good default values.\n  tcp_nopush        on;\n  tcp_nodelay       off;\n  # output compression saves bandwidth\n  gzip            on;\n  gzip_http_version 1.0;\n  gzip_comp_level 2;\n  gzip_proxied any;\n  gzip_types      text\/plain text\/html text\/css application\/x-javascript text\/xml application\/xml application\/xml+rss text\/javascript;  # this is where you define your mongrel clusters.\n  # you need one of these blocks for each cluster\n  # and each one needs its own name to refer to it later.\n  upstream mongrel {\n        fair; # This changes from round-robin based to load based.\n    server 127.0.0.1:8000;\n    server 127.0.0.1:8001;\n    server 127.0.0.1:8002;\n  }  # the server directive is nginx's virtual host directive.\n  server {\n    # port to listen on. Can also be set to an IP:PORT\n    listen 80;    # Set the max size for file uploads to 50Mb\n    client_max_body_size 50M;    # sets the domain[s] that this vhost server requests for\n    # server_name www.[yourdomain].com [yourdomain].com;    # doc root\n    root \/path\/to\/your\/application\/public;    # vhost specific access log\n    access_log  \/var\/log\/nginx.vhost.access.log  main;    # this rewrites all the requests to the maintenance.html\n    # page if it exists in the doc root. This is for capistrano's\n    # disable web task\n    if (-f $document_root\/system\/maintenance.html) {\n      rewrite  ^(.*)$  \/system\/maintenance.html last;\n      break;\n    }    location \/ {\n      # needed to forward user's IP address to rails\n      proxy_set_header  X-Real-IP  $remote_addr;      # needed for HTTPS\n      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;\n      proxy_set_header Host $http_host;\n      proxy_redirect false;\n      proxy_max_temp_file_size 0;      # If the file exists as a static file serve it directly without\n      # running all the other rewite tests on it\n      if (-f $request_filename) {\n        break;\n      }      # check for index.html for directory index\n      # if its there on the filesystem then rewite\n      # the url to add \/index.html to the end of it\n      # and then break to send it to the next config rules.\n      if (-f $request_filename\/index.html) {\n        rewrite (.*) $1\/index.html break;\n      }      # this is the meat of the rails page caching config\n      # it adds .html to the end of the url and then checks\n      # the filesystem for that file. If it exists, then we\n      # rewite the url to have explicit .html on the end\n      # and then send it on its way to the next config rule.\n      # if there is no file on the fs then it sets all the\n      # necessary headers and proxies to our upstream mongrels\n      if (-f $request_filename.html) {\n        rewrite (.*) $1.html break;\n      }      if (!-f $request_filename) {\n        proxy_pass http:\/\/mongrel;\n        break;\n      }\n    }    error_page   500 502 503 504  \/500.html;\n    location = \/500.html {\n      root   \/path\/to\/your\/application\/public;\n    }\n  }\n}<\/code><\/pre>\n<p>Now that you have nginx configured, start it and you should have a solid rails stack up and running.<\/p>\n<p><code># \/etc\/init.d\/nginx start<\/code><\/p>\n<h4>Request Cycle<\/h4>\n<p>Unlike more traditional setups, each individual request does not necessarily take the same path. Nginx monitors port 80 and accepts all incoming requests. Using the rewrite rules it looks for a file in the <code>public<\/code> directory with <code>\/url\/path + .html<\/code>. If it finds this static <span class=\"caps\">HTML<\/span> page, it replies. It also searches for <code>index.html<\/code> to retrieve folder indexes. These static <span class=\"caps\">HTML<\/span> files are generated by rails\u2019 page caching mechanism, and provide enormous performance gains at a cost of flexibility. If nginx is unable to find an <span class=\"caps\">HTML<\/span> page, it forwards the request to the cluster of mongrels. The mongrel that gets the request depends on which proxy algorithm you choose; the fair option spread the requests based on availability. Once received, the mongrel then runs the request through the rails application: the database is hit, ruby does some magic, and a page is completed and passed back to the mongrel which logs the request as complete and passes it back to nginx. If page caching is enabled, rails writes the page that was just generated to the <code>public<\/code> directory (lazy caching) before handing it off to the mongrel. Nginx takes this page and forwards it to the browser, acting as a proxy (gateway). The general process is repeated on each request, but the mongrel that is given the dynamic request will be different, and a cached page may be found, avoiding the need to engage the mongrels all together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux, MySQL, nginx, Mongrel , Rails, and friends. Big Picture: An ideal setup uses two different servers or virtual machines to split up the load and facilitate scaling. The first machine runs nginx serving static content and acting as a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":0,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}