How to load the RVM part of user's .bashrc to run Ruby CGI scripts under Apache?

徘徊边缘 提交于 2019-12-11 03:37:49

问题


I've configured a new server on Ubuntu 12.04 and I started to use RVM.

I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from

!#/usr/bin/ruby

to

!#/usr/bin/env ruby

However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment (last lines of .bashrc) was not loaded under Apache.

I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users and different paths.

The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:

sudo ln -s /home/apache_user/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby

But I want to know if there's a better solution, because I guess that rvm --default use is better than sudo ln -s.

I am thinking about:

  • loading the RVM Environment on startup (but I don't know how to achieve that);
  • loading the RVM Environment for each web request (which can degrade performance, and I don't know how to configure Apache to do that);
  • maybe the RVM Environment is loaded and all I must do is to guess the name of relevant variables to pass with PassEnv directive. But I doubt that. (Why Apache would run the .bashrc instead of another shell like csh or ksh?)

回答1:


you can source the ruby environment, I'm not sure if it's enough to source it in $HOME for apache or if you need to modify /etc/init.d/apache2, but the line is:

source /path/to/rvm/environments/<name>

where for <name> you can either use full ruby name or an alias name

You can create aliases with:

rvm alias create veve 1.9.3-p125@my-project

which for RVM installed in /home/app/.rvm will allow you to use:

source /home/app/.rvm/environments/veve

in .bashrc or /etc/init.d/apache2 (just near top, bellow shebang).




回答2:


you could always do

sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby

That will link the default version of the rvm-managed Ruby to /usr/bin/ruby and you will never have to do anything. set it and forget it.



来源:https://stackoverflow.com/questions/12010699/how-to-load-the-rvm-part-of-users-bashrc-to-run-ruby-cgi-scripts-under-apache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!