简体   繁体   English

Symfony2 dev环境工作,prod环境给出404错误

[英]Symfony2 dev environment works, prod environment gives 404 error

I have recently successfully installed Symfony2 on my machine. 我最近在我的机器上成功安装了Symfony2。

I can access http:/localhost/app_dev.php (dev environment) 我可以访问http:/localhost/app_dev.php (开发环境)

However, when I try to access the prod environment: 但是,当我尝试访问prod环境时:

http:/localhost/app.php

I get the following error message in the browser: 我在浏览器中收到以下错误消息:

Oops! 哎呀! An Error Occurred 发生错误

The server returned a "404 Not Found". 服务器返回“404 Not Found”。 Something is broken. 有些东西坏了。 Please e-mail us at [email] and let us know what you were doing when this error occurred. 请发送电子邮件至[email],告诉我们发生此错误时您在做什么。 We will fix it as soon as possible. 我们会尽快修复它。 Sorry for any inconvenience caused. 给您造成的任何不便,请原谅。

I have checked the obvious: The file app.php does exist in the same folder as app_dev.php - so I don't know whats causing this. 我已经检查了显而易见的:文件app.php确实与app_dev.php存在于同一个文件夹中 - 所以我不知道是什么造成这种情况。

Does anyone have a solution to fix this? 有没有人有解决方案来解决这个问题?

[[Edit]] [[编辑]]

I have cleared the cache by typing: sudo php app/console cache:clear env=prod no-debug as recommended. 我通过输入以下内容清除了缓存: sudo php app/console cache:clear env=prod no-debug按推荐。 I now get a blank screen. 我现在得到一个空白的屏幕。 Worryingly, there are no error messages logged in app/logs/prod.log, so I have not got the faintest idea as to what is wrong (prod environment still works just fine). 令人担忧的是,app / logs / prod.log中没有记录错误消息,因此我对于出了什么问题没有最微妙的想法(prod环境仍然可以正常工作)。

Contents of my app/config/routing.yml file: 我的app / config / routing.yml文件的内容:

### fos routing, remove later
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

###


# Internal routing configuration to handle ESI
#_internal:
#   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
#   prefix:   /_internal

Here is my app/config/routing_dev.yml file 这是我的app / config / routing_dev.yml文件

_welcome:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Welcome:index }

_demo_secured:
    resource: "@AcmeDemoBundle/Controller/SecuredController.php"
    type:     annotation

_demo:
    resource: "@AcmeDemoBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo

_assetic:
    resource: .
    type:     assetic

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

I just noticed that I DO NOT have a routing_prod.yml** 我刚注意到我没有 routing_prod.yml **

(alarm bells ringing) - does Symfony2 not ship with a production routing config file? (警钟响了) - Symfony2没有附带生产路由配置文件吗?

My Apache configuration file content is shown below: 我的Apache配置文件内容如下所示:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /path/to/symfony/web
    ServerName localhost

    # Custom log file
    Loglevel warn
    ErrorLog  /path/localhost.error.log
    CustomLog /path/localhost.access.log combined

    <Directory /path/to/symfony/web>
        AllowOverride None
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^(.*)$ app.php [QSA,L]
    </Directory>
</VirtualHost>

[[Further Details]] [[更多详情]]

Contents of app/logs/prod.log app / logs / prod.log的内容

[2012-08-10 18:10:38] security.INFO: Populated SecurityContext with an anonymous Token [] [] [2012-08-10 18:10:38] request.ERROR: Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException: No route found for "GET /" (uncaught exception) at /path/to/symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php line 83 [] [] [2012-08-10 18:10:38] security.INFO:填充SecurityContext,带有匿名令牌[] [] [2012-08-10 18:10:38] request.ERROR:Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException:在/path/to/symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php第83行[] []找不到“GET /”(未捕获的异常)的路由

Have you turned on the production environment and cleared the cache? 您是否打开了生产环境并清除了缓存? Run the console and do this: 运行控制台并执行以下操作:

app/console --env=prod cache:clear

I think I had a similar problem. 我想我有类似的问题。 I believe warming up the cache solved my instance if it. 我相信升温缓存解决了我的实例。

php app/console cache:warmup --env=prod --no-debug

Having recently installed Symfony 2.2 I ran into this issue too but it's actually the normal behavior. 最近安装了Symfony 2.2我也碰到了这个问题,但这实际上是正常行为。 Meaning out of the box Symfony 2.x (as of this writing) doesn't ship with any routes/content for the production environment. 开箱即用的意义Symfony 2.x(截至本文撰写时)并未附带生产环境的任何路径/内容。

