Ruby On Rails 环境搭建

Ruby On Rails


Ruby On Rails是最流行和体验最好的敏捷开发WEB框架(没有之一),介绍性的内容这里就不赘述了。本文介绍下Ruby On Rails环境的搭建流程。



$ sudo apt-get upgrade
$ sudo apt-get install build-essential libssl-dev libreadline-gplv2-dev \
  lib64readline-gplv2-dev zlib1g zlib1g-dev libyaml-dev libsqlite3-dev



$ wget
$ tar xzvf ruby-1.9.3-p0.tar.gz
$ cd ruby-1.9.3-p0
$ ./configure
$ make
$ sudo make install


$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]



$ wget
$ tar -zxvf rubygems-1.8.24.tgz
$ cd rubygems-1.8.24
$ ruby setup.rb
RubyGems 1.8.24 installed
== 1.8.24 / 2012-04-27

* 1 bug fix:

  * Install the .pem files properly. Fixes #320
  * Remove OpenSSL dependency from the http code path


RubyGems installed the following executables:


$ gem update --system 
Latest version currently installed. Aborting.



$ sudo gem install rails
Successfully installed i18n-0.6.1
Successfully installed multi_json-1.3.6
Successfully installed activesupport-3.2.8
Successfully installed builder-3.0.3
Successfully installed rack-ssl-1.3.2
Successfully installed thor-0.16.0
Successfully installed railties-3.2.8
Successfully installed bundler-1.2.1
Successfully installed rails-3.2.8
28 gems installed


ERROR:  Loading command: update (LoadError)
    no such file to load -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::UpdateCommand


sudo apt-get install zlib1g-dev
cd /ruby-source-files/ext/zlib
ruby extconf.rb
sudo make install


Installing ri documentation for rails-3.2.8...
file 'lib' not found
Installing RDoc documentation for rails-3.2.8...
file 'lib' not found


sudo gem install rdoc

安装Rails Completion

$ wget -O ~/.rails.bash
$ echo source ~/.rails.bash >> ~/.bashrc
$ source ~/.bashrc


Rails安装完成之后,你可以创建自己的Rails应用了: $ cd /srv $ rails new myapp create
create README.rdoc create Rakefile create create .gitignore create Gemfile ... ... Using rails (3.2.8) Using sass (3.2.1) Using sass-rails (3.2.5) Using sqlite3 (1.3.6) Installing uglifier (1.3.0) Your bundle is complete! Use bundle show [gemname] to see where a bundled gem is installed.


$ cd myapp
$ rails server
rails server
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-09-23 15:02:22] INFO  WEBrick 1.3.1
[2012-09-23 15:02:22] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin12.0.0]
[2012-09-23 15:02:22] INFO  WEBrick::HTTPServer#start: pid=9907 port=3000

执行以上的操作后,系统会启动WEBrick服务器,你可以通过http://来访问应用的内容了。不过WEBrick只可以作为测试用, 不能应用在大规模的生产环境中,所以我们需要一个高性能的服务器,这就是下面将要介绍的Unicorn


Unicorn(独角兽)是一个高性能的Rack HTTP server,更多介绍参见Github的介绍

Unicorn依赖于JavaScript Runtime,首先我们安装Node.js:

$ sudo apt-get install nodejs


$ gem install unicorn
Fetching: kgio-2.7.4.gem (100%)
Building native extensions.  This could take a while...
Fetching: raindrops-0.10.0.gem (100%)
Building native extensions.  This could take a while...
Fetching: unicorn-4.3.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed kgio-2.7.4
Successfully installed raindrops-0.10.0
Successfully installed unicorn-4.3.1
3 gems installed
Installing ri documentation for kgio-2.7.4...
Installing ri documentation for raindrops-0.10.0...
Installing ri documentation for unicorn-4.3.1...
Installing RDoc documentation for kgio-2.7.4...
Installing RDoc documentation for raindrops-0.10.0...
Installing RDoc documentation for unicorn-4.3.1...


