[英]How to deploy Symfony 5 application on Heroku
我用這兩個網站來幫助我:
在我的 Heroku 儀表板上一切正常,它說:
構建成功。
但是當我打開應用程序時,我得到一個
403 禁止錯誤。 您無權訪問此資源。
然后我跑了
heroku logs --num 10
控制台中的完整結果:
2020-04-02T10:56:12.107189+00:00 heroku[router]: at=info method=GET
path="/" host=portefolio-symfony.herokuapp.com request_id=0c0a08dd-
5c11-4489-88e6-7d5246f38b09 fwd="176.189.137.142" dyno=web.1
connect=0ms service=1ms status=403 bytes=363 protocol=https
2020-04-02T10:56:12.107944+00:00 app[web.1]: [Thu Apr 02
10:56:12.107237 2020] [autoindex:error] [pid 161:tid 139621535053568]
[client 10.32.165.53:18173] AH01276: Cannot serve directory /app/: No
matching DirectoryIndex (index.php,index.html,index.htm) found, and
server-generated directory index forbidden by Options directive,
referer: https://dashboard.heroku.com/
2020-04-02T10:56:12.108260+00:00 app[web.1]: 10.32.165.53 - -
[02/Apr/2020:10:56:12 +0000] "GET / HTTP/1.1" 403 199
"https://dashboard.heroku.com/" "Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149
Safari/537.36
2020-04-02T11:30:01.272965+00:00 heroku[web.1]: Idling
2020-04-02T11:30:01.350792+00:00 heroku[web.1]: State changed from up
to down
2020-04-02T11:30:02.554326+00:00 app[web.1]: SIGTERM received,
attempting graceful shutdown...
2020-04-02T11:30:02.554551+00:00 app[web.1]: Stopping php-fpm...
2020-04-02T11:30:02.556082+00:00 app[web.1]: Stopping httpd
gracefully...
2020-04-02T11:30:02.556083+00:00 app[web.1]: Stopping httpd...
2020-04-02T11:30:02.635664+00:00 app[web.1]: Shutdown complete.
我看到它可能來自 public/ 目錄中的 .htaccess 文件
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
這里是 Procfile :
'web: $(composer config bin-dir)/heroku-php-apache2 public/'
好的,我找到了解決方案! 如果您需要,這里是幫助您的詳細信息。
在base.html.twig 中
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('build/app.css') }}">
{% endblock %}
然后我把腳本標簽放在最后
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
在.htaccess中
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
在app.js我添加了這一行:
require('../css/app.scss');
在webpack.config.js 中:
Encore
// directory where compiled assets will be stored
.setOutputPath("public/build/")
// public path used by the web server to access the output path
.setPublicPath("/build")
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')
.copyFiles({
from: './assets/images'
})
.addEntry("app", "./assets/js/app.js")
在package.json 中,我已將此行添加到:
"scripts": {
"postinstall": "encore production --progress"
},
Procfile只有這一行
web: $(composer config bin-dir)/heroku-php-apache2 public/
最后在 Heroku > Settings > Config Vars
Heroku > 設置 > Buildpack
這是我曾經再次幫助我的鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.