Capifony 和目录所有者

发布于 2024-12-24 19:21:30 字数 1450 浏览 4 评论 0原文

当我cap部署我的Symfony2项目,然后登录到我的服务器时,我看到开发版本(app_dev.php)运行正常,但产品版本(app.php)运行正常。

错误是

[Tue Jan 03 14:31:48 2012] [error] [client xxx.xxx.xxx.xxx] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Failed to write cache file "/var/www/example/prod/releases/20120103202539/app/cache/prod/classes.php".' in /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache:1079\nStack trace:\n#0 /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache(1017): Symfony\\Component\\ClassLoader\\ClassCollectionLoader::writeCacheFile('/var/www/example/p...', '<?php  ????name...')\n#1 /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache(682): Symfony\\Component\\ClassLoader\\ClassCollectionLoader::load(Array, '/var/www/example/p...', 'classes', false, false, '.php')\n#2 /var/www/example/prod/releases/20120103202539/web/app.php(10): Symfony\\Component\\HttpKernel\\Kernel->loadClassCache()\n#3 {main}\n  thrown in /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache on line 1079

查看最近部署的缓存目录,我看到:

drwxrwxrwx 4 root     root     4096 Jan  3 14:28 .
drwxrwxr-x 5 root     root     4096 Jan  3 14:28 ..
drwxr-xr-x 6 www-data www-data 4096 Jan  3 14:28 dev
drwxrwxr-x 7 root     root     4096 Jan  3 14:28 prod

我可以使用 chown -R www-data.www-data prod/ 修复问题,但我想知道是否可以阻止这种情况在首位?为什么这些目录有不同的所有者?

When I cap deploy my Symfony2 project, then log into my server I see that the the dev (app_dev.php) runs ok but the prod version (app.php) does not.

The error is

[Tue Jan 03 14:31:48 2012] [error] [client xxx.xxx.xxx.xxx] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Failed to write cache file "/var/www/example/prod/releases/20120103202539/app/cache/prod/classes.php".' in /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache:1079\nStack trace:\n#0 /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache(1017): Symfony\\Component\\ClassLoader\\ClassCollectionLoader::writeCacheFile('/var/www/example/p...', '<?php  ????name...')\n#1 /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache(682): Symfony\\Component\\ClassLoader\\ClassCollectionLoader::load(Array, '/var/www/example/p...', 'classes', false, false, '.php')\n#2 /var/www/example/prod/releases/20120103202539/web/app.php(10): Symfony\\Component\\HttpKernel\\Kernel->loadClassCache()\n#3 {main}\n  thrown in /var/www/example/prod/releases/20120103202539/app/bootstrap.php.cache on line 1079

Looking at the recently deployed cache directory I see:

drwxrwxrwx 4 root     root     4096 Jan  3 14:28 .
drwxrwxr-x 5 root     root     4096 Jan  3 14:28 ..
drwxr-xr-x 6 www-data www-data 4096 Jan  3 14:28 dev
drwxrwxr-x 7 root     root     4096 Jan  3 14:28 prod

I can fix the issue with chown -R www-data.www-data prod/ but I wondered if I can stop this from happening in the first place? And why do the directories have different owners?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-12-31 19:21:30

发生这种情况是因为您的网络服务器由用户运行,该用户无法写入刚刚创建的 cache/prod 目录。

我知道并使用两种解决方案。首先,添加额外的命令以在部署到 Capfile 后运行。 Capfile 会喜欢这样:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/bundles/*/*/recipes/*.rb'].each { |bundle| load(bundle) }
load Gem.find_files('symfony2.rb').last.to_s

after "deploy:finalize_update" do
  run "sudo chown -R www-data:www-data #{latest_release}/#{cache_path}"
  run "sudo chown -R www-data:www-data #{latest_release}/#{log_path}"
  run "sudo chmod -R 777 #{latest_release}/#{cache_path}"
end

load 'app/config/deploy'

第二种解决方案更优雅。您指定正确的用户,该用户可以写入deploy.rb中的缓存,并确保您不使用sudo:

set :user, "anton"
set :use_sudo, false

This happens because your web-server is running by user, who is not able to write to just created cache/prod directory.

There are two solutions, which I know and use. First, add extra commands to run after deployment to Capfile. Capfile will like this:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/bundles/*/*/recipes/*.rb'].each { |bundle| load(bundle) }
load Gem.find_files('symfony2.rb').last.to_s

after "deploy:finalize_update" do
  run "sudo chown -R www-data:www-data #{latest_release}/#{cache_path}"
  run "sudo chown -R www-data:www-data #{latest_release}/#{log_path}"
  run "sudo chmod -R 777 #{latest_release}/#{cache_path}"
end

load 'app/config/deploy'

Second solution is more elegant. You specify correct user, who can write to cache in deploy.rb and make sure that you don't use sudo:

set :user, "anton"
set :use_sudo, false
给我一枪 2024-12-31 19:21:30

在 capifony 的最新版本中,他们添加了设置可写目录的选项。
这是官方文章,解释了我在下面写的内容: http://capifony.org/cookbook/ set-permissions.html

您必须使用 sudo 进行部署(不是一个好的做法,但它可以完成工作)

set   :use_sudo,      false
# To prompt the sudo password
default_run_options[:pty] = true

并告诉 capifony 哪些文件使缓存和日志文件夹可写:(

set :writable_dirs,     ["app/cache", "app/logs"]
set :webserver_user,    "www-data"
set :permission_method, :acl

您必须在你的机器,或者使用 :chwon 而不是 :acl)

编辑
我刚刚意识到这还不够,“set_permissions”任务不会自动调用,所以你必须显式运行

cap deploy:set_permissions

或在你的deploy.rb中添加这一行:

before "deploy:restart", "deploy:set_permissions"

In the last version of capifony, they've added the option to set writable directories.
Here's the official article which explains what I've written below : http://capifony.org/cookbook/set-permissions.html

You have to deploy using sudo (not a good practice, but it gets the job done)

set   :use_sudo,      false
# To prompt the sudo password
default_run_options[:pty] = true

and tell capifony which files to make cache and logs folder writable :

set :writable_dirs,     ["app/cache", "app/logs"]
set :webserver_user,    "www-data"
set :permission_method, :acl

(you have to install acl on your machine, or use :chwon instead of :acl)

EDIT :
I've just realized that this is not enough, the "set_permissions" task is not automatically called, so you have to explicitly run

cap deploy:set_permissions

Or add this line in your deploy.rb :

before "deploy:restart", "deploy:set_permissions"
栩栩如生 2024-12-31 19:21:30

我通过将缓存文件夹添加到共享文件夹解决了这个问题。

set :shared_children,     [app_path + "/cache", app_path + "/logs", web_path + "/uploads", "vendor"]

这样部署时每次都不会重新创建目录,所以不存在权限问题。

I solved this problem by adding cache folder to shared folders.

set :shared_children,     [app_path + "/cache", app_path + "/logs", web_path + "/uploads", "vendor"]

This way the directory is not recreated each time during deployment, so there is no problem with permissions.

﹎☆浅夏丿初晴 2024-12-31 19:21:30

是的,不需要每次部署后重新创建缓存,这个解决方案是合乎逻辑且实用的。

Anton 的第二个解决方案 - 如果您在开发环境中缓存文件夹权限为 true,则可以使用

Yes, don't need recreate cache every time after deploy, this solution is logical and pragmatical.

Second solution from Anton - is work if you cache folder permission true in develop environment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文