将Symfony2应用程序部署到AWS Elastic Beanstalk-部署后缓存清除


Deploying Symfony2 Application to AWS Elastic Beanstalk - Post Deployment Cache Clearing

将Symfony2应用程序部署到Elastic Beanstalk时,我需要执行手动缓存清除,以便应用程序开始运行。因此,我添加了一个容器命令来在部署期间清除prod缓存。命令运行,但我仍然必须手动清除才能使应用程序工作。

经过一番挖掘,我发现Symfony2缓存文件本身中存在绝对路径字符串。我添加的命令运行"预部署"或在应用程序文件从其暂存文件夹(称为"/var/app/ondeck"(移动到其最终静止位置("/var/app/current"(之前。因此,缓存文件中的绝对路径字符串是错误的,应用程序无法加载。

此外,开发环境工作得很好,因为它会自动重建自己的缓存。生产环境是唯一受到影响的环境。

我的问题:

  • 有没有一种方法可以在代码移动到位后自动运行缓存清除命令
  • 或者,有没有办法让Symfony2让您为缓存生成指定一个不同的"基本路径"?这样它就可以被设置为指向正确的最终位置

提前感谢大家:-(

当各种Symfony命令在composer安装过程和/或您自己的命令(例如assetic:dump(结束时运行时,缓存填充在ondeck环境中,因此会出现问题。

解决方案是将清除缓存作为部署的最后一个命令,并指定--no warmum以停止Symfony自动重新填充缓存,这样当环境从ondeck移动到current时,缓存是空的。在我的.eextensions/symfony.config中,我有:

container_commands:
  01_migrate:
    command: php app/console doctrine:migrations:migrate --env=prod --no-debug --no-interaction
    leader_only: true
  02_dumpassets:
    command: php app/console assetic:dump --env=prod --no-debug
  99_clearcache:
    command: php app/console cache:clear --env=prod --no-debug --no-warmup

它没有很好的文档记录,但在环境移动到当前后,您也可以使用部署后挂钩来预热缓存。同样在.eextensions/symfony.config:中

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/01-cachewarm.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_CURRENT
      php app/console cache:warmup --env=prod --no-debug      

这个问题在最新的Symfony发行版中得到了修复2.3.23、2.5.8、2.6.1和即将推出的版本(2.7.、3.、…(

请参阅https://github.com/heroku/heroku-buildpack-php/issues/64

Symfony现在将在容器中使用与FILE相关的链接,因此您不再需要使用部署后破解。请注意,您仍然需要将资产安装为硬链接。

我花了一些时间试图让事情以可重用的方式运行,这是我们最终确定的ebextensions文件。

files:
  "/tmp/parameters.yml":
    mode: "000444"
    content: |
      parameters:
        database_driver: pdo_mysql
        database_host: '...'
        database_port: null
        database_name: ...
        database_user: ...
        database_password: ...
        mailer_transport: smtp
        mailer_host: 127.0.0.1
        mailer_user: null
        mailer_password: null
        locale: en
        secret: ...
option_settings:
  - namespace: aws:elasticbeanstalk:hostmanager
    option_name: LogPublicationControl
    value: true
  - namespace: aws:elasticbeanstalk:container:php:phpini
    option_name: document_root
    value: /web
  - namespace: aws:autoscaling:launchconfiguration # This is for permission to the RDS instance
    option_name: SecurityGroups
    value: ...
container_commands:
  01-params:
    command: cp /tmp/parameters.yml app/config/parameters.yml
  02-params:
    command: chown webapp:webapp app/config/parameters.yml
  03-bootsrap:
    command: php vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php
  04-bootsrap:
    command: chown webapp:webapp app/bootstrap.php.cache
  05-cache:
    command: php app/console cache:clear --env=prod --no-debug --no-warmup
  06-cache:
    command: chmod -R 0777 app/cache
  07-logs:
    command: chmod -R 0777 app/logs
  08-cache:
    command: chown -R webapp:webapp app/cache
  09-logs:
    command: chown -R webapp:webapp app/logs

我遇到了同样的问题,我在这个页面上找到了解决方案

我的最后一个代码是-我使用的是Symfony Framework(v2.8(:

commands:
  01_create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/initial_cron_job.sh":
  mode: "000755"
  owner: root
  group: root
  content: |
    #!/usr/bin/env bash
    # This line was very important for me as I needed to get EnvVars
    . /opt/elasticbeanstalk/support/envvars
    rm -rf /var/app/current/app/cache/* /var/app/current/app/logs/*
    chmod -R 777 /var/app/current/app/cache
    chmod -R 777 /var/app/current/app/logs
    su -c "command_name" $EB_CONFIG_APP_USER

释义:

  • 我不需要预热缓存,所以我在缓存日志文件夹上执行rm-rf
  • 我更改缓存和日志文件夹权限,以100%确保它们对$EB_CONFIG_APP_USER是可写的-在这种情况下webapp用户

是否尝试覆盖getCacheDir?

app/AppKernel.php中:

public function getCacheDir()
{
    return "/path/to/the/cache";
}

基于@rhunwicks的答案,这是我的.ebextensions/symfony.config:

commands:
  01updateComposer:
    command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/01-cachewarm.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_CURRENT
      php app/console cache:clear --env=prod
      php app/console assetic:dump --env=prod

确保您的composer中没有"symfony-assets-install": "symlink",这样您就不用花几个小时来弄清楚符号链接指向/var/app/ondeck/src/BundleName/MyBundle/Resources/public而不是current文件夹的原因。