How to properly check if a Symfony application is installed? - symfony

In our Dockerfile's entrypoint.sh I need to check (actually the database) to see if there's an application currently installed and if not run a php app/console command.
This is my current approach:
CHECK=$(php app/console doctrine:query:sql "SELECT * FROM user" -q)
if [ $? -ne 0 ]; then
php app/console vendor:install
fi
Which doesn't feel right. What other ways is out there to properly check this?
NOTE That the database is created by a 3rd-party MySql broker service. So I can't check for database existence, 'cause always is true.

Based on Composer hooks, you can check for these files and directories.
Required:
app/boostrap.php.cache or var/boostrap.php.cache.
app/config/parameters.yml.
vendor/symfony.
Check if app/cache or var/cache has been warmed up.
Optional:
Check if executing app/SymfonyRequirements.php or var/SymfonyRequirements.php would return a success - this may fail however for some strict requirements.
web/bundles if there are any assets to install.

Related

Symfony2 : clear cache don't generate required file EntityManager_XXXX.php (for doctrine)

After a cache:clear I have this in php log :
PHP Warning: require_once(/var/www/mysite/releases/20150706130613/app/cache/prod/jms_diextra/doctrine/EntityManager_56274d8036816.php): failed to open stream: No such file or directory in /var/www/mysite/releases/20150706130613/app/cache/prod/appProdProjectContainer.php on line 279
I check the file list in app/cache/prod/jms_diextra/doctrine/, I find EntityManager_XXXX.php where XXXX.php is allways different from 56274d8036816 !! So i duplicate the existing file with the required file name and then it works !! The strange thing is it' allways the same file name (56274d8036816) symfony need and never find/generate !!
I don't want to manually create the needed file each time I clear the cache :( what's going wrong ??
Maybe you should add permissions to cache directory
chmod 777 -R cache
Had the same issue.
The other comments are correct, here are the steps:
Delete all files/directories in your cache directory.
chmod -R 777 cache
php app/console cache:warmup
This should succesfully generate the cache.

Clear Symfony2 Http Cache

Is there an easy way for fast removing http cache in symfony2? We've over 30.000 files in cache directory and removing them tooks very long. Is there an better way for doing this? Btw...linking the cache to /dev/null when removing...
The easiest way to clear cash would be to use console command:
app/console cache:clear
If this is production – you need to add environment (using paramentr --env=prod. )
As default all console commands run in the dev environment.
So, for example, this command looks like app/console cache:clear -e=prod.
Would always recommend using the Symfony console:
php app/console cache:clear --env=prod
This should be fastest, as it moves/renames your current cache folder and creates a new one before deleting your old cache, so there should zero downtime.
If you look in Symfony's cache directory /var/cache/ you'll find an http_cache directory.
So you could use PHP's exec() to remove the directory.
$root = $this->get('kernel')->getRootDir();
$path = $root . '/../var/cache/prod/http_cache';
exec('rm -rf ' . $path);

Symfony2 "Assetic:dump -env-prod" Permission denied Exception

Before i executed an update (composer.phare update) with the root user, every thing works fine, but now when i tries to run "Assetic:dump -env-prod" i get a "Permission denied" error
[Assetic\Exception\FilterException]
An error occurred while running:
'' '-jar' '/home/symfony/www/app/Resources/java/yuicompressor.jar' '--ch
arset' 'UTF-8' '-o' '/tmp/YUI-OUT-vbRlyu' '--type' 'css' '/tmp/YUI-IN-OoRVH
Q'
Error Output:
sh: 1: : Permission denied
Input:
meta.foundation-version{ ...
I tried all the solutions in this post Fontawesome fonts fail after assets:install and assetic:dump
clear the cache, chown, chgrp and chmod nothing worked always the same problem
One way to deal with file permissions when you are running a web based application which requires either auto deployment or constant manual updates like using bin/console from symfony2, its to make sure that the files belongs to the user under which your application runs.
As you did not provide environment settings, I will be making a few assumptions and provide you with a generic setup scenario, hopefully this will help guide you to the the best solution for your specific case.
Environment Assumptions:
OS: linux flavor;
Web server: nginx will be running as www-data;
PHP: php-fpm will running as testapp and using a socket connection for this application;
Generic set-up steps:
In the /etc/nginx/nginx.conf file, make sure that the user/group are set to www-data;
In the /etc/php5/fpm/pool.d/apptest.conf file, make sure that the user & group are set to testapp;
TIP: The file above might need to be created, if that's the case you should just copy the content of the www.conf file located in the same folder.
In the /etc/php5/fpm/pool.d/apptest.conf file, make sure listen.owner & listen.group are set to www-data;
Make sure that you have a line like the one below in this file /etc/php5/fpm/pool.d/apptest.conf:
listen = /var/run/php5-fpm.apptest.sock.
NOTE: the fpm.apptest.sock portion of that line above, its the name of a file that does not exist yet but will be created when you restart php. The benefit is that you will have an isolated php process for this application;
a) In the case on nginx and if you are using socket connections, make sure to add this line in your apptest conf file:
unix:/var/run/php5-fpm.apptest.sock;
b) If you are using apache add this line in that conf file:
-socket /var/run/php5-fpm.apptest.sock;
If you are on a linux box, create user with no password and it should be called, apptest.
Note: apptest is the name of your application, it will also be the user under which php will be running and it should also be the application files/folders owner.
Restart php and nginx/apache.
Tip: to change to a user in linux which has no password, you should have root privileges and run:
sudo -u apptest -i.
After this, you should perform all your commands as the apptest user previously created, including running the symfony2 bin/console.
These are very generic steps, so if you need any clarification, let me know.
I do not recommend to use root for updating. In my opinion the way to go is to have /app/logs /app/cache writable for the server and the src and vendor folder only readable for the server.
So lets say your user and group is: coolman, than try this:
# everything is yours
chown coolman:coolman -R .
# all and group can access folders and read files, you as user can additionally write them
chmod ag=rX,u=rwX -R .
# full access to logs and cache for everyone (also the server)
chmod a+rwX -R app/logs app/cache
You make your composer update with your coolman user.
There is only one small problem, too. The logs might be www-data:www-data rw-r--r-- so you cannot delete them. So just add a line to your app.php and your app/console file:
\umask(0000);
I think this line is commented out as default. This says, that if no explicit rights are set within PHP, than every file which is created will get 0777 - mask = 0777 so you can delete logs and cache then.

