0

I have an application developed with Symfony 5, which is working fine in my local environment (Vagrant running Laravel Homestead box), but when I deploy it to the live server it crashes with an empty white page and responds with 500 Internal Server Error.

When I SSH into the server and manually change the APP_ENV variable's value to dev from prod inside my .env file, I can see the error:

Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle". Did you forget a "use" statement for another namespace?

enter image description here

I'm using Github Actions to deploy my application and in my action I'm installing the PHP dependencies like this

  - name: Install dependencies
    run: |
      export APP_ENV=prod
      composer install --no-dev --optimize-autoloader

So I don't want development only dependencies in my production server, hence the --no-dev flag, and if I understand correctly the WebProfilerBundle is used for inspecting the request in development, so it's indented to be used only in the dev environment.

Why does Symfony think it has to load this bundle? Also, I supposed to have my custom error pages configured for 404 and 500 errors (adding a bundles/TwigBundle/Exception/error.html.twig file under the templates folder), why isn't my custom error page shown? Why am I getting an empty white page?

I'm suspecting it's something with the way I'm deploying (since it work on my local machine).

I've tired removing the flags from the composer install (so just running composer install as is), but that also resulted in an error.

You can view my deployment action here. Also here's the link to the repository.

The app's supposed to run on this domain: https://thebedechkacase.com/

Cooty
  • 365
  • 4
  • 14
  • Take a look at this? https://stackoverflow.com/questions/58593872/attempted-to-load-class-webprofilerbundle-from-namespace-symfony-bundle-webpr – Wesley Smith Sep 23 '20 at 20:31
  • `why isn't my custom error page shown?` Just a guess, but most likely because this error occurs before your custom page logic is loaded. – Wesley Smith Sep 23 '20 at 20:35
  • Most likely your composer.json file has something in `post-install-cmd` thats trying to us that class – Wesley Smith Sep 23 '20 at 20:37
  • 3
    A few things come to mind: First it is very important for you to know that the error you receive has nothing to do with the issue you run into. You install the application with the --no-dev flag and thus the profiler is not installed. Once you switch to `dev` env it is required but not installed yet, thus the error is thrown. In your case my guess would be that you miss some rewrite settings on your server. Have a look at this: https://symfony.com/doc/current/setup/web_server_configuration.html Let me know if this fixes your issue – floGalen Sep 23 '20 at 21:17
  • 3
    @floGalen is correct about what is causing this specific error message. If you are still stuck consider staying in dev while just using a simple 'composer install'. That might point you to the actual deployment problem(s). Once the problems are resolved then go back into your original production mode. You might just start by checking your server logs. – Cerad Sep 23 '20 at 23:22

2 Answers2

3

Check your config/bundles.php and be sure to set Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true]. If i understood correctly your app is trying to load class which is not present under composer. Also be sure to clear cache on prod (php bin/console cache:clear). Also be sure that your production server have no .env.local file (or it's APP_ENV should also be prod)

Hordii
  • 159
  • 1
  • 7
0

Both the comment of @floGalen and @Gordy's answer were pointing me in the right direction, so thanks for that. But the exact anwser came from reading Symfony's guide on deployment.

If you get a “class not found” error during this step, you may need to run export APP_ENV=prod (or export SYMFONY_ENV=prod if you’re not using Symfony Flex) before running this command so that the post-install-cmd scripts run in the prod environment.

Since I wasn't using Symfony Flex I also needed the SYMFONY_ENV variable set to prod.

In addition I had my .env in the repo - which was actually only the one with development data and APP_ENV=dev, the "real" .env file was created during the deployment processes on the runner, retrieving it's value from Github's secret's store. Now the additional problem was that I first checked out my code to the runner, then run composer install then created the production .env (overwriting the one checked out from the repo), so composer install actually run with the dev settings. To fix this I moved the creation of my env file right before running composer install --no-dev --optimize-autoloader, then also did export SYMFONY_ENV=prod.

The combination of these two solved my issue!

Cooty
  • 365
  • 4
  • 14