In your case it looks you installed the Friends of Symfony bundle, which did setup some routes in your production route ( routing.yml ), but at a glance none of the routes seem to be targeting the root of your production environment ie http:/localhost/app.php/ so the 404 is probably expected. 在你的情况下,它看起来你安装了Symfony之友捆绑,它确实在生产路线( routing.yml )中设置了一些路线,但是一眼就看不到任何路线似乎都是针对你的生产环境的根,即http:/localhost/app.php/所以404可能是预期的。 I can't be totally sure though since it's importing routes so the details are obscured. 我不能完全确定,因为它导入路线所以细节被模糊了。 A great way to check your routes is to read up on Visualizing & Debugging Routes where you can learn about the app/console router:debug CLI command. 检查路由的一个好方法是阅读可视化和调试路由 ,您可以在其中了解app/console router:debug CLI命令。

routing.yml is the default place for routes in your production environment (meaning routing_prod.yml isn't a thing). routing.yml是生产环境中路由的默认位置(意味着routing_prod.yml不是一个东西)。 You'll notice routing_dev.yml imports routing.yml . 您会注意到routing_dev.yml导入routing.yml That means that everything that you put in production is (out of the box) accesible in the development. 这意味着您在生产中投入的所有内容(开箱即用)都可以在开发中访问。 The demo content you see is exclusive to the dev environment so that's why you're not seeing it in the production. 您看到的演示内容是开发环境独有的,这就是您在制作中没有看到它的原因。 Feel free to move some stuff around to suite your needs but it's generally a good idea for the dev to import the production but not vice versa. 随意移动一些东西以满足您的需求,但通常一个好主意是开发人员导入生产,但反之亦然。

Any time you want to test out the changes in your production you'll want to clear your cache as @tolgap recommends. 每当您想要测试生产中的更改时,您都希望清除缓存,如@tolgap建议的那样。 The production environment leans heavily on the precompiled cache, so that command will force your production cache to refresh. 生产环境严重依赖预编译缓存,因此该命令将强制生产缓存刷新。 The dev environment is always refreshing it's cache. 开发环境总是刷新它的缓存。 Understanding this is crucial to working with Symfony. 了解这一点对于与Symfony合作至关重要。 Though the Book on Symfony.org is a great entry point it doesn't exactly drive home this point about the subtitles of caching and workflow. 虽然Book on Symfony.org是一个很好的切入点,但它并没有完全说明缓存和工作流的字幕。 I was definitely confused for a minute before I realized the relationship between routing.yml and routing_dev.yml and the soft caching of the dev versus the hard caching of the production. 在我意识到routing.ymlrouting_dev.yml之间的关系以及dev的软缓存与生产的硬缓存之前,我肯定感到困惑。

I'm surprised that nobody told you: you have no route for / on the prod environment. 我很惊讶没有人告诉你:你在生产环境中没有路线。

write this in app/config/routing.yml 在app / config / routing.yml中写这个

  _welcome:
 pattern:  /
 defaults: { _controller: AcmeDemoBundle:Welcome:index }

You need to install APC for your local server, and everything will work in prod mod for you. 您需要为本地服务器安装APC,一切都将在prod mod中工作。 Also try to hit correct database in parameters.yml, because dev is adding DATABASENAME_dev. 还尝试在parameters.yml中找到正确的数据库,因为dev正在添加DATABASENAME_dev。

I tried the solution @gilden gave in his comment; 我尝试了@gilden在评论中给出的解决方案; it works. 有用。

I ran php bin/console cache:clear --env=prod instead of rm -rf app/cache/* . 我运行了php bin/console cache:clear --env=prod而不是rm -rf app/cache/*

Today I had the same problem and tried various solutions. 今天我遇到了同样的问题并尝试了各种解决方案。 Until I remembered that it is important to have the mod_rewrite apache service enabled. 直到我记得启用mod_rewrite apache服务很重要。 To do this run the following command (on Ubuntu) 为此,请运行以下命令(在Ubuntu上)

sudo a2enmod rewrite

And then restart the server 然后重启服务器

sudo service apache2 restart

I hope that someone can be helpful. 我希望有人可以提供帮助。 Regards! 问候!

My symfony cache folder was not writable. 我的symfony缓存文件夹不可写。 It worked when i made the cache complete directory and its files as writable. 当我将缓存完整目录及其文件设置为可写时,它工作。

sudo chmod -R 777 .

(Agrega esto dentro del archivo routing_dev.yml) Add this in routing_dev.yml (Agrega esto dentro del archivo routing_dev.yml)在routing_dev.yml中添加它

_wellcome:
    pattern: /
    defaults: { _controller:AppBundle:Default:index }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM