Centos+Apache+passenger+rails: running rails apps

2019-07-12 14:38发布

问题:

I'm trying to learn Rails, so I'm new, and would like a little help.

I'm trying to run multiple apps at once, and I'm having an odd problem. When I try to generate things with rails, it gives me a permissions error. I've looked around stackoverflow and several other sites and haven't found any good solutions.

Details: This setup successfully runs two rails apps in these locations:

  1. http://rails.mechapps.co/
  2. http://slide.mechapps.co/

2 currently shows an error because of a missing controller (4/19/2014 12:30am). However, when I navigate to that app's directory, and run 'rails generate controller slideshow view' I get this error:

$ rails generate controller slideshow create
/usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/env.rb:68:in `initialize': Permission denied @ rb_sysopen - /tmp/spring/bff8002d6fbedba8d81e3befe2342559.pid (Errno::EACCES)
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/env.rb:68:in `open'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/env.rb:68:in `open'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/env.rb:68:in `server_running?'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:18:in `call'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/command.rb:7:in `call'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/rails.rb:23:in `call'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/command.rb:7:in `call'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client.rb:26:in `run'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/bin/spring:48:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/binstub.rb:11:in `load'
from /usr/local/rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/binstub.rb:11:in `<top (required)>'
from /var/www/html/slide.mechapps.co/slide/bin/spring:16:in `require'
from /var/www/html/slide.mechapps.co/slide/bin/spring:16:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

Here's my setup: /etc/http/conf/httpd.conf:

...
LoadModule passenger_module /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41
PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.1.1/wrappers/ruby
</IfModule>
...
RackEnv development
<VirtualHost *:80>
    ServerName rails.mechapps.co
    DocumentRoot /var/www/html/rails.mechapps.co/blog/public
        <Directory /var/www/html/rails.mechapps.co>
                AllowOverride all
                Options -MultiViews
        </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName slide.mechapps.co
    DocumentRoot /var/www/html/slide.mechapps.co/slide/public
        <Directory /var/www/html/slide.mechapps.co>
                AllowOverride all
                Options -MultiViews
        </Directory>
</VirtualHost>

My Root Directory's permissions:

drwxr-xr-x [user] [group] 4096 Apr 18 23:17 rails.mechapps.co
drwxr-xr-x [user] [group] 4096 Apr 19 04:22 slide.mechapps.co

The App Directory's permissions:

-rw-rw-r-- 1 [user] [group] 1337 Apr 19 04:22 Gemfile
-rw-rw-r-- 1 [user] [group] 2982 Apr 19 04:22 Gemfile.lock
-rw-rw-r-- 1 [user] [group]  478 Apr 19 04:22 README.rdoc
-rw-rw-r-- 1 [user] [group]  249 Apr 19 04:22 Rakefile
drwxrwxr-x 8 [user] [group] 4096 Apr 19 04:22 app
drwxr-xr-x 2 [user] [group] 4096 Apr 19 04:22 bin
drwxrwxr-x 5 [user] [group] 4096 Apr 19 04:26 config
-rw-rw-r-- 1 [user] [group]  154 Apr 19 04:22 config.ru
drwxrwxr-x 2 [user] [group] 4096 Apr 19 04:24 db
drwxrwxr-x 4 [user] [group] 4096 Apr 19 04:22 lib
drwxrwxr-x 2 [user] [group] 4096 Apr 19 04:22 log
drwxrwxr-x 2 [user] [group] 4096 Apr 19 04:22 public
drwxrwxr-x 8 [user] [group] 4096 Apr 19 04:22 test
drwxrwxr-x 3 [user] [group] 4096 Apr 19 04:22 tmp
drwxrwxr-x 3 [user] [group] 4096 Apr 19 04:22 vendor

I am logged in as the owner of both locations.

I suspect the following problems:

  1. Running multiple rails Apps at once is confusing the rails generator
  2. I somehow messed up a ruby/rails configuration and the apps are conflicting
  3. Ruby was installed as root, and my user was added to the rvm group. Perhaps this is a rvm issue?

If you know how to fix this, or have any ideas, please help. I've spent almost 8 hours searching the internet and trying various configurations. Thanks!

回答1:

Ok, I figured this out, and I've edited the question title to clarify.

My problem was, I was putting my rails apps int the /var/www/ directory of my CentOS server. Aparantly CentOS has separate security measures for that folder, and rails was unable to make it's modifications correctly.

I moved the files from the www folder to /apps/... and pointed my VirtualHosts there, and everything has been smooth sailing since.

In addition, I realized that you never want to install rails apps in the www folder due to security issues. CentOS is set to automatically treat your www folder as open to the public. A user could probably get Apache to serve up your rails application files. Not good.



回答2:

Running multiple apps at once doesn't confuse the generator - the generator has to be part of an app instance

Rails is a gem, which means it will only act in its own environment -- having multiple apps on the same server is like saying you can only develop one app on your system

If you're using SSH to perform these commands, are you sure you have the correct permissions? Apache runs its shell with the user www-data - you've not detailed how you're calling these commands, but if it's from ssh - are you sure your user has the right permissions?

I somehow messed up a ruby/rails configuration and the apps are conflicting

The apps will only conflict if they are in the same folder. This is how Rails treats the generator - by loading the environment as it needs to run. As long as you keep your Rails apps in individual folders, you'll be safe and sound

Ruby was installed as root, and my user was added to the rvm group. Perhaps this is a rvm issue?

I don't think it's RVM - I think it's a permissions problem. Looking online, looks like you need to chmod your tmp directory to make it accessible & writeable