Using Passenger with Rails and Apache 2 / SpawnPreparer Permission denied - ruby-on-rails

my environment is as follows:
Ubuntu 10.04
Ruby 2.0.0
Rails 4.0
The deployed Rails application resides in /var/www/application. The deployment is handled by Capistrano, therefore, the directory structure is as follows:
root#lvps91-250-114-42:/var/www/application# ls -la
total 16
drwxrwxr-x 4 www-data www-data 4096 2013-11-14 12:53 .
drwxr-xr-x 6 www-data www-data 4096 2013-11-12 22:54 ..
lrwxrwxrwx 1 www-data www-data 39 2013-11-14 12:53 current -> /var/www/application/releases/20131114115156
drwxrwxr-x 11 www-data www-data 4096 2013-11-14 12:51 releases
drwxrwxr-x 8 www-data www-data 4096 2013-11-13 01:49 shared
The config/deploy.rb is configured to use a :local ruby (which has been installed into ../shared). The capistrano config:
require 'bundler/capistrano'
require 'rvm/capistrano'
#....
set :bundle_flags, "--deployment"
set :default_shell, '/bin/bash -l'
set :rvm_ruby_string, :local
#....
before 'deploy:setup', 'rvm:install_rvm'
before 'deploy:setup', 'rvm:install_ruby'
Maybe this plays a role because there is another rvm/ruby installation which is recognized when installing the Passenger. Passenger told me to configure the Apache 2 as follows:
LoadModule passenger_module /root/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.24/buildout/apache2/mod_passenger.so
PassengerRoot /root/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.24
PassengerDefaultRuby /root/.rvm/wrappers/ruby-2.0.0-p247/ruby
But it complained about my .rvm installation (which resides in root :-() and that I will need to change the permissions (but I didn't change them):
It is recommended that you relax permissions as follows:
sudo chmod o+x "/root"
Press Ctrl-C to return to the shell. (Recommended)
After relaxing permissions, re-run this installer.
-OR-
Press Enter to continue anyway.
The corresponding sites-enabled/application configuration:
<VirtualHost *:80>
ServerName subdomain.domain.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/application/current/public
<Directory /var/www/application/current/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
When I started the application, I got the following error in my browser:
Cannot execute "/root/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.24/buildout/agents/SpawnPreparer": Permission denied (errno=13)
After:
sudo chmod o+x "/root"
Everything is working.
What is your advice to fix it? Should I re-install .rvm and passenger? Can I reconfigure the Apache configuration so that Apache will access all those executables which reside in /var/www/application?
Thank you in advance!!
jepetko

If you have used the rvm installation method for a single user, have you considered setting the ownership of the files for the ruby application to be the same as the owner of rvm?
For myself, I have ran into this problem before and here is what I have done:
Create new user for managing rvm
Add the new user to be able to sudo to root
Login as the new user and install rvm as a single user
Install passenger gem
Run passenger installation for module with rvmsudo
Set the ownership of the ruby application to be the same as the new user account for rvm management
Sometimes I have had to add the following to my apache conf:
SetEnv LD_LIBRARY_PATH /home/rvmuser/.rvm/default/lib
SetEnv GEM_PATH /home/rvmuser/.rvm/gems/ruby-1.9.3-p484:/home/rvmuser/.rvm/gems/ruby-1.9.3-p484#global
Hope this helps!

i have same problem with you before and this is how i solve it:
try use set config PassengerUser https://www.phusionpassenger.com/library/config/apache/reference/#passengeruser . Set it with user that you use when install rvm. Because you put the rails app on /var/www/ which is that is owned with apache/www-data you need specify user that owned the rvm.
So, based on you example, just add: PassengerUser deploy after or before DocumentRoot config on your apache config.
Don't forget to restart the apache. Good luck.

Related

How do I connect Apache to my running Ruby on Rails instance using passenger?

I’m using Apache 2.4 and Rails 5 on Mac Sierra. I did
brew install passenger
and then I created this file
localhost:apache2 davea$ cat /etc/apache2/other/passenger.conf
LoadModule passenger_module /usr/local/opt/passenger/libexec/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/opt/passenger/libexec/src/ruby_supportlib/phusion_passenger/locations.ini
PassengerDefaultRuby /usr/bin/ruby
But after restarting Apache
sudo apachectl restart
and then starting my Rails server (which runs on port 3000), I visit the local url on my machine
https://mylocalurl.com/
and I’m still seeing my Apache page, not my Rails page. What else am I missing that would connect Apache to Rails?
You need to configure a virtualhost in apache and then Passenger will start your rails app for you (you don't start your rails server when using apache integration).
At a minimum you need to point the apache virtualhost's document root to the public directory in your rails app.
<VirtualHost *:80>
ServerName yourserver.com
DocumentRoot /path/to/myapp/public
<Directory /path/to/myapp/public>
Allow from all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
Further reading:
https://www.phusionpassenger.com/library/config/apache/intro.html
https://www.phusionpassenger.com/library/config/apache/reference/

How can I get passenger-config restart-app to work?

Rails 4.1 on Ubuntu 14.04 with rbenv and ruby 2.2.1.
Using capistrano with the capistrano-passenger gem, but the restart at the end fails:
INFO [8213c63a] Running /usr/bin/env passenger-config restart-app /home/deployer/my_app --ignore-app-not-running as deployer#mysite.com
DEBUG [8213c63a] Command: passenger-config restart-app
DEBUG [8213c63a] Please pass either an app path prefix or an app group name. See --help for more information.
When I try to run this command at the command line via SSH, I get this:
deployer#host:~/app/shared/config$ passenger-config restart-app
*** ERROR: You are not authorized to query the status for this
What am I doing wrong here?
I'm using Apache, here's the relevant parts of my /etc/apache2/apache2.conf:
LoadModule passenger_module /home/deployer/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/passenger-5.0.5/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /home/deployer/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/passenger-5.0.5
PassengerDefaultRuby /home/deployer/.rbenv/versions/2.2.1/bin/ruby
</IfModule>
<VirtualHost *:80>
ServerName mysite.name.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /home/deployer/myssite/current/public
<Directory /home/deployer/mysite/current/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
Here's what got me running, I added this to my conifg/deploy.rb:
set :passenger_restart_with_sudo, true
Ref: https://github.com/capistrano/passenger/
To add password-less sudo access for the deployer user, on the server do:
(you might want to be more specific as to the allowed commands)
sudo tee /etc/sudoers.d/deployer > /dev/null <<'EOF'
deployer ALL=(ALL) NOPASSWD:ALL
EOF
...and in your delpoy.rb, have:
set :user, 'deployer' # Deployment user on remote servers
Note: it should be noted that the Passenger authors are working on a method so that sudo will not be required any longer in the future.
If you don't want to use sudo for restarting application server, simply add to config/deploy.rb:
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
end
For restarting with sudo (Note that it doesn't have any effect on Passenger < 5):
set :passenger_restart_with_sudo, false
If you want to change restarting options, you might override these:
set :passenger_restart_command, 'passenger-config restart-app'
set :passenger_restart_options, -> { "#{deploy_to} --ignore-app-not-running" }

running a Rails app on a fresh installation of Nginx/Passenger causes error "no such file to load -- bundler/setup (LoadError)"

I have a VPS running Ubuntu 12.04. I've installed Nginx and Passenger on it by following these and these instructions, and as per the instructions, I've added to the http block of file /etc/nginx/nginx.conf the following lines:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/ruby;
and the following block (where I've replaced the actual domain name by www.example.com):
server {
listen 80;
server_name www.example.com;
root /srv/rails/myapp/public;
passenger_enabled on;
}
The Rails application lives in directory /srv/rails/myapp/, which is also a Git project directory. The directory and all its contents are owned by user rails. I've made the directory world-writeable, and, as user rails, run the following commands in it:
git pull
gem install bundler
bundle install
rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile
After this, running the application in development mode succeeds: when I command rails s, and open http://www.example.com:3000, the application works fine.
However, after doing the all the above, and commanding sudo service nginx restart, opening http://www.example.com yields a Passenger error screen, which says:
no such file to load -- bundler/setup (LoadError)
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:245:in `run_load_path_setup_code'
/usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:348:in `running_bundler'
/usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:243:in `run_load_path_setup_code'
/usr/share/passenger/helper-scripts/rack-preloader.rb:100:in `preload_app'
/usr/share/passenger/helper-scripts/rack-preloader.rb:158
There's lots of debug output below that, and I'm not sure which of it is useful, but here's an excerpt:
Application root
/srv/rails/myapp
Environment (value of RAILS_ENV, RACK_ENV, WSGI_ENV and PASSENGER_ENV)
production
Ruby interpreter command
/usr/bin/ruby
User and groups
uid=1001(rails) gid=1002(rails) groups=1002(rails)
Environment variables
SHELL = /bin/bash
PASSENGER_DEBUG_DIR = /tmp/passenger.spawn-debug.XXXX2fPMPM
USER = rails
PWD = /srv/rails/myapp
SHLVL = 0
HOME = /home/rails
LOGNAME = rails
SERVER_SOFTWARE = nginx/1.4.6
IN_PASSENGER = 1
PYTHONUNBUFFERED = 1
NODE_PATH = /usr/share/passenger/node
RAILS_ENV = production
RACK_ENV = production
WSGI_ENV = production
NODE_ENV = production
PASSENGER_APP_ENV = production
SERVER_PROTOCOL = HTTP/1.1
SCGI = 1
DOCUMENT_ROOT = /srv/rails/myapp/public
QUERY_STRING =
SERVER_NAME = www.example.com
REMOTE_PORT = 49316
REMOTE_ADDR = [redacted]
SERVER_PORT = 80
REQUEST_METHOD = GET
SERVER_ADDR = [redacted]
REQUEST_URI = /
...
General Ruby interpreter information
RUBY_VERSION = 1.8.7
RUBY_PLATFORM = x86_64-linux
RUBY_ENGINE = nil
RubyGems version = 1.8.15
The application uses Ruby v2.0.0p353 while Passenger seems to use v1.8.7; I'm not sure if that has anything to do with this problem. (The Ruby used by the app lives in /home/rails/.rvm/rubies/ruby-2.0.0-p353/.)
I've searched for other problems where Passengers outputs this error, but nothing I've tried so far fixes the problem.
I don't really know if this will help, but try, in your projects folder:
which ruby # for example /home/rails/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
And this will output which ruby your bundle is using. Put that value, replacing this:
passenger_ruby /usr/bin/ruby;
to this
passenger_ruby /home/rails/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
end
edit:
this Linux command:
echo $MY_RUBY_HOME/bin/ruby
might be the easiest and most correct way to find your ruby binary.
After trying various things, I gave up on having nginx installed from Debian packages. I removed it, and also removed my rubies and RVM, then reinstalled everything, following these directions. The article advises how to install nginx using the passenger-install-nginx-module command that comes with the passenger gem. It checks all dependencies, and if it can proceed, downloads and compiles nginx. By default, it's installed in /opt/nginx/.
This did not work immediately. I also had to create an nginx startup script; instructions here. Furthermore, I had to edit the /opt/nginx/conf/nginx.conf file to add a reference to my application, and also had to comment out the location / block. After all this, and commanding sudo service nginx restart, the site is up.

Deployed rails site on ec2 using capistrano, but it doesn't show up on the browser, here is the site -> http://passionate4.net/

I am using apache passenger to deploy rails application.
I have followed each and every step clearly and everything works fine.
No error message in deployment.
But the site doesn't show up on browser.
Here are the files
$ sudo cat /etc/apache2/sites-available/capi_app
<VirtualHost *:80>
ServerName www.passionate4.net
DocumentRoot /var/www/apps/capi_app/public
RailsEnv production
<Directory /var/www/apps/capi_app/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
I also tried to change
$ sudo cat /etc/apache2/sites-available/capi_app
<VirtualHost *:80>
ServerName www.passionate4.net
DocumentRoot /var/www/apps/capi_app/current/public
RailsEnv production
<Directory /var/www/apps/capi_app/current/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
--> With this, when i restart apache, I don't get error (/ / / public) doesn't exist, but site still doesn't show up.
#Martin
$ sudo cat /etc/apache2/sites-enabled/capi_app
<VirtualHost *:80>
ServerName www.passionate4.net
DocumentRoot /var/www/apps/capi_app/current/public
RailsEnv production
<Directory /var/www/apps/capi_app/current/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
----------- deploy.rb file -------------
require 'bundler/capistrano'
#using RVM!
$:.unshift("#{ENV["HOME"]}/.rvm/lib")
require "rvm/capistrano"
set :rvm_type, :user
set :application, "capi_app"
set :deploy_to, "/var/www/#{application}"
role :web, "50.18.155.154" # Your HTTP server, Apache/etc
role :app, "50.18.155.154" # This may be the same as your `Web` server
role :db, "50.18.155.154", :primary => true # This is where Rails migrations will run
default_run_options[:pty] = true
set :repository, "git#github.com:jaipratik/capi_app.git"
set :scm, :git
set :branch, "master"
set :user, "ubuntu" #if error use whats shown in podcast
set :use_sudo, false
set :admin_runner, "ubuntu"
set :rails_env, 'production' #<<<<<<<<<< NEW
#set :use_sudo, false #if error delete this
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
ubuntu#ip-10-166-185-18:~$ ls -l /var/www/apps/capi_app
total 8
lrwxrwxrwx 1 ubuntu ubuntu 46 2011-11-17 19:04 current -> /var/www/
apps/capi_app/releases/20111117190420
drwx-w---- 5 ubuntu ubuntu 4096 2011-11-17 19:04 releases
drwx-w---- 6 ubuntu ubuntu 4096 2011-11-17 09:48 shared
ubuntu#ip-10-166-185-18:~$
I placed the application at this particular location var/www/myapplication.
This resolved the error "Symbolic link not allowed or link target not accessible". I will put all the steps I took to resolve this issue.
In addition to this i'll also blog on how to resolve errors involved with first time deployment of rails 3.1 to ec2. here ->
http://recipe4developer.wordpress.com/2011/11/19/correcting-errors-in-first-time-rails-3-1-deployment-to-ec2/
Make sure that your vhost lives somewhere in /etc/apache2/sites-available, and then make sure to add it to apache, either by using the a2ensite command, or by manually symlinking /etc/apache2/sites-enabled/your_vhost_name_here to whatever you called the site.
Possible commands to enable the site (use either one):
sudo a2ensite your_vhost_name
sudo ln -s /etc/apache2/sites-available/your_vhost_name /etc/apache2/sites-enabled
After you've done this, make sure to reload apache, by running the following command:
sudo service apache2 restart
Is there a reason you're not using Heroku? You get EC2 with very easy deployment.

VirtualHost problem with passenger (mod_rails)

I'm at my wit's end here with virtual hosting. I'm trying to install redmine and it works with the webrick test server, but when I tried to use passenger (mod_rails) to host and go to the address I specified when in the virtualhost part of my apache config file nothing happens. Here is the relavent section of /etc/httpd/conf/httpd.conf where I try to set up the virtual host:
<VirtualHost *:80>
SetEnv RAILS_ENV production
ServerName redmine.MYSITE.com:80
DocumentRoot /opt/redmine-1.0.5/public/
<Directory /opt/redmine-1.0.5/public/>
Options -MultiViews
Allow from all
AllowOverride none
</Directory>
However, when I got to redmine.MYSITE.com:80 nothing happens, I just get our normal home page. I have no idea what the problem is, any help our guidance would be greatly appreciated. If you need any other information, please tell me and I'll provide it.
It took me a while to get Redmine running under Passenger. This is what I have
Install passenger:
passenger-install-apache2-module
Edit Apache confg file:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /usr/bin/ruby
Create a new directory in the default server:
<Directory>
/webserver_root/html/redmine
RailsBaseURI /redmine
AllowOverride all
Options -MultiViews
</Directory>
Lastly, make a link to the physical directory where your ruby apps live (ie NOT under /html or whatever directory you serve most Apache pages from. This MUST be a link to redmine's public subdirectory)
ln -s /webserver_root/ruby_apps/redmine/public /webserver_root/html/redmine
So Apache now tries to show all the public pages as normal, but all other pages get routed through passenger. You access it as a subfolder - eg. http://mywebserver/redmine
Your VirtualHost *:80 directive must be matched by a corresponding NameVirtualHost *:80 earlier in your config.
You can ask apache what it makes of your vhosts with the -S switch. Of course, the name of your server binary depends somewhat on how it was installed and on what OS/Distro.

Resources