Rails not loading CSS/javascript/images on ISP server - ruby-on-rails

I have a Rails app that works fine on my local environment. But when I upload it to my ISP the app is not loading any of the assets in the public directory. Because the ISP uses .htaccess rewrites for Rails apps, I suspect this is the problem, but I'm pretty green on that sort of thing.
On the server, my rails app is deployed at ~/etc/rails_apps/myapp
In my home dir there's a symlink www -> etc/rails_apps/myapp/public
Isn't the idea that the symlink loads the assets from etc/rails_apps/myapp/public? Am I missing something else?
The server is Mongrel, if that matters.
The public/stylesheets and public/javascripts dirs both have 755 permissions, if that matters.

Please specify the Hosting environment that you are using whether its shared host like Bluehost or Hosmonster or Dedicated Hosting Service .
If you are hosting in a Shared hosting environment , Then your rails root directory should be in /home/user-name/rails_apps. Since the shared host provider doesn't allow you to change the permission of the /etc folder .
if your railsapps is in /home/user-name/rails_apps/yourapp then in the created symbolic link chmod 777 permission to the created symobilic folder which will be your domain name inside the public_html folder .
ln -s ~/rails/your-rails-app-name/public your-domain-name-without-extension
example : ln -s ~/rails/your-rails-app-name/public stackoverflow
$ chmod 777 /public_html/stackoverflow
Hope this helps !

Related

Rails / Carrierwave / GIT / nginx / Capistrano - can't create a directory in git releases folder

I'm using carrierwave in a rails app to upload files. It works fine on my development environment, but on my production VM (Ubuntu), I'm getting this error:
An Errno::EACCES occurred in users#update:
Permission denied - /home/yards/apps/yardsapp/releases/20130616143623/public/uploads/tmp/20130616-1438-14186-3184
/usr/local/lib/ruby/1.9.1/fileutils.rb:244:in `mkdir'
I'm pretty sure I understand what is going on, but I can't seem to figure out a fix. My capistrano deploy.rb is set up with the user as root. So when it creates the new release folder on a deploy, the access rights are for root (I think).
Then when I try to upload a file, I get that error because nginx is trying to execute a mkdir as www-data.
I could chown the folder after the deploy and it works...but then another deploy creates another new directory with owner set to root as default.
At least I think this is what is going on. Does anyone have any ideas on how I should be doing this?
Run your deployment as www-data. You might need to adjust the authorized_keys file for the www-data user as well to be able to connect.
To fastest way would be to copy over your authorized_keys file for whatever user you are using at the moment (assuming you are root):
mkdir $WWW_DATA_HOME/.ssh
cp ~/.ssh/authorized_keys $WWW_DATA_HOME/.ssh/authorized_keys
chown www-data:www-data $WWW_DATA_HOME/.ssh/authorized_keys
You might also need to change the shell for the www-data user to log in to it:
chsh -s /bin/bash www-data
Now you should be able to do
ssh www-data#your-host.tld
and log in.
What this came down to was an improper Capistrano configuration. I followed the capistrano docs correctly (and made a 'deployer' user, same thing as the www-data as suggested above) and I have capistrano working like a charm. Also upgraded to Capistrano 3.

How should I set up permission for Rails app?

Currently I set 0777 to all the directories and files.
However, I'm scared of being accessed from others.
Log files and all the controllers, models, views, and the files in config are set to 0777
In general, how they are supposed to be set?
Log files directory and its files
controller files
model files
view fies
the files in config directory
You should definitely not use 0777 for your file permissions. This more easily exposes you to vulnerabilities.
In general, follow this principle:
For folders, use 0755, which equates to rwxr-xr-x. The execute permission allows folder contents to be viewed.
find /your/rails/dir -type d -exec chmod 755 {} +
For executed scripts, also use 0755. This allows anyone to execute the scripts, but not make changes (write) to them.
For all other files, use 0644 which equates to rw-r--r--. This allows everyone to read the file, the owner to write to the file, and no one to execute the file. This prevents, among other things, malicious scripts from being uploaded and executed.
find /your/rails/dir -type f -exec chmod 644 {} +
Optionally, files containing passwords you could consider more restrictive permissions on, especially config/database.yml or any files containing passwords for things like mail services (mandrill, sendgrid, postmark), Amazon S3 buckets, or Redis connections. For these files you might use 0600.
In a production environment, your rails app should be running as the same user (not root) that owns all of these files. This is accomplished most easily by using passenger, unicorn, or running a web server such as mongrel or webrick as the local user listening on a port such as localhost:3000, and having Apache or Nginx reverse proxy to localhost:3000.

How to Move A Ruby On Rails Website To New Server

I have a Ruby on Rails website which I have successfully tar the apps directory which included the current folder ect. Which I Wget to transfer the files over. The server I have moved to is setup to run Ruby on Rails but is there anything else I need todo to get it running?
Any commands via SSH?
My new server setups is Ubuntu 11.04 running ISPConfig 3 as the server admin.
Current the file are in the correct location with the correct permissions and owners. But all I'm getting is the default ISPConfig page.
(This is the default index page of your website.
This file may be deleted or overwritten without any difficulty. This is produced by the file index.html in the web directory.)
If anyone can point me in the right direction that would be great.
Have you tried creating a symbolic link on the server to the application something like
cd ~/public_html;
ln -s ~/rails-test-application/public rails3
http://www.site5.com/blog/programming/ruby-rails/how-to-deploy-phusion-passenger-to-a-subdirectory-routing-errors-and-restarting/20090414/

Rails/Passenger/Nginx user permission errors

My Rails app is having trouble writing into it's public/ directory. I've setup nginx with user root;, the capistrano recipe I'm using also is using root when connecting via ssh.
To fix this I made capistrano run chmod o+w -R #{current_path}/ but I don't think this is a good solution. What am I missing?
According to Phusion Passenger's documentation:
Under no circumstances will applications be run as root. If
environment.rb/config.ru is owned as root or by an unknown user, then
the Rails/Rack application will run as the user specified by
passenger_default_user and passenger_default_group.
http://modrails.com/documentation/Users%20guide%20Nginx.html#user_switching

Can my /public directory be a symlink with rails 3 + passenger 3 + nginx 0.8?

I'm putting together a rails deployment where the public directory is a
symlink to another directory on the system. This is with passenger 3 on
nginx .8. It does't seem to like that setup. Nginx always follows symlinks by default, so AFAIK it's not a matter of doing the equivalent of Apache's +FollowSymLinks.
update
Looks like this is covered here: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#application_detection
Note that Phusion Passenger for Nginx
does not resolve any symlinks in the
root path. So for example, suppose
that your root points to
/home/www/example.com, which in turn
is a symlink to
/webapps/example.com/public. Phusion
Passenger for Nginx will check for
/home/www/config/environment.rb, not
/webapps/example.com/config/environment.rb.
This file of course doesn’t exist, and
as a result Phusion Passenger will not
activate itself for this virtual host,
and you’ll most likely see some output
generated by the Nginx default
directory handler such as a Forbidden
error message.
Detection of Rack applications happens
through the same mechanism, exception
that Phusion Passenger will look for
config.ru instead of
config/environment.rb.
So I wonder if some proper symlinking of config.ru might do the trick.
Can't you use the following:
mount --bind /original_path /your_ngnix_root_path
instead of using symlinks? For ngnix it would be a usual directory, why it will point to another directory as you wanted it to be.
I think it is not possible to use a symlinked public directory. The only workaround I can imagine is to symlink any file and directory inside of the public dir.
# public folder: /data/public
# app folder: /webapp/
mkdir -p /webapp/public && ln -sf /data/public/* /webapp/public/
For every new file or directory in /data/public you have to run this command again.
I was able to do this with the guide linked below.
Create your rails app in /var/www/html
Delete default.
Edit the /etc/nginx/nginx.conf file
symlink to sites-enabled
Restart nginx
How to Deploy Ruby on Rails with Passenger and Nginx on Ubuntu 16.04

Resources