$ cd /srv/myapp
$ unicorn -l -E development
I, [2012-09-23T15:22:39.793852 #12107]  INFO -- : listening on addr= fd=5
I, [2012-09-23T15:22:39.794149 #12107]  INFO -- : worker=0 spawning...
I, [2012-09-23T15:22:39.795973 #12107]  INFO -- : master process ready
I, [2012-09-23T15:22:39.797613 #12108]  INFO -- : worker=0 spawned pid=12108
I, [2012-09-23T15:22:39.797927 #12108]  INFO -- : Refreshing Gem list
I, [2012-09-23T15:22:41.245205 #12108]  INFO -- : worker=0 ready

其中-l指定Unicorn监听8080端口,-E development指定运行环境为开发环境,更多内容参见unicorn -h。Unicorn启动成功之后,可以通过http://来访问Rails应用。




worker_processes 4
working_directory "/srv/myapp" # available in 0.94.0+

listen "/tmp/unicorn.sock", :backlog => 64
listen 8080, :tcp_nopush => true

timeout 30

pid "/srv/myapp/tmp/"
stderr_path "/srv/myapp/log/unicorn.stderr.log"
stdout_path "/srv/myapp/log/unicorn.stdout.log"

# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and

  # The following is only recommended for memory/DB-constrained
  # installations.  It is not needed if your system can house
  # twice as many worker_processes as you have configured.
  # # This allows a new master process to incrementally
  # # phase out the old master process with SIGTTOU to avoid a
  # # thundering herd (especially in the "preload_app false" case)
  # # when doing a transparent upgrade.  The last worker spawned
  # # will then kill off the old master process with a SIGQUIT.
  # old_pid = "#{server.config[:pid]}.oldbin"
  # if old_pid !=
  #   begin
  #     sig = ( + 1) >= server.worker_processes ? :QUIT : :TTOU
  #     Process.kill(sig,
  #   rescue Errno::ENOENT, Errno::ESRCH
  #   end
  # end
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  # sleep 1

after_fork do |server, worker|
  # per-process listener ports for debugging/admin/migrations
  # addr = "{9293 +}"
  # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)

  # the following is *required* for Rails + "preload_app true",
  defined?(ActiveRecord::Base) and

  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)


upstream app_server {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response (in case the Unicorn master nukes a
    # single worker for timing out).

    # for UNIX domain socket setups:
    server unix:/tmp/unicorn.sock fail_timeout=0;

server {
    listen       8888;
    server_name  _;

    #access_log  logs/host.access.log  main;

    location / {
        # an HTTP header important enough to have its own Wikipedia entry:
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # enable this if you forward HTTPS traffic to unicorn,
        # this helps Rack set the proper URL scheme for doing redirects:
        # proxy_set_header X-Forwarded-Proto $scheme;

        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;
        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # set "proxy_buffering off" *only* for Rainbows! when doing
        # Comet/long-poll/streaming.  It's also safe to set if you're using
        # only serving fast clients with Unicorn + nginx, but not slow
        # clients.  You normally want nginx to buffer responses to slow
        # clients, even with Rails 3.1 streaming because otherwise a slow
        # client can become a bottleneck of Unicorn.
        # The Rack application may also set "X-Accel-Buffering (yes|no)"
        # in the response headers do disable/enable buffering on a
        # per-response basis.
        # proxy_buffering off;

      proxy_pass http://app_server;


RAILS_ENV=development  #environment 
RAILS_ROOT=/srv/myapp #


$ wget -O unicornd
$ sudo cp unicornd /etc/init.d/
$ sudo chmod +x /etc/init.d/unicornd
$ echo /etc/init.d/unicornd start >> /etc/rc.local
$ sudo /etc/init.d/unicornd start


comments powered by Disqus
Powered by GitHub  &&  Jekyll | CC BY-NC-SA