Best practices for using RVM to run Ruby on Rails 5 on a web server - rvm

I realize that this question has already been asked a million times, but after days of searching I am still yet to find a suitable answer.
I would like to run Ruby on Rails 5 on Centos 7. The default Ruby currently packaged with Centos 7 is 2.0.0. For Rails 5, I need a higher version, hence the need for RVM.
Here are some of the problems I have encountered when using RVM:
If I use the recommended option to install RVM
\curl -sSL https://get.rvm.io | bash -s stable --ruby
RVM and all Ruby gems will install into my $HOME directory, but this is not ideal, because the server has many users, and, since it is a web server, it is, of course, not desirable to have gems contained with users' home directories, especially if those directories could be deleted at any time (if an employee's contract ends).
Some people recommend installing RVM using sudo to get around that problem (though just as many people frown on this approach, as it seems to negate the purpose of RVM):
\curl -sSL https://get.rvm.io | sudo bash -s stable --ruby
and based on RVM's own suggestion, I comment out the line containing
Defaults secure_path=...
and my gems are installed into /usr/local/rvm/gems/ruby-2.3.1, which is great, except it produces another problem:
If I run gem install rails I receive an error:
You don't have write permissions for the /usr/local/rvm/gems/ruby-2.3.1 directory.
Yes, I could solve this by adding myself to the rvm group, but this is not ideal, because that means I always have to remember to add every new user to and remove every old user from this group.
If I run sudo gem install rails I also receive an error:
/usr/bin/env: ruby: No such file or directory
If I run rvmsudo gem install rails, everything works, except I will also have to run bundle install using rvmsudo, which produces a warning, because it is not recommended.
I could run everything using root user, which works... but then I need to remember to change the owner of all of my project files because apache doesn't quite have root privileges and is unable to write to certain of the project's directories. Plus, running as root seems wrong on so many levels.
In other words, no matter what approach I choose while using RVM, I run into one problem or another. Perhaps, someone else who has deployed this successfully can share their insights.
The only reasonable approach I have been able to come up with is to create a dedicated Rails user, to which all server users would switch prior to performing any maintenance, but I am worried that a service like apache won't "see" the gems installed locally.
Comments, thoughts, ideas? Maybe there is something I am missing about RVM?

Related

Install Redmine 3.x on WHM/Cpanel

Guys I've spent last 24 hours continuously trying to install Redmine 3.x on the WHM/Cpanel server but failed to do so. Can some please guide me with one proper way or tell me about a relatively new article regarding this issue because every solution i find on the internet is old.
There is no out-of the box solution for Cpanel.
Redmine is complex Ruby on Rails based web application, and as such it relies on lot's of 3rd party libraries, defined in Gemfile.
Although it migth be possible to install all Gems manually via Cpanel and avoid bundle install command, you would still need to run some commands like
bundle exec rake generate_secret_token
bundle exec rake db:migrate RAILS_ENV=production
So to install it via WHM/Cpanel, I suggest you create a cpanel account for your Redmine, then enable SSH access for that Cpanel user, and follow official installation tutorial from Redmine. (Which is too big to write here inside answer).
Just make sure to unpack your Redmine outside public_html!
And you can deploy your Redmine via Cpanel as FastCGI, by pointing your application path to Redmine's public directory, just make sure to have dispatch.fcgi in your public folder of Redmine
mv dispatch.fcgi.example dispatch.fcgi
mv htaccess.fcgi.example .htaccess

System ruby/gem vs RVM ruby/gem

