Php 为什么我在将Symfony 4应用程序部署到Heroku时遇到问题?

Php 为什么我在将Symfony 4应用程序部署到Heroku时遇到问题?,php,symfony,heroku,deployment,Php,Symfony,Heroku,Deployment,我正试图将我的Symfony 4应用程序部署到Heroku,显然,它遇到了一些问题 我第一次出现“403禁止”错误,因为我的Procfile中没有设置文档根。 我的答案是,在阅读了我能找到的其他问题和答案后,使用以下任何一种: web: $(composer config bin-dir)/heroku-php-apache2 public/ web: bin/heroku-php-apache2 public/ web: vendor/bin/heroku-php-apache2 public

我正试图将我的Symfony 4应用程序部署到Heroku,显然,它遇到了一些问题

我第一次出现“403禁止”错误,因为我的Procfile中没有设置文档根。 我的答案是,在阅读了我能找到的其他问题和答案后,使用以下任何一种:

web: $(composer config bin-dir)/heroku-php-apache2 public/
web: bin/heroku-php-apache2 public/
web: vendor/bin/heroku-php-apache2 public/
web: vendor/bin/heroku-php-apache2 /public/
他们都返回了不同类型的“500个内部服务器错误”页面,其中大多数页面表示,由于可能的配置错误,
请求超出了10个内部重定向的限制。

这让我想到了我的
public/.htaccess
文件中的一些问题

删除注释后,其外观如下所示:

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>
#将前控制器用作索引文件。在以下情况下,它可作为备用解决方案:
#每隔一次重写/重定向都会失败(例如,在没有
#修改(重写)。此外,这还减少了对象的匹配过程
#起始页(路径“/”),否则Apache将应用重写规则
#到每个配置的DirectoryIndex文件(例如index.php、index.html、index.pl)。
DirectoryIndex app.php
#默认情况下,如果未启用此功能,Apache不会计算符号链接
#服务器配置中的功能。如果需要,请取消注释以下行
#将资产安装为符号链接,或者如果遇到与符号链接相关的问题
#编译更少的/Sass/CoffeScript资产时。
#选项如下符号链接
#禁用多视图可防止不必要的协商,例如“/app”不应解决
#到前端控制器“/app.php”,但被重写为“/app.php/app”。
选项-多视图
重新启动发动机
#自动确定RewriteBase并将其设置为环境变量。
#如果您使用Apache别名进行大规模虚拟托管或安装
#在子目录中的项目中,将在基本路径前面加上前缀,以允许正确的
#解析app.php文件并重定向到正确的URI。会的
#也可以在没有路径前缀的环境中工作,提供一个安全的单一大小
#适用于所有解决方案。但是,在这种情况下,您不需要它,您可以进行评论
#以下两条线路可消除开销。
RewriteCond%{REQUEST_URI}:$1^(/.+)/(.*):\2$
重写规则^(.*)-[E=基:%1]
#设置Apache删除的HTTP_授权头
RewriteCond%{HTTP:Authorization}。
重写规则^-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
#重定向到不带前端控制器的URI以防止重复内容
#(带或不带“/app.php”)。仅在初始路径上执行此重定向
#由Apache重写,而不是在后续周期中重写。否则我们会被解雇
#无休止的重定向循环(请求->重写到前端控制器->
#重定向->请求->…)。
#所以,如果你得到一个“太多重定向”错误或你总是得到重定向
#因为您的Apache没有公开重定向的状态,所以无法访问起始页
#环境变量,您有两种选择:
#-通过注释以下两行或两行禁用此功能
#-使用Apache>=2.3.9,用结束标志替换所有L标志,并删除
#第二步(最佳解决方案)
RewriteCond%{ENV:REDIRECT_STATUS}^$
重写规则^app\.php(?:/(.*)|$)%{ENV:BASE}/$1[R=301,L]
#如果请求的文件名存在,只需提供它即可。
#我们只想让Apache服务于文件,而不是目录。
RewriteCond%{REQUEST_FILENAME}-f
重写规则^-[L]
#将所有其他查询重写到前端控制器。
重写规则^%{ENV:BASE}/app.php[L]
#当mod_rewrite不可用时,我们指示临时重定向
#将起始页显式地发送到前端控制器,以便网站
#生成的链接仍然可以使用。
重定向匹配302^/$/app.php/
#不能改为使用RedirectTemp
这确实很奇怪,因为我没有
public/app.php
文件,但是,将其替换为index.php似乎也无法解决问题


我真的不知道还能尝试什么,非常感谢您的帮助。

如果其他人在尝试部署Symfony/Symfony 4应用程序时遇到此问题,我必须采取以下步骤来解决此问题:

  • 我的.htaccess文件已过时,仍然引用旧的Symfony 3 app.php。我要做的就是删除它,然后通过
    composer require symfony/apache-pack
  • 在这一点上,我仍然得到一个500错误和Heroku日志没有说任何有用的东西。这是因为Heroku要求您将内容记录到
    php://stderr
    将它们正确地登录到CLI和web界面。因此,我必须修改我的
    config/packages/prod/monolog.yaml
    文件,使
    嵌套的
    处理程序如下:
    路径:php://stderr“

在我的例子中,第二个问题是我忘记了在我的
postinstall
脚本中运行
node\u modules/.bin/encore production
,但只是将日志重定向到
php://stderr
应该为任何人指明正确的方向。

在我的情况下,我没有添加路线“/”。Symfony在本地环境中加载默认页面,但在heroku中,我们需要添加它

我显示我的代码供您参考:

class DefaultController extends AbstractController
{
    /**
     * @Route("/", name="default")
     */
    public function ping()
    {
        return $this->json([
            'message' => 'pong'
        ]);
    }
}

哇!第900位。这令人印象深刻。配置文件中有app.php这一事实表明您使用的是较旧的设置。为什么不使用Symfony 4配置?对不起,我使用Symfony的时间不长,几个月后我又回到这个项目,所以我的知识非常基础。我将尝试查看“迁移”页面,也许我可以找到一些更快的解决方案,而不是创建新项目和移动内容,非常感谢您的直觉。据我所知,当前的heroku php buildpack不支持使用默认配置的Symfony 4,您应该创建自己的.htaccess。奇怪的是如果我尝试创建一个新的Symfony 4