Apache-2.2

如何讓 Apache 的 www-data 辨識 RVm?

  • October 31, 2011

問題:Apache/Passenger 無法辨識 RVM

根據規範說明,我有 RVM 的多使用者安裝:http: //beginrescueend.com/rvm/install/

然後我編輯了 /etc/profile.d/rvm.sh 以包含以下行,以便 Ruby 1.9 是用於所有登錄使用者的版本:

rvm use --default 1.9.2

我的系統上至少有 3 個使用者:

  • ubuntu(我用來執行所有互動任務的預設使用者)
  • 乘客(這是部署任務的使用者)
  • www-data(這大概是我為服務 httpd 請求而設置的使用者)

當以互動方式登錄到ubuntu乘客時,以下內容正確返回:

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

但是,實際為這些請求提供服務的www-data使用者從不執行 /etc/profile.d/rvm.sh(或 profile.d 中的任何內容)。因此,以 www-data 使用者身份登錄時總是會發生以下情況:

$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

因此,Bundler 在我的開髮沙箱上使用正確的 Ruby 版本執行良好,但在執行時出現“無法在 Ruby 1.8 上安裝 Gem”類型的錯誤:

$ cap deploy
<snip>
 * executing "cd /var/www/app/releases/20111031001406 && bundle install --gemfile /var/www/app/releases/20111031001406/Gemfile --path /var/www/app/shared/bundle --deployment --quiet --without development test"
servers: ["example.com"]
[example.com] executing command
</snip>

(example.com 和 app 是我的真實伺服器和應用程序的佔位符)

我收到的錯誤是“linecache gem 需要 Ruby > 1.9)”,這就是我知道 Ruby 1.9.2 沒有執行是問題所在。

如何讓 Apache 辨識 RVM 和我想要的 Ruby 版本? (根據多使用者模式,RVM 和我想要的 Ruby 安裝在**/usr/local/rvm 中**

我已經多次遵循我能想到的每一條指令,但我顯然在這裡遺漏了一些東西。這裡的任何指導將不勝感激。

FWIW,這是我的 Capistrano 部署腳本:

# if you're still using the script/reapear helper you will need
# # these http://github.com/rails/irs_process_scripts
#
# # bundler bootstrap
require 'bundler/capistrano'

set :nice_name, "App"
set :application, "app"
set :domain, "example.com"

role :web, "#{domain}"                          # Your HTTP server, Apache/etc
role :app, "#{domain}"                          # This may be the same as your `Web` server
role :db,  "#{domain}", :primary => true # This is where Rails migrations will run
# # server details
set :default_run_options, {:pty => true}
set :ssh_options, {:forward_agent => true, :keys => "/path_to/ssh.key"}
#ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]

set :deploy_to, "/var/www/#{application}/"
set :user, "passenger"
set :use_sudo, false 

# repo details
set :scm, :git
set :scm_username, "githubuser"
set :repository, "git://github.com/githubuser/app.git"
set :branch, "master"
set :git_enable_submodules, 1

# tasks
namespace :deploy do
 task :start, :roles => :app do
   run "touch #{current_release}/tmp/restart.txt"
 end

 task :stop, :roles => :app do
 # Do nothing.
 end

 desc "Restart Application"
 task :restart, :roles => :app do
   run "touch #{current_release}/tmp/restart.txt"
 end

 desc "Symlink shared resources on each release - not used"
 task :symlink_shared, :roles => :app do
   #run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
 end
end

namespace :passenger do
 desc "Restart Application"  
 task :restart do  
   run "touch #{current_path}/tmp/restart.txt"  
 end
end

after :deploy, "passenger:restart"
after 'deploy:update_code', 'deploy:symlink_shared'

您可能需要設置PassengerRootandPassengerRuby指令以指向您希望它使用的 ruby​​ 版本(由 RVM 控制的版本)。如果您還沒有這樣做,我還建議每個應用程序使用一個 gemset。只要正確設置了 apache 指令,您就可以決定將乘客安裝到您希望的任何 gemset 中。

範例(使用 ree 和 Ubuntu):

sudo apt-get install build-essential bison openssl libreadline5 libreadline5-dev curl zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev  autoconf libc6-dev ncurses-dev libmysqlclient15-dev apache2-prefork-dev apache2-mpm-prefork libapr1-dev libaprutil1-dev libcurl4-openssl-dev 
rvm use ree@${gemset_here} --create --default --passenger
rvm ree@${gemset_here} gem install passenger --version="${PASSENGER_VERSION}"
passenger-install-apache2-module --auto --apxs2-path $(which apxs2)

您可能需要為您的發行版獲取適當的建構依賴項。乘客編譯後,您需要確保配置它(檢查輸出)。如果您需要使用 apache conf 片段,請執行以下命令:

passenger-install-apache2-module --snippet

我把我的放進去/etc/apache2/conf.d/passenger.conf(在一個 Ubuntu 系統上)。您的配置目錄可能因發行版而異。如果此時您仍然遇到問題,可能是您需要修復您正在使用的 RVM gemset 目錄的一些權限。

引用自:https://serverfault.com/questions/326204