Heroku PHP应用程序可在本地运行,如果我推送供应商目录,则在部署时可以运行,但如果我不推送供应商目录,则无法部署

时间:2019-04-26 18:30:06

标签: php heroku

这让我很困惑。我已经构建了一个简单的heroku应用程序。它在本地工作正常。如果我推送供应商目录,它实际上在Heroku上可以正常工作,但是heroku警告我不要推送作曲家供应商目录。该消息将我定向到一个链接,该链接指示我将composer.lock文件用于依赖项。因此,我添加了一个.gitignore来忽略供应商目录并再次推送,但是当我这样做时,该应用程序将无法运行。我检查了heroku遥控器上的文件结构,发现依赖项已正确安装,但该应用程序无法正常工作。我正在尝试以正确的方式执行此操作,因此,即使我知道可以将供应商文件夹推入即可,但我确实希望它能够正常工作。有人遇到过吗?

这是我composer.json文件中的代码

{
    "require": {
        "salesforce-mc/fuel-sdk-php": "1.2.2",
        "guzzlehttp/guzzle": "~6.0",
        "ext-soap": "*",
        "php": "7.1.28",
        "ext-mcrypt": "*"
    }
}

和我的Procfile

web: vendor/bin/heroku-php-apache2

我尝试加载页面时遇到的错误非常普遍

This page isn’t working project.herokuapp.com is currently unable to handle this request.
HTTP ERROR 500

以下是heroku日志

    $ heroku logs
    2019-04-26T18:14:40.303998+00:00 app[api]: Initial release by user stan_alach@hotmail.com
    2019-04-26T18:14:40.888724+00:00 app[api]: Enable Logplex by user stan_alach@hotmail.com
    2019-04-26T18:14:40.888724+00:00 app[api]: Release v2 created by user stan_alach@hotmail.com
    2019-04-26T18:14:40.303998+00:00 app[api]: Release v1 created by user stan_alach@hotmail.com
    2019-04-26T18:15:12.000000+00:00 app[api]: Build started by user stan_alach@hotmail.com
    2019-04-26T18:15:21.000000+00:00 app[api]: Build succeeded
    2019-04-26T18:15:22.259480+00:00 heroku[web.1]: Starting process with command `vendor/bin/heroku-php-apache2`
    2019-04-26T18:15:20.868799+00:00 app[api]: Deploy 1f57bbb2 by user stan_alach@hotmail.com
    2019-04-26T18:15:20.868799+00:00 app[api]: Release v3 created by user stan_alach@hotmail.com
    2019-04-26T18:15:20.887739+00:00 app[api]: Scaled to web@1:Free by user stan_alach@hotmail.com
    2019-04-26T18:15:24.264059+00:00 app[web.1]: Optimizing defaults for 1X dyno...
    2019-04-26T18:15:24.284000+00:00 app[web.1]: 4 processes at 128MB memory limit.
    2019-04-26T18:15:24.288460+00:00 app[web.1]: Starting php-fpm...
    2019-04-26T18:15:26.290267+00:00 app[web.1]: Starting httpd...
    2019-04-26T18:15:26.665144+00:00 heroku[web.1]: State changed from starting to up
    2019-04-26T18:15:34.496861+00:00 heroku[router]: at=info method=GET path="/" host=cloud-buddy.herokuapp.com request_id=7fa87fe1-fb53-4ef6-ae75-f14488321528 fwd="174.65.133.198" dyno=web.1 connect=0ms service=645ms status=500 bytes=169 protocol=http
    2019-04-26T18:15:34.495116+00:00 app[web.1]: [26-Apr-2019 18:15:34 UTC] PHP Fatal error:  Uncaught Error: Class 'FuelSdk\SQZL_ET_Client' not found in /app/index.php:18
    2019-04-26T18:15:34.495133+00:00 app[web.1]: Stack trace:
    2019-04-26T18:15:34.495135+00:00 app[web.1]: #0 {main}
    2019-04-26T18:15:34.495201+00:00 app[web.1]: thrown in /app/index.php on line 18
    2019-04-26T18:15:34.496655+00:00 app[web.1]: 10.51.197.106 - - [26/Apr/2019:18:15:33 +0000] "GET / HTTP/1.1" 500 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36
    2019-04-26T18:16:07.874786+00:00 app[api]: Starting process with command `bash` by user stan_alach@hotmail.com
    2019-04-26T18:16:10.152678+00:00 heroku[run.9911]: State changed from starting to up
    2019-04-26T18:16:10.244363+00:00 heroku[run.9911]: Awaiting client
    2019-04-26T18:16:10.279025+00:00 heroku[run.9911]: Starting process with command `bash`
    2019-04-26T18:20:32.049330+00:00 heroku[run.9911]: State changed from up to complete
    2019-04-26T18:20:32.028902+00:00 heroku[run.9911]: Process exited with status 0

