将Symfony-CMF项目部署到Heroku

时间:2016-01-31 11:12:21

标签: symfony heroku symfony-cmf

我目前正在关注CMF文档来创建项目:https://symfony.com/doc/master/cmf/tutorial/introduction.html

随着我继续使用Tutorial,我想将项目推送到heroku。但是当我需要数据库连接时,我遇到了问题。

我找到了这个来源:https://coderwall.com/p/qpitzq/deploing-symfony-project-using-mysql-to-heroku

这有帮助,但我还需要配置' phpcr_backend'参数。我在控制台上设置它们:

heroku config:set phpcr_backend=[type:doctrinedbal,connection:default]
or
heroku config:set phpcr_backend=(type:doctrinedbal,connection:default)
or 
heroku config:set phpcr_backend={type:doctrinedbal,connection:default}
or
heroku config:set phpcr_backend=type:doctrinedbal,connection:default

heroku config:set phpcr_workspace=default
heroku config:set phpcr_user=admin
heroku config:set phpcr_pass=admin

并更新parameters_production.php文件:

<?php
$db = parse_url(getenv('...'));
...
$container->setParameter('phpcr_backend', getenv('phpcr_backend'));
$container->setParameter('phpcr_workspace', getenv('phpcr_workspace'));
$container->setParameter('phpcr_user', getenv('phpcr_user'));
$container->setParameter('phpcr_pass', getenv('phpcr_pass'));

现在,当我部署项目时遇到此错误:

           [Symfony\Component\Config\Definition\Exception\InvalidTypeException]                                        

       Invalid type for path "doctrine_phpcr.session.sessions.default.backend". Expected array, but got boolean    

我不确定,我在正确的语法上设置这些参数。现在这就是问题所在。

编辑:

我在config.yml文件中硬编码了doctrine_phpcr参数:

doctrine_phpcr:
   # configure the PHPCR session
   session:
       backend: { type: doctrinedbal, connection: default}
       workspace: default
       username: admin
       password: admin
    # enable the ODM layer
   odm:
       auto_mapping: true
       auto_generate_proxy_classes: "%kernel.debug%"

目前错误是

PHP Fatal error:  Cannot use 'String' as class name as it is reserved in /tmp/build_4d5c173733f27d9fb1cec775f9522884/ersah123-cmf-testing-118c5df/vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Mapping/Annotations/String.php on line 32   

编辑2:

通过更改composer.json中的php版本,修复了最后一个问题。现在,当我部署项目构建成功时。但是有另一个问题:

 heroku run php bin/console doctrine:database:create


 [Doctrine\DBAL\Exception\ConnectionException]                                                                                   
  An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known 

您的帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

沙箱的分步说明。

首先,克隆:

git clone https://github.com/symfony-cmf/cmf-sandbox.git
cd cmf-sandbox
composer install

然后根据需要声明可选扩展名:

php -dmemory_limit=4G $(which composer) require "ext-gd:*" "ext-exif:*"
git add composer.json composer.lock
git commit -m "require gd and exif extensions"

数据库设置

在composer.json中映射(JAWSDB_|CLEARDB_)?DATABASE_URL

diff --git a/composer.json b/composer.json
index 0d880da..07a3ba8 100644
--- a/composer.json
+++ b/composer.json
@@ -91,7 +91,9 @@
         "incenteev-parameters": [
             {
                 "file": "app/config/parameters.yml",
-                "env-map": {}
+                "env-map": {
+                    "database_url": "DATABASE_URL"
+                }
             },
             {
                 "file": "app/config/phpcr.yml",

使用config.yml详细信息更新config_prod.ymldatabase_url

diff --git a/app/config/config.yml b/app/config/config.yml
index 3075825..0685fda 100644
--- a/app/config/config.yml
+++ b/app/config/config.yml
@@ -55,13 +55,6 @@ swiftmailer:
 # for jackalope-doctrine-dbal
 doctrine:
     dbal:
-        driver:   '%database_driver%'
-        host:     '%database_host%'
-        port:     '%database_port%'
-        dbname:   '%database_name%'
-        user:     '%database_user%'
-        password: '%database_password%'
-        path:     '%database_path%'
         charset:  UTF8

 # cmf configuration
diff --git a/app/config/config_prod.yml b/app/config/config_prod.yml
index aa51fbf..b704da2 100644
--- a/app/config/config_prod.yml
+++ b/app/config/config_prod.yml
@@ -17,3 +17,7 @@ monolog:
             type:  stream
             path:  '%kernel.logs_dir%/%kernel.environment%.log'
             level: debug
+
+doctrine:
+    dbal:
+        url: '%database_url%'

...并更新并提交:

php -dmemory_limit=4G $(which composer) update --lock
git add composer.json composer.lock app/config/config.yml app/config/config_prod.yml
git commit -m "map DATABASE_URL"

登录

使用日志记录详细信息更新config_prod.yml

diff --git a/app/config/config_prod.yml b/app/config/config_prod.yml
index b704da2..755cff9 100644
--- a/app/config/config_prod.yml
+++ b/app/config/config_prod.yml
@@ -15,7 +15,7 @@ monolog:
             handler:      nested
         nested:
             type:  stream
-            path:  '%kernel.logs_dir%/%kernel.environment%.log'
+            path:  'php://stderr'
             level: debug

 doctrine:

...并提交:

git add app/config/config_prod.yml
git commit -m "log to stderr in prod"

PHPCR配置需要在repo中:

cp app/config/phpcr_doctrine_dbal.yml.dist app/config/phpcr.yml
sed -i '' '/phpcr.yml/d' .gitignore
git add app/config/phpcr.yml
git commit -m "PHPCR config"

创建Procfile

echo 'web: $(composer config bin-dir)/heroku-php-apache2 web/' > Procfile
git add Procfile
git commit -m "Heroku Procfile"

部署

heroku create
heroku config:set SYMFONY_ENV=prod
heroku addons:create heroku-postgresql
git push heroku master

init DB

heroku run "php app/console doctrine:phpcr:init:dbal --force"
heroku run "php app/console doctrine:phpcr:workspace:create default"
heroku run "php app/console doctrine:phpcr:repository:init"
heroku run "php app/console -v -n doctrine:phpcr:fixtures:load"

完成!

heroku open
相关问题