I have a problem with Symfony assets on heroku (official php buildpack).
My code is based on Symfony 2.5 with new 3.0 folder structure, and it's using one local js and css file (the rest is loaded via cdn).
Those files are loaded exactly as described here: http://symfony.com/doc/current/cookbook/assetic/asset_management.html.
On dev environment everything is ok, but when i change env to prod then i need to do assetic:dump to force symfony dump my assets files into web/css|js folders. I made test on my local machine (switching env to prod) and everything is ok. The problem is with heroku, and its ephemeral filesystem:
When I try to run assetic:dump --env=prod by composer post-install-cmd, then deploy finish with successs, but every request ends up with:
AH01071: Got error 'PHP message: PHP Fatal error: Uncaught exception
'InvalidArgumentException' with message
'The file "/tmp/build_012d99e7-a14a-4626-afec-3ded3d4baeec/app/config/routing.yml"
When i deploy app without assetic:dump then of course my local css and js files are not available :/
Maybe there is some other script hook i can use to dump my assets?
Any ideas? Anyone succeded with assetic:dump on heroku and official php buildpack?
PHP and Symfony support has been revamped a lot in the last months, check out these links:
http://symfony.com/doc/current/cookbook/deployment/heroku.html
https://devcenter.heroku.com/articles/getting-started-with-php
https://devcenter.heroku.com/articles/php-support
https://devcenter.heroku.com/articles/getting-started-with-symfony2
What's most important:
Set an environment variable for SYMFONY_ENV
The value of SYMFONY_ENV is used to define the environment for CLI commands. So you don't have to fiddle about something like --env=prod in you custom commands. Composer hooks from composer.json (like post-install-cmd) are automatically executed with the defined environment. Please note: Set it to something different than dev. A dev environment may not work well on Heroku because Composer's dev dependencies (require-dev) are not installed. This is due to the --no-dev flag during the composer install on Heroku. For example, in the Symfony Standard Edition) the SensioGeneratorBundle never gets installed (if you do not move it to the require key in composer.json) and the dev environment won't work.
Error on /tmp path
The mentioned error should have been fixed with https://github.com/symfony/symfony/pull/12784. See also https://github.com/heroku/heroku-buildpack-php/issues/64.
Custom commands during deployment
Custom commands like assetic:dump can be placed in composer.json's script section under the compile key:
{
"scripts": {
"compile": [
"app/console assetic:dump"
]
}
}
See also https://devcenter.heroku.com/articles/php-support#custom-compile-step. Environment is taken from the above mentioned SYMFONY_ENV environment variable.
Related
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?
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/
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)
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!
I came from frontend stack so I can misanderstand something when I'm learning symfony3.
I downloaded Symfony according to documentation with:
php -r "readfile('https://symfony.com/installer');" > symfony
To make php command available i added some values to the enviroment PATH variable (according to this tutorial: https://seiler.it/installing-symfony-framework-into-xampp-for-windows/ - step 2.3) like this:
c:\xampp\mysql\bin;c:\xampp\php;c:\xampp\php\PEAR;C:\xampp\php\symfony
As you see I'm using xampp for PHP development. I added that last, because i copied that downloaded symfony file to c:\xampp\php\symfony directory.
OK, now if i want to use symfony command, like
symfony new project
I need to go to directory where there is that symfony file and add 'php' before, like
php symfony new project
Because simple 'symfony new project' doesn't work (error msg tells about that command isn't known by Windows). How can i fix it?
In frontend there is something like npm (node package manager) with which you can install library/tool locally in directory or globally (to run it in terminal from any place on the computer). Is there is any equivalent in PHP development? I tried to install this by pear (from mentioned tutorial in step 7), but it installed me symfony 1.x version (and error of running that command was that there is no task like 'new') and i want to learn 3, not 1.
I tried to get composer, but i don't see where can i install there symfony globally.
So, how to run symfony globally? What am I missing?
From this doc:
In Windows, move the symfony file to a directory that's included in the PATH environment variable to create the global command or move it to any other directory convenient for you
idea
Install and use Git Bash. Gives you more futures then the windows CLI.
Performance
To run a ten times more fast environment for Symfony i prefer a Linux OS.
I'm new to Lumen (and Laravel). I've created a project with Composer and now I'm trying to get PHPUnit to work.
I'm following a book, where it should be possible to run a default passing test by typing vendor/bin/phpunit in the terminal, but it gives the error:
'vendor' is not recognized as an internal or external command,
operable program or batch file
I've checked that the phpunit file is actually there and that phpunit is added as dependency in my composer.json file. I've also tried ./vendor/bin/phpunit and vendor/bin/phpunit/phpunit, but with the same result.
I've searched Google to find a solution, but everyone else seem to have issues when running phpunit (wihout vendor/bin) and the solution is to use the full path vendor/bin/phpunit, but since I'm already doing that, it does not fix my problem.
I'm using PHPStorm on a Windows machine and running the PHP server via PHPStorm. I've not modified the default Lumen project.
Any help is greatly appreciated!
UPDATE:
Trying php vendor/bin/phpunit gives the following error:
You need to set up the project dependencies using the following
commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
I'm not sure what that means, since I've already installed Composer. I used Composer to create the project and I haven't changed the dependencies from the default.
I had the same problem, for Windows it's vendor\bin\phpunit ;)
It turned out that some symlinks and permissions were not installed properly in the default project. I tried deleting the entire vendor/ directory and run composer install.
Now I can run phpunit with the command vendor\bin\phpunit (because I'm running on Windows - thanks Nizarii)
try this:
php vendor/bin/phpunit
Try putting php in front of the phpunit path like so:
php vendor/bin/phpunit
Symfony2 developers:
Recently, I am planning to get familiar with Symfony2, using it as api project. Current version is v2.7.1.
I already have apache installed as my local environment. So the installation root directory is:
//localhost/symfony
So I followed official guide to install it. Everything looks fine after installation. But when I view the page in development environment (app_dev.php).
//localhost/symfony/web/app_dev.php
Page content display as expected, but not styled. Then I open console and find some 404 response as follows:
Failed to load resource
//localhost/symfony/web/bundles/framework/css/structure.css
//localhost/symfony/web/bundles/framework/css/body.css
//localhost/symfony/web/bundles/framework/css/exception.css
//localhost/symfony/web/app_dev.php
//localhost/symfony/web/bundles/framework/css/structure.css
//localhost/symfony/web/bundles/framework/css/body.css
//localhost/symfony/web/bundles/framework/css/exception.css
I looked into project/web/bundles directory, found nothing inside but two empty file.
framework and sensiodistribution.
Fresh installation without any config change. I got this problem. Could you guys give some explanation why it occurs. Otherwise I didn't get the full package?
Do I miss those files inside project/web/bundles?
If the case in which i missed those file. So could you send me an archive file on current version 2.7.1?
You must run the command app/console assets:install to generate symlinks (or files) in /web/bundles
If these files exists then you probably have problem with URL Rewriting check your .htaccess or configure your host. You can also use command php app/console server:start and check there if it works
You can run composer install to install all necessary bundles and libraries and probably composer.json has defined postinstall commands.
If you use assetic then you can try to run php app/console assetic:dump more on http://symfony.com/doc/current/cookbook/assetic/asset_management.html
In Symfony 2.6 you have new asset manager read more on http://symfony.com/blog/new-in-symfony-2-6-smarter-assets-install-command
php app/console assets:install or php app/console assets:install --symlink
for Symfony 3.x the directory changed from app to bin so commands are:
php bin/console assets:install or php bin/console assets:install --symlink
Since I landed here from a Symfony 3 search this might be useful for Symfony 3 users having the same problem
Run php bin/console assets:install at the project root
I've been searching everywhere for an answer to this.
I built an app in Laravel - I chose Heroku for deployment, I included a custom Procfile and httpd.conf to rewrite the document root to public. That all works great.
However my app shows an internal 500 error upon loading because it hasn't loaded any dependencies i.e. not run composer upon deployment.
My question is: how do I make composer run upon deployment?
p.s. I'm a total noob with this kind of stuff so please provide as much detail as possible.
The only possible solution I've seen is here: http://bergie.iki.fi/blog/using_composer_to_manage_dependencies_in_heroku_php_apps/
But I'm convinced there is better way than this.
Try the following
Composer Fix
You need to generate and include composer.lock in the repo, but Laravel's default .gitignore ignores composer.lock
Remove composer.lock from .gitignore
$ composer install
Add your Procfile
Heroku knows which processes to run for your app based on a configuration file called a Procfile. The default apache2 process (if you don't use a Procfile) points to the web root, not to /public, so we need to create a custom Procfile to serve the site from /public.
Add a file with the name Procfile that contains this line:
web: vendor/bin/heroku-php-apache2 public
Deploy your code to the Heroku app
$ git push heroku master
Another possible cause is that Heroku doesn't recognize your app as a PHP app, for example because of the presence of package.json in the root. And so it doesn't do composer install for you.
Solution is to specify a buildpack, and do a push again.
heroku config:add BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php
See also:
Heroku doesn't recognize my Laravel app as PHP app, and does not do composer install