I have a Symfony web application. I would like to deploy it to a virtual private server.
I’ve developed the application in a Laravel Homestead Vagrant box, and I’m doing the deployment via Github actions.
On my local machine, the app runs fine in the Vagrant environment, but after syncing the files to the live server it gives me an error screen.
Warning: require(/var/www/thebedechkacase.com/src/config/bundles.php):
failed to open stream: No such file or directory
The error is coming from line 20 of Kernel.php, which looks like this
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
What I don’t understand is that according to the error message and the code above, the output of $this->getProjectDir()
resolves to /var/www/thebedechkacase.com/src/
(thebedechkacase.com
is the folder where my application code is stored), but bundles.php
is actually located in /var/www/thebedechkacase.com/config/
.
Why does Symfony think that the projectDir
is in src
and why doesn’t it look for config
in the root directory (aka thebedechkacase.com/
)? Why is this working in my dev environment, but not on the live server?
Additional info: I’m trying to set up the necessary environment variables with a shell script that I’m calling in my Github action, but when I check the environment variables manually on the live server’s shell, I see that non of them get set, so my script is probably broken in the first place (that’s why you’re able to see the default Symfony error page). Can that have something to do with this or this is another error?
URL where you can see the error:
https://thebedechkacase.com/
Github repo of the project:
https://github.com/Cooty/the-bedechka-case
The action that deploys the code:
https://github.com/Cooty/the-bedechka-case/blob/develop/.github/workflows/main.yml
Symfony version: 5.0
The live server is running Ubuntu 20.04, Ngingx with PHP-FPM 7.4 and PHP 7.4.
2
Answers
@Cerad lead me to the solution, I excluded composer.json from list of files in the rsync command, I thought that it's only needed for installing dependencies, but not that Symfony also reads from it at runtime.
Symfony sets the "project dir" based on "composer.json" file location.
"Project dir" is required to bootstrap the app (web and cli).
That’s why when you deploy you app without "composer.json", eg. to Elasticbeanstalk, you need to modify your kernel like this:
Notice the overridden getProjectDir() method.