Checking that the bundle is autoloaded: FAILED

I cannot create a bundle. I'm getting "Checking that the bundle is autoloaded: FAILED" which never used to happen before until now. Steps below always worked until today.
Any solutions?
To setup Symfony2:
I downloaded: Symfony_Standard_Vendors_2.4.5.zip
I unzipped under: /var/www/html/local/three. three is the main folder.
I set the permissions:
desktop#ubuntu:/var/www/html/local/three$ sudo chown desktop -R app/logs
desktop#ubuntu:/var/www/html/local/three$ sudo chown desktop -R app/cache
desktop#ubuntu:/var/www/html/local/three$ sudo chmod 777 -R app/cache
desktop#ubuntu:/var/www/html/local/three$ sudo chmod 777 -R app/logs
This URL works fine: http://localhost/local/three/web/app_dev.php
To create my bundle:
desktop#ubuntu:/var/www/html/local/three$ php app/console generate:bundle --namespace=Myblog/PublicBundle
Welcome to the Symfony2 bundle generator
In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest MyblogPublicBundle.
Bundle name [MyblogPublicBundle]:
The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.
Target directory [/var/www/html/local/three/app/cache/dev/../src]:
Determine the format to use for the generated configuration.
Configuration format (yml, xml, php, or annotation): yml
To help you get started faster, the command can generate some
code snippets for you.
Do you want to generate the whole directory structure [no]? yes
Summary before generation
You are going to generate a "Myblog\PublicBundle\MyblogPublicBundle" bundle
in "/var/www/html/local/three/app/cache/dev/../src/" using the "yml" format.
Do you confirm generation [yes]? yes
Bundle generation
Generating the bundle code: OK
Checking that the bundle is autoloaded: FAILED
Confirm automatic update of your Kernel [yes]? yes
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]? yes
Importing the bundle routing resource: OK
The command was not able to configure everything automatically.
You must do the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
Solved:
Problem is to do with line:
Target directory [/var/www/html/local/three/app/cache/dev/../src]:
I don't understand why it is suggesting cache path. Old days it was always automatically suggesting src directory instead. I've checked my logs that's why I know. Anyway change it to:
/var/www/html/local/three/src
Done!
This is a known issue now, I guess it will be fixed on the next version
https://github.com/symfony/symfony/issues/10972
https://github.com/symfony/symfony/pull/10999
https://github.com/symfony/symfony-standard/issues/659
for now when the generate:bundle displays this:
The bundle can be generated anywhere. The suggested default directory
uses the standard conventions.
Target directory:
-- /type/your/directory/src here

symfony2 blank page on web hosting server

I try to deploy my project on my distant web server. I think the sf2 installation is ok I have the app_dev.php and the config.php pages, the check.php doesn't return me any error and I set up the acl on the directories app/cache and app/logs.
but I cant have any of my pages from my controllers. If I try something like .../Symfony/web/app_dev.php/myurl I have a blank page.
What can I do? thanks in advance
Most likely there's a php error, you should check your web server/php error log files. The exact location of those depends on configuration, ask your hosting provider if you can't find them yourself.
As a wild guess, check the permission on the app/cache folder - this is a common problem I believe. You should always run app/console cache:clear --no-debug --env=prod with the webserver user.
You may also check whether mod_rewrite is enabled in your server.
I am not sure if you have checked the obvious but app_dev contains the following:
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !in_array(#$_SERVER['REMOTE_ADDR'], array(
'127.0.0.1',
'::1',
))
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
Make sure to add your IP, remove the IP block altogether or come up with a login solution.
I resolved the blank pages simply executing this command on the project root:
app/console assets:install web
i opened my Linux terminal and execute :
php app/check.php
it showed me ERROR:
date.timezone setting must be set
Set the "date.timezone" setting in php.ini*
i set server timezone and restart my server, my blank page gone, and symfony 2.7.3 version start working

Resources