I installed RVM, read the documentation and do not understand it well. For example, I had an rails application that is created and run just find (before I install RVM) and it uses the system ruby and system gem. Now after I install RVM:
Do I need to re-install these gems into RVM so these gem can be under RVM control?
How to a port the application to use RVM gem instead of system gem?
Is RVM a wrapper for ruby and gem or a separate repository of these ruby and gem? So if I install a gem under RVM, the gem only exist in RVM or exist across RVM and system, or vice versa. Am I duplicating or does RVM and System synchronize their gem/system themselves?
I am still completely confused of the goods and uses of RVM. Now all my previous applications that used to work not doesn't even start with various errors.
My environment: Mac OS X 10.8; Rails 3.2.9; TextMate
Yes. Use bundler and there will be no pain.
It just need to be run in RVM environment. No special porting required.
It is intended to provide separate environment for each of your projects by substituting environment variables like $PATH, $GEM_HOME, $GEM_PATH.
Pros:
You can have different ruby interpreters installed to fulfil your applications' requirements. Imagine that you are starting a new project with Rails 1.9.3, but you are still working on old two which use 1.8.7 and 1.9.1 and have not been ported so far.
Your gems does not conflict with each other. For example Psych has special needs. If you use it, you got to use it in all your project. But with RVM you can create different gemsets for each project.
Moves gem directories too dirs where you got read+write access. This is good because does not force you to compile gems with root privileges.
Cons:
I had problems with RVM when using it for long time under Fish shell. Two times, after some weeks whole RVM went crazy and just get broken. Not going into details, I got to remove whole ~/.rvm directory. Never happened under Bash.
My typical workflow with RVM is following:
Add ruby "1.9.3" or equivalent to Gemfiles of my projects to avoid running it with wrong Ruby version.
Install RVM and install Rubies I need, rvm install 1.9.3.
In given project, rvm use 1.9.3.
Install required gems.
Use my app normally.
Please note I am not using Gemsets. This is because of Cons #1. I really love Fish shell, can't live without it, and bundler alone gives me decent management of Gems (one problem: Psych). To use Gemsets, two additional steps between 3. and 4.:
3a. Create one rvm gemset create gemset1.
3b. Use it rvm use gemset1#1.9.3.
I always use RVM when working with some legacy projects.
Refer to this screencast: http://railscasts.com/episodes/200-rails-3-beta-and-rvm for decent tutorial.

How can I make rbenv install certain gems automatically?

I always want bundler and gem-ctags to be installed in any Ruby I have installed. Is there a way to make rbenv / ruby-build install them automatically?
RVM has #global gemsets; is there an equivalent in rbenv?
Use rbenv Plugins
By default, rbenv doesn't use gemsets. People who love rbenv probably just hack their GEM_PATH and GEM_HOME when they want different gems, but there are also some plugins that automate some of this behavior. Two that I know of are:
rbenv-vars by Sam Stephenson (the author of rbenv)
rbenv-gemset by Jamis Buck
Use Bundler's --path Flag
Of course, you can also just use bundler with a --path flag to install bundled gems to a unique directory instead of using gemsets. The bundle-install(1) docs say:
--path=<path>
The location to install the gems in the bundle to. This defaults to the gem home,
which is the location that gem install installs gems to. This means that, by
default, gems installed without a --path setting will show up in gem list. This
setting is a remembered option.
Consider the "Batteries-Included" RVM Instead
The argument for rbenv is that is simpler and does less under the hood than RVM. I'd argue that if you need to add ruby-build, rbenv-gemset, and other plugins to get the functionality you need, then you may as well use RVM in the first place.
This isn't a critique of rbenv, or praise for RVM. I'm just pointing out that if you want a tool that gives you most of RVM's functionality, you might was well just use the tool that gives you want you want "out of the box" rather than bolt the functionality on post facto.
You can certainly do what you need to do with rbenv, one way or another. I'd just suggest that you not put yourself in a position where you have to fight your tools to get things done.
I wrote a little shell script that tells Gem to add particular gems to any Ruby currently set. That way it works whether I'm using rbenv, RVM or I'm on a machine where I installed Ruby from source by hand.

Run a non-jRuby project after installing jRuby (or remove jRuby)

I installed jRuby to try it in a new project, but I didn't think it would affect my entire Ruby installation.
Now when I try to start an old project, I get an error
/usr/bin/env jruby: no such file or directory
I've tried searching for how to remove jruby, but don't come up with anything. is there a nice way to get my rails apps working again with regular ruby?
I REALLY hope so.
Looks like it created symlinks; point them back at at the Ruby you want to use.
Use something like rvm to avoid this. (Or rbenv if you prefer.)
Rerun "bundle install" -- if you ran bundle install under jruby, the shebang for your scripts in the /bin folder was likely changed to reference jruby. By re-running bundle install under MRI, bundler will fix the shebangs for you.

Why do I have to use sudo when installing gems

I'm a newbie when it comes to OSX and was curious if there was a setting I could enable to not force me to use sudo when installing gems.
Anyone?
You need to use sudo because you're installing the libraries to the system area, by default, which is not writable by non-privileged users.
I don't know offhand if there's a way to get gems to install to a user library, but assuming they learned from perl's CPAN, it seems likely that that is possible.
On Linux at least (and I think OSX is the same), you don't have to use sudo to install gems. If you don't, they are installed into your home folder. If you do, they are installed into a location that isn't writeable without sudo.
Try running gem env and sudo gem env and comparing the output. You will see that the installation directory and gem paths are different.
Use RVM to manage your ruby installs, multiple ruby versions, and gems for different ruby versions and projects. Then you will not need sudo to install gems and a lot of other things will be easier too.
Mainly this is a security assurance, since you could be installing potentially harmful binaries, placing files in protected folders or changes to the OS.