这是包含供应商文件夹时得到的输出,当我这样做时,该应用程序便可以正常工作。我想现在我会顺其自然,因为它正在工作,但是很高兴知道如何以正确的方式进行操作。自动加载一定存在某种文件结构问题,当heroku安装依赖项时,该问题无法自行解决。

$ git push heroku master
Enumerating objects: 612, done.
Counting objects: 100% (612/612), done.
Delta compression using up to 8 threads
Compressing objects: 100% (595/595), done.
Writing objects: 100% (612/612), 1.08 MiB | 307.00 KiB/s, done.
Total 612 (delta 247), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote:        - php (7.1.28)
remote:        - ext-mcrypt (bundled with php)
remote:        - ext-soap (bundled with php)
remote:        - apache (2.4.39)
remote:        - nginx (1.14.2)
remote:
remote:  !     WARNING: Your app's PHP version is no longer actively maintained
remote:  !
remote:  !     Only security updates will be provided for your PHP version
remote:  !     series by the PHP Group until its end-of-life on 2019-12-01.
remote:  !
remote:  !     It is strongly recommended you update your app to a version of
remote:  !     PHP with "active support" status immediately to ensure you get
remote:  !     the latest bugfixes and security updates each time you deploy.
remote:  !
remote:  !     You may check the list of versions supported by the PHP Group
remote:  !     and their EOL dates here: http://php.net/supported-versions.php
remote:  !
remote:  !     For a list of supported runtimes & extensions on Heroku, please
remote:  !     refer to: https://devcenter.heroku.com/articles/php-support
remote:
remote: -----> Installing dependencies...
remote:        Composer version 1.8.4 2019-02-11 10:52:10
remote:
remote:  !     WARNING: Composer vendor dir found in project!
remote:  !
remote:  !     Your Git repository contains Composer's 'vendor' directory.
remote:  !
remote:  !     This directory should not be under version control; only your
remote:  !     'composer.json' and 'composer.lock' files need to be added, as
remote:  !     Composer will handle installation of dependencies on deploy.
remote:  !
remote:  !     To suppress this notice, first remove the folder from the index
remote:  !     by running 'git rm -r --cached vendor/'.
remote:  !     Next, edit your project's '.gitignore' file and add the folder
remote:  !     '/vendor/' to the list, then commit the changes.
remote:  !
remote:  !     For more info, refer to the Composer FAQ: 
remote:
remote:        Loading composer repositories with package information
remote:        Installing dependencies from lock file
remote:        Nothing to install or update
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 16.2M
remote: -----> Launching...
remote:        Released v3
remote:        https://cloud-buddy.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/cloud-buddy.git
 * [new branch]      master -> master

1 个答案:

答案 0 :(得分:0)

结果证明,这与文件结构有关。我的index.php是我应用程序的根目录。当我在Heroku上实际查看文件结构时,我意识到Heroku期望有所不同。因此,由于目录结构的原因,自动加载基本上无法正常工作。我进行了重组,将html文件放在/ public目录中,并将供应商文件夹保留在根目录下。现在它可以完美地工作了,我不需要上载供应商目录。我的文件结构现在看起来像这样:

.git
public
--index.php
src
vendor
--autoload.php
composer.json
composer.lock
Procfile

我的Procfile文件只有这一行:

web: vendor/bin/heroku-php-apache2 public/

最后,只需确保在/ public目录下的php文件中上一层即可获取根目录和子文件夹中的php文件。这样的行看起来像:

require '../vendor/autoload.php';