Rails 3.1 部署到生产(使用 Apache 和 Passenger)资产问题
Rails 3.1 改变了处理资产管道的方式,并且在部署到生产时导致问题。
我正在使用 Apache 和 Passenger,它们似乎工作得很好。
我的制作是这样设置的(目前)。
# 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
我在 Ubuntu 上运行 rake asset:precompile 并启动服务器。而且……什么也没有。我的图像均未加载。
传说中的“我在此 URL 找不到图像”框。
我在 CentOS 上运行 rake asset:precompile 并启动服务器。还有...权限错误。
*Error Compiling CSS Asset*
Errno::EACCESS: Permission Denied - [app path]/tmp/cache/assets/E95
[path to RVM Ruby]/fileutils.rb:243:in 'mkdir'
我无法让它移动。非常感谢任何帮助。谢谢你!
更新
这个解决方案每次对我来说都有效:
首先清理你的资产
rm -rf public/assets
,
rake assets:clean RAILS_ENV=production
其次,在#development.rb中更改
config.assets.compile = false
为
config.assets.compile = true
第三,运行以预编译你的资产
rake assets:precompile RAILS_ENV=production
第四,在#development.rb中更改
config.assets.compile = true
回
config.assets.compile = false
第五,通过运行以下命令重新启动服务器:
touch tmp/restart.txt
取消对新创建资产的权限限制
第六,通过运行此命令chmod -R 777 public/assets
第七,庆祝!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个简单的权限问题。授予服务器/守护进程在 [app_path]/tmp 中递归创建文件的权限。
假设您的服务器进程与您执行此操作的
www-data
用户一起运行:如果该目录不属于该用户,您必须更改所有权:
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:and if the directory does not belong to the user you have to change the ownership:
尝试通过 sudo 创建
public/assets
或尝试执行rvmsudo rake assets:precompile
- 本质上,它无法在您的服务器上创建目录 - 因此会出现错误。Try creating
public/assets
via sudo or try performingrvmsudo rake assets:precompile
- essentially, it's not able to create the directory on your server — hence the error.在 Windows 8 上:
On Windows 8:
您更新的解决方案对我不起作用。
我使用的是 Rails 4.2,CSS 和 JS 仅在我设置时才起作用
config.serve_static_files = true (不推荐,但这是我可以让事情在这里工作的唯一方法)。
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).