Rails 3.1 Deploy to Production (with Apache & Passenger) Asset Problems
Asked Answered
H

4

10

Rails 3.1 has changed the way it handles the asset pipeline and it is causing issues when deploying to production.

I am using Apache and Passenger, which seem to work fine.

My production is setup like this (for now).

# congif/environments/production.rb
config.cache_classes = false
config.consider_all_requests_local       = true
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

I run rake assets:precompile on Ubuntu and start server. And... nothing. None of my images load.

The legendary 'I can't find an image at this URL' box.

I run rake assets:precompile on CentOS and start server. And... permission errors.

*Error Compiling CSS Asset*
Errno::EACCESS: Permission Denied - [app path]/tmp/cache/assets/E95
[path to RVM Ruby]/fileutils.rb:243:in 'mkdir'

I can't get it to budge. Any help is greatly appreciated. Thank you!

UPDATE

This solution has worked every time for me:

First Clean out your Assets

rm -rf public/assets

and

rake assets:clean RAILS_ENV=production

Second, in #production.rb change

config.assets.compile = false

to

config.assets.compile = true

Third, run to precompile your assets

rake assets:precompile RAILS_ENV=production

Fourth, in #production.rb change

config.assets.compile = true

back to

config.assets.compile = false

Fifth, restart your server by running:

touch tmp/restart.txt

Sixth, un-restrict permissions on your newly created assets by running this command

chmod -R 777 public/assets

Seventh, celebrate!!

Harping answered 27/10, 2011 at 18:6 Comment(0)
C
8

That's a simple permission problem. Give the server/daemon the right to create files in [app_path]/tmp recursively.

Assuming your server process runs with the www-data user you do this with:

cd APP_PATH
chmod -R u+w tmp

and if the directory does not belong to the user you have to change the ownership:

chown -R www-data tmp
Candide answered 27/10, 2011 at 18:56 Comment(4)
I tried this and it is still fighting me with the same error. I am on CentOS and www-data isn't the valid user. I tried it the the apache user. Update: I did chown -R 755 tmp and it worked!Harping
Ok you fixed my CentOS error and Michael de Silva fixed my Ubuntu error. Thanks guys!Harping
how do I do this on Heroku? I have the same problems where an image is giving me the access-denied treatmentJustice
i made the changes locally and pushed it onto heroku. everything works fine nowJustice
G
4

Try creating public/assets via sudo or try performing rvmsudo rake assets:precompile - essentially, it's not able to create the directory on your server — hence the error.

Goins answered 27/10, 2011 at 18:20 Comment(1)
Ok you fixed my Ubuntu error and topek fixed my CentOS error. Thanks guys!Harping
C
0

On Windows 8:

  1. Remove references to stylesheets
  2. Restart production
  3. Go to an affected page using browser
  4. Add stylesheet references back
  5. Restart production
  6. Worked for me!
Columba answered 11/11, 2013 at 16:2 Comment(0)
D
0

Your updated solution did not work for me. I am on rails 4.2 and css and js works only when I set config.serve_static_files = true (which is not recommended but it is the only way I can make things work here).

Deploy answered 2/6, 2016 at 2:24 Comment(1)
This doesn't seem to be an answer. This might be better posted as a comment.Disgust

© 2022 - 2024 — McMap. All rights reserved.