部署 Ubuntu 12.04 (Torquebox 3, Rails 4)

Unknow user · January 24, 2014 · Last by jimrokliu replied at January 24, 2014 · 3260 hits

无聊发帖。发帖无聊。 翻译自此.自此翻译

正文开始

Torquebox是 JRuby 下基于 JBoss 的应用服务器。作为一个应用服务器,它命中注定地捆绑了 Web 服务器,消息队列,JRuby 运行环境以及 websocket 支持。我们 (AmberBit) 选择用它来部属一些高流量 web 应用,BOSS 夸我们:"你们是太 TM 机智了。"

Torquebox 立足于 Java, 得到了 RedHat(毁人不倦,此君最爱) 的大力赞助,谁用谁知道,一句话,"碉堡了!". 但是 (为什么总是有但是) 除非有怪叔叔教你,不然要想在 Ubuntu 12.04 上成功部署它,熊猫烧香吧。

"大丈夫!", 本文就是个傻瓜 (说的是我,不是你) 教程,教你在 Ubuntu 12.04 上跑 Torquebox 3.0.1. 我使用的是DigitalOcean2GB RAM 的 VPS, 客官要准备台内存大大的机器。Java, 你懂的。

依赖环境

$ sudo apt-get install git unzip nodejs openjdk-7-jre-headless wget

安装 Torquebox

$ wget http://torquebox.org/release/org/torquebox/torquebox-dist/3.0.1/torquebox-dist-3.0.1-bin.zip
$ unzip torquebox-dist-3.0.1-bin.zip
$ mv torquebox-3.0.1/ /opt/torquebox

大家都知道,单服务单用户组单用户,而且三者名称必定相同。

$ sudo adduser torquebox
$ sudo chown torquebox.torquebox -R /opt/torquebox/

一如往日,设置环境变量。创建 (/etc/profile.d/torquebox.sh) 写入以下内容

$ export TORQUEBOX_HOME=/opt/torquebox
$ export JBOSS_HOME=$TORQUEBOX_HOME/jboss
$ export JRUBY_HOME=$TORQUEBOX_HOME/jruby
$ export PATH=$JRUBY_HOME/bin:$PATH

大家还知道,接下来 Sign out and Sign in. 初步测试一下,就一下下,就一下下嘛。

$ jruby -S irb

如果成功进入 IRB, 我们继续吧. 如果出了问题,熊猫烧香吧,我们继续. Anyway, 我们继续。

接下来该注册系统服务了。

$  cd /opt/torquebox
$ rake torquebox:upstart:install

初步测试一下,就一下下,就一下下嘛。

$ sudo service torquebox start

如果直接就成功注册了服务,恭喜你,熊猫烧香吧. 如果失败了,大丈夫,这是命中注定的。编辑一下 (/etc/init/torquebox.conf), 差不多像下边这样就行了

description "This is an upstart job file for TorqueBox"

pre-start script
bash << "EOF"
  mkdir -p /var/log/torquebox
  chown -R torquebox /var/log/torquebox
EOF
end script

start on started network-interface
stop on stopped network-interface
respawn

limit nofile 4096 4096

script
bash << "EOF"
  su - torquebox
  /opt/torquebox/jboss/bin/standalone.sh >> /var/log/torquebox/torquebox.log 2>&1
EOF
end script

再测试一下,就一下下,就一下下嘛。

$ sudo service torquebox start

无论结果如何,Anyway, 我们继续. Torquebox 默认霸占了 8080 端口,打开 (http://localhost:8080), 出现了脑残的 404, 就说明你成功了。

Nginx 代理

可以不用 Nginx. 但只有 Asshole 才这么干。

$ sudo apt-get install nginx

对于那些一定要手动编译安装自我感觉良好的人,我只能说"***************"!

编辑 (/etc/nginx/site-enabled/default)

server {
  listen 80;
  server_name example.com; # 亲 不要眼花  注意修改
    location / {
      access_log off;
      proxy_pass http://127.0.0.1:8080;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
$ sudo service nginx restart 

测试一下,就一下下,就一下下嘛. 打开 (http://localhost), 如果出现了之前的脑残的 404, 恭喜你,熊猫烧香吧。

ROR&&Capistrano(v2)

对不起,我还在上班,打字有点累,所以介绍性的文字就直接砍掉了。如果你连 Capistrano 都不知道,估计也不会看到这里. 修改 (config/deploy.rb), 环境变量请自行修改。

require 'torquebox-capistrano-support'
require 'bundler/capistrano'

set :application, "myapp"

set :torquebox_home,    '/opt/torquebox'
set :jboss_home,        "#{torquebox_home}/jboss"
set :jruby_home,        "#{torquebox_home}/jruby"
set :bundle_dir, "~/shared"

set :default_environment, { 
  "TORQUEBOX_HOME"=>"/opt/torquebox",
  "JBOSS_HOME"=>"/opt/torqueobox/jboss",
  "JRUBY_HOME"=>"/opt/torquebox/jruby",
  "PATH"=>"/opt/torquebox/jruby/bin:$PATH"
}

role :web, "XXX.XXX.XXX.XXX"                       # Your IP here
role :app, "XXX.XXX.XXX.XXX" 
role :db,  "XXX.XXX.XXX.XXX", :primary => true

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

set :user, -> { "torquebox" }
set :group, -> { "torquebox" }
set :use_sudo, false

set :scm, :git
set :stage, "production"
set :repository,  "git://github.com/yourcompany/yourwebapp.git" # Address of your Git repository goes here

set :deploy_to, -> { "/home/#{user}" } # Change this if you want to deploy to other directory
set :deploy_via, :remote_cache
set :deploy_env, -> { "production" }
set :rails_env, -> { "production" }
set :app_context,       "/"

namespace :deploy do
  desc "Symlink shared configs and folders on each release."
  task :finalize_update do
    # If this task fails, please make sure you put config/database.yml and config/app_config.yml in the shared directory
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end

  desc "Start the server"
  task :start do
    run "cd #{current_path} && RAILS_ENV=production bundle exec torquebox deploy --name=myapp" # change name
  end

  task :stop do
    run "cd #{current_path} && RAILS_ENV=production bundle exec torquebox undeploy --name=myapp" # change name
  end

  desc "Hot-restart the server"
  task :restart do
    run "cd #{current_path} && touch tmp/restart.txt" # hot deployment made easy!
  end
end
  • deploy:start 以及类似中的 name 是 knob 文件的名称。my-app 对应 myapp-knob.yml 具体请查文档。
  • task: restart 是零宕机的。当然除非你修改这个 restart task. 不过你都能主动修改了,还会脑残的看这个?

接下来就是零碎的东西. setup 工作

$ bundle exec cap deploy:setup

请到服务器配置 (../torquebox/shared/config/database.yml). 文件位置根据上面配置而定,不要秀下限。vps 土豪请 ssh 到服务器,本机做测试的 so easy, 用 mac 使用 vagrant 装 ubuntu 的自我感觉极其良好但实际上就是个渣渣的就不要来秀优越感了。

第一次部署

$ bundle exec cap deploy:cold

之后的就是

$ bundle exec cap deploy

OVER

好像有个 Lite 版本的 Torquebox,JRuby 确实有一些独特的地方,但部署也不容易啊。兼容性就够头疼。

You need to Sign in before reply, if you don't have an account, please Sign up first.