devxlogo

Real-Life Rails: Develop with NetBeans, Deploy on Linux

Real-Life Rails: Develop with NetBeans, Deploy on Linux

he Ruby language’s conciseness makes it great for development?fewer lines of code means reduced development time and maintenance costs. Rails, the Ruby-based web application framework, can be equally developer-friendly, but some developers have encountered scaling limitations when deploying large-scale Rails web applications. While scaling Rails applications over many servers to handle many thousands of concurrent users can indeed lead to problems, with the proper tools and techniques developers can deploy small- and medium-scale Rails applications effectively.

In this article, I explain how to do just that using my preferred Ruby and Rails development IDE NetBeans 6.0, some open source server and caching systems, and the typical Rails application I work with: running on a single server and needing to support only 100 or so concurrent users. The article begins by walking you through how to set up NetBeans 6.0 for Ruby and rails development, and then moves on to techniques for deploying Rails applications efficiently.

This article assumes you have some experience with Rails development (in particular, creating a Rails application and using the Rails command-line tools) and version control (in particular, installing and using the command-line SVN tools?see Sidebar 1. Using Subversion for Version Control). It also assumes you know how to set up a Linux server for production (with Ruby, Gem, and Rails installed). It will not provide instructions for setting these up. To get the most out of this article, you will need the following technologies in addition to Rails and your Linux server:

What You Need
NetBeans 6.0
The nginx HTTP and proxy server
The Mongrel server
The memcached system

Leveraging NetBeans 6.0 Ruby and Rails Support
The first step toward using NetBeans 6.0 for your Ruby and Rails development is installing NetBeans and the Ruby plugins. Click here to download the Ruby NetBeans version 6.0 bundle I use. Add any optional Ruby and Rails plugins (e.g., if you use JRuby and want to add GlassFish plugins) by running the menu item Tools?Plugins.

Although I do use JRuby for projects that use existing Java libraries (for me, this is mostly artificial intelligence development where I need Java libraries for machine learning, reasoning, the semantic web, etc.), I still use the C version of Ruby for almost all Rails development and deployment. By default the Ruby NetBeans 6.0 bundle sets JRuby as the Ruby system. Change this setting by using the menu item NetBeans?Preferences?Ruby (see Figure 1 for a screenshot from my MacBook. If you use Linux or Windows, you will see something similar.).

Next, you will use the Rails Generator in NetBeans to make your development environment as convenient as possible. Figure 2 shows my Rails CookingSpace.com project (and a few other projects that are collapsed from view while I work on CookingSpace.com). When I right-click on the top-level project (or control-click on a Mac), NetBeans displays a popup tools menu with the standard NetBeans tools on the bottom and the Ruby- and Rails-specific tools added to the top. The top option (Generate…) launches Rails Generator.


Figure 1. Change Default Ruby System for Ruby NetBeans 6.0 Bundle: By default the Ruby NetBeans 6.0 bundle sets JRuby as the Ruby system.
 
Figure 2. NetBeans Popup Tools Menu: Here’s a screenshot of my Rails CookingSpace.com project.

I find this popup tools menu more convenient than keeping a shell window open to use the Rails command line utilities. Figure 3 shows the Rails Generator dialog box.

You should now have NetBeans and the Ruby plugins installed and ready to go.


Figure 3. Rails Generator Dialog: Here’s a screenshot of my Rails CookingSpace.com project.
 
Figure 4. NetBeans Options for Running Tests Inside the IDE: The Ruby and Rails plugins have menu options for running tests inside the IDE.

If you are new to NetBeans, it may take you a while to get used to the environment. To shorten that process, use the following usage tips:

 
Figure 5. Test Server and Rails Console Running at the Same Time: The panel in the lower right corner has both server and console (currently selected) tabs.
  1. Check out the NetBeans web site’s one page summary of keyboard shortcuts.
  2. Make use of the Ruby and Rails plugins’ menu options for running tests inside the IDE, which I find more convenient than using the command line rake tools (see Figure 4).
  3. One of the most useful techniques for interactively developing a Rails web application using NetBeans 6.0 is to run both a test server (WEBrick or Mongrel) and a Rails console at the same time. As you edit and save code you see the changes testing in a web browser on the test server and you can test snippets of code before adding them to your models or controllers. Meanwhile, the console enables you to inspect live data. See Figure 5 for an example of this setup where the panel in the lower right corner has both server and console (currently selected) tabs.

Before proceeding with this article, now would be a good time to either create a trivial test Rails application using your new NetBeans development setup or import an existing Rails application that you have already written. You can import an existing Rails web application by using the menu File?New Project?Ruby?Ruby on Rails Project with Existing Sources.

Techniques for Efficient Rails App Deployment
You have many good options for deploying Rails web applications. I will discuss only one: a deployment setup that works well on a single server but supports adding additional servers as needed. The setup is composed of the nginx server, the Mongrel server, and the memcached system, which provides a high-performance, distributed memory object caching system that you can use to cache both database requests and web services requests. Rails applications can scale, but as with most web applications, they might have only a few concurrent users. If you add memcached to Mongrel, which is fast by itself, you might be able to put off a more complex deployment until you have more concurrent users.

You should have your Rails application’s development settings set for your development PC and the production settings for your Linux server. This is important because this discussion assumes (because you are not using Capistrano) that to deploy a new version of your web application you can simply do an svn update in your server’s deployment directory.

Using memcached is as easy as installing a gem:

sudo gem install memcache-client

And adding two lines to your production.rb file:

config.action_controller.fragment_cache_store = :mem_cache_storeconfig.action_controller.session_store        = :mem_cache_store

I do not usually add these to my development.rb file.

Deploying a Cluster on Mongrels on a Linux Server
With Ruby, Gem, and Rails installed on your Linux server (using the same version numbers as on your development PC), you can deploy a Mongrel cluster on it. I will demonstrate how using my CookingSpace.com project as the example. I start by creating a new unprivileged account with the same name as my Rails application. I prefer to name the account after the web application because later I might want to use the nginx web server to service several virtual domain names and I would like the Mongrel cluster for each web application to run under its own account.

Next, I create a deployment directory, create the account, and set ownership of the deployment directory:

sudo mkdir /var/mongrelsudo mkdir /var/mongrel/cookingspacesudo /usr/sbin/groupadd cookingspacesudo /usr/sbin/adduser -r cookingspace -g cookingspacesudo chown -R cookingspace /var/mongrel/cookingspacesudo chgrp -R cookingspace /var/mongrel/cookingspace

Now, I use SSH to remotely login to my server using the account cookingspace and pull the latest version of my web application from my subversion server:

cd /var/mongrel/cookingspace/svn co  svn+ssh://MY_ACCOUNT@MY_SERVER.com/home/svn/svn-repos/cookingspace .

I am using MySQL for my web application so, on my Linux server, I need to set up the databases:

mysqladmin create cookingspace_production --user=root -pmysqladmin create cookingspace_development --user=root -pmysqladmin create cookingspace_test --user=root -pexport RAILS_ENV=productionrake db:migrate

Before going any further, it is a good idea to test the web application running a single Mongrel, but in production mode:

cd /var/mongrel/cookingspace/script/server --environment=production

Assuming that this works, we will now install the mongrel_clusters Ruby Gem:

sudo gem install mongrel_cluster

When logged in as user cookingspace, configure three Mongrels and start and stop the cluster to test it:

cd /var/mongrel/cookingspace/mongrel_rails cluster::configure -e production -p 3001 -n 3  -a 127.0.0.1 --user cookingspace --group cookingspacemongrel_rails cluster::startmongrel_rails cluster::stop

Installing and Configuring the nginx Proxy and Web Server
On your server, download the latest stable version of nginx and install it:

cd nginx-0.5.35./configuremakesudo make install	

This installs nginx in /usr/local/nginx, and you need to edit the file /usr/local/nginx/conf/nginx.conf to look like this:

worker_processes  3;error_log  logs/error.log notice;error_log logs/warning.log warning;pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       conf/mime.types;    default_type  application/octet-stream;    tcp_nopush     on;    keepalive_timeout  65;    tcp_nodelay        on;    upstream mongrel {        server 127.0.0.1:3001;        server 127.0.0.1:3002;        server 127.0.0.1:3003;    }    gzip on;    gzip_min_length  1100;    gzip_buffers     4 8k;    gzip_types       text/plain;    server {        listen       80;        server_name  cookingspace.com;        root /var/mongrel/cookingspace/public;        access_log  off;        rewrite_log on;        location / {            proxy_pass  http://mongrel;            proxy_redirect     off;            proxy_set_header   Host             $host;            proxy_set_header   X-Real-IP        $remote_addr;            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;        }    }}

To test, make sure that your Mongrel cluster has started and run nginx manually:

cd /usr/local/nginx/sbin/nginx -c conf/nginx.conf

Starting Your Mongrel Cluster and Nginx Automatically
This was a tough section for me to write because different Linux distributions have slightly different tools for managing processes at startup. That said, even though it is a little “old school,” I think that most Linux distributions (and also FreeBSD, etc.) support putting startup commands in /etc/rc.local. So that is what I will show here. I tested this on only one Linux server:

# I made sure that /usr/local installs are visible:		export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATHexport PATH=$PATH:/usr/local/bin# start memcached using a non-privileged account:/usr/local/bin/memcached -d -m 16384 -l 127.0.0.1 -p 11211 -u cookingspace >
/home/cookingspace/memcached.log &# start the mongrel cluster and nginx using non-privileged accounts:(cd /var/mongrel/cookingspace/ ; su cookingspace -m -c "nohup
/usr/local/bin/mongrel_rails cluster::restart > /home/cookingspace/mongrel_rails.log &" )(cd /usr/local/nginx/ ; sbin/nginx -c conf/nginx.conf)# note: nginx spawns worker processes using a non-privileged account.

This is a little simplistic, but it has the advantage of likely working on most systems.

A Time-saver
After working with server-side Java for over 10 years, I find writing Rails applications to be a lot of fun. It seems to take much less time to get projects done. I can spend more time solving real problems and less time dealing with infrastructure software. I will still look to server-side Java for projects that must scale and that require large development teams?the Java features that slow down development (static typing, verbose but readable code) also make Java better for very large development teams. That said, most projects are small enough to be quickly written by a few developers, and Ruby and Rails are a great fit for small and medium-sized projects (Sidebar 2. What If I Need to Scale? offers some suggestions for adding additional servers to handle increased user loads).

Deploying Rails applications to servers, however, has a reputation for being tricky?and if you are not familiar with Linux system administration it probably is. I hope that this article saves you time and aggravation in your deployments.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist