Error with Symfony's cache and permissions - php

I got this continuous error with cache. Something with permissions but I can't figure what's going on. In my local environment when I go a php bin/console cache:clear -e=dev it return me this
[Symfony\Component\Filesystem\Exception\IOException]
Failed to remove file "/project/var/cache/de~/pools/ORsqbHaOKl/L/K/iZULk48B-k00dzIKC2qg": unlink(/project/var/cache/de~/pools/ORsqbHaOKl/L/K/iZULk48B-k00dzIKC2qg): Permission denied.
So I need to make first a chmod -R 777 var/, then again the clear cache and it works. But when I run the website it return me
Failed to create "/project/var/cache/dev/tcpdf": mkdir(): Permission denied.
So I need again to make chmod -R 777 var/
In the production server without erasing anything sometimes i get this error
Warning: rename(C:\project\var\cache\prod/doctrine/orm/Proxies\__CG__AppBundleEntitySomeEntity.php.5a142ad84e8464.47105642,C:\project\var\cache\prod/doctrine/orm/Proxies\__CG__AppBundleEntitySomeEntity.php): Access is denied. (code: 5)
error in vendor\doctrine\common\lib\Doctrine\Common\Proxy\ProxyGenerator.php
(line 309 rename())
$tmpFileName = $fileName . '.' . uniqid('', true);
file_put_contents($tmpFileName, $proxyCode);
#chmod($tmpFileName, 0664);
rename($tmpFileName, $fileName);
}
Local environment: debian 9
Production environment: windows server 2008

You need to set the var folder permissions as suggested by this symfony article.
Run the following commands in your project directory and you will not come across any permission related issues on cache and logs
HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1)
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var

I've just played around with it a bit and noticed that every time you call
bin/console cache:clear --env=prod
The user of the folder /prod will be changed to the user that is executing the call. The scripts deletes the folder and creates a new own, but that will be done be the current user set.
You can stop the script from creating a new folder by using the --no-warmup option. With that, I've fixed my problem.
bin/console cache:clear --env=prod --no-warmup
For that to work you need to fix the current situation once, at best by deleting all folders and files within the /var/cache directory.

Related

Symfony prod.log exception /var/cache/prod/annotations is not writable

I have web based on symfony 3. For deploy i have own bash script. In this script are routines like git pull, clear cache...
On server is: Debian GNU/Linux 9 (stretch)
But after deploy i can see exception in prod.log:
[2018-05-11 16:55:36] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "The directory "/var/www/domain.com/var/cache/prod/annotations" is not writable." at /var/www/domain.com/vendor/doctrine/cache/lib/Doctrine/Common/Cache/FileCache.php line 92 {"exception":"[object] (InvalidArgumentException(code: 0): The directory \"/var/www/domain.com/var/cache/prod/annotations\" is not writable. at /var/www/domain.com/vendor/doctrine/cache/lib/Doctrine/Common/Cache/FileCache.php:92)"} []
But problem is - before and after clear cache i have command chmod -R 777 /var and after deploy if i look to /var/cache/prod/annotations i see new files with time of change equals time when i run clear cache (via sh script).
Anybody some ideas? Where look what chceck?
This error occurs if your shell writes into app/cache first and then you open your website where other parts should be written to that folder too.
Simple solution: add your shell user to the group that your webserver uses, probably www-data, through sudo usermod -a -G www-data userName
The problem is the permissions, to fix use this:
setfacl -R -m u:www-data:rX folderProject
setfacl -R -m u:www-data:rwX folderProject/var/cache folderProject/var/logs folderProject/var/sessions folderProject/app/sessions
setfacl -dR -m u:www-data:rwX folderProject/var/cache folderProject/var/logs folderProject/var/sessions folderProject/app/sessions
Replace folderProject with your project name.
If you are using Ubuntu with apache this command fix the problem, if you are using another GNU/Linux change the user www-data to your user

Laravel 5.2 could not open laravel.log

I know there are a lot of questions on this topic but my issues is really weird that's why I decided to post.
I have this error in /var/logs/apache/error.log
[Tue Mar 01 07:26:51.435312 2016] [:error] [pid 8837] [client 127.0.0.1:37843] PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/personale/librarie-cor/storage/logs/laravel.log" could not be opened:
failed to open stream: Permission denied' in /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87\nStack trace:
\n#0 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\\Handler\\StreamHandler->write(Array)
\n#1 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Logger.php(289): Monolog\\Handler\\AbstractProcessingHandler->handle(Array)
\n#2 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Logger.php(565): Monolog\\Logger->addRecord(400, Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
\n#3 /var/www/personale/librarie-cor/vendor/laravel/framework/src/Illuminate/Log/Writer.php(202): Monolog\\Logger->error(Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
\n#4 /var/www/personale/librarie-cor in /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 87
The thing is I already did chmod -R 777 storage/, here is a screenshot:
How can I get rid of the error?
TLDR;
Run the following commands on your terminal
# Clear Laravel cache and the compiled classes
php artisan cache:clear
php artisan clear-compiled
# Change the storage and cache directories permission
sudo chmod -R 777 storage
sudo chmod -R 777 bootstrap/cache
# Regenerate the composer autoload file
composer dump-autoload
More thorough explanation
This usually happens because of the web server needs a write access to the storage and bootstrap/cache directories.
1. Check which user is being used by web server process
First, make sure that your webserver process is run by an account with limited privileges. Nginx and Apache usually will automatically create and use the less privileged www-data user and group. You can always use the ps command to check which user is being used by the running service:
ps aux | grep nginx
2. Set the owner of your project directory
Next, make sure that your Laravel project directory is owned by the same user and group who run the web server process. Suppose your web server is run by www-data and your project directory is located at /var/www/laravel, you can set the ownership like so:
sudo chown -R www-data:www-data /var/www/laravel
3. Give a write access to storage and cache directories
This is the IMPORTANT step, make sure you give the write permission both to storage and bootstrap/cache directories.
sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache
Still Not Working?
If the above steps are still not working, you can try to run the following commands in the shell:
# 1. Clear Laravel cache
php artisan cache:clear
# 2. Delete the compiled class
php artisan clear-compiled
# 3. Regenerate the composer autoload file
composer dump-autoload
Yet, it still not working?
For the last resource, try to set the permission to 777 which means any users will have the ability to read and write to the given directories.
sudo chmod -R 777 /var/www/laravel/storage
sudo chmod -R 777 /var/www/laravel/bootstrap/cache
Hope this help.
I had the same issue with Centos7 ... I tried EVERYTHING. Finally, I found a post on stackoverflow suggesting it could be selinux. I disabled selinux and it worked!
If you are in Centos, then disable SELinux enforcement
run below command to do this.
setenforce 0
In my case, this solution worked very well. Hope this will help.
U should try somethings like this-
php artisan cache:clear
php artisan clear-compiled
sudo chmod -R 777 storage/ -R
composer dump-autoload
U can go to this question for more details.
I finally found a solution.. Seems like the problem had to do with PSR-4 and auto importing classes
So here's the solution for anyone else having this problem:
php artisan clear-compiled
composer dump-autoload
php artisan optimize
php artisan cache:clear
use this
chcon -R -t httpd_sys_rw_content_t storage

Symfony2 NGINX Unable to create the cache directory

I know this may seem as a repeated question but so far I have 4 hours digging around unable to find a straight answer to this. I have setup a brand new CentOS 7 and I installed NGINX (https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7) + PHP 5.6 (https://webtatic.com/packages/php56/) and MariaDB.
Following the guides I was able to make the NGINX serve PHP files successfully.
I have a project in Symfony2 which I am trying to get in this server, my problem is when I try to open up http://server/web/app.php. I get the following error:
Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory (/usr/share/nginx/html/app/cache/prod) ' in ................
I read in the documentation (http://symfony.com/doc/current/book/installation.html) that I should work around my permissions so I was able to apply the following:
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
Since I have CentOS 7 from scratch that was the option that I was able to apply. However, I still have the same issue, after this approach these are the other options I've tried:
changed the owner for app/cache and app/logs to nginx:nginx
changed the permissions to 777 to the above folders
verified and re-verified that PHP is running under the nginx user, I had to change this in the www.conf
if I do ls -Al it shows that app/cache and app/logs are owned by nginx
restarted services after each single change
restarted the server completely
cleared the cache and performed warmup and still
Tried the umask option provided by symfony and still.
So after all these options I still get the same issue, to other people out there with this problem it's simply following the Symfony2 guidelines for applying the ACL and puff, done. But this is not the case.
What am I missing?
Ps: I double checked the permissions of the folder..and I am in the server as root.
Update 1
I re-installed CentOS 7 from scratch and followed https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7 and in combination with Symfony's suggestion for the configuration file and still I get the " Uncaught exception 'RuntimeException' with message 'Unable to create the cache directory" error
You might be running into SELinux issues, since CentOS 7 ships with SELinux running by default.
You can test by executing setenforce 0 to see if that solves the problem. If that works, you need to configure SELinux to allow the nginx user to write to the cache directory, and then re-enable SELinux. SELinux does provide some good security, so it would be best to figure out how to configure it properly, as opposed to turning it off completely.
You need to add a local custom rule (policy module), suppose you have your application under the /usr/share/nginx/www directory:
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/www/.+/app/(cache|logs)(/.*)?'
You need to restore the contexts for all the files in that dir:
restorecon -Rv /usr/share/nginx/www/your-app-dir
Change the group of the cache and logs directories and add permissions for the group:
chgrp -R apache /usr/share/nginx/www/your-app-dir/app/{cache,logs}
chmod -R 775 /usr/share/nginx/www/your-app-dir/app/{cache,logs}

Symfony2 Installation problems

I'm new to Symfony2 and I'm trying to create a new project in my local environment following the steps listed in the Symfony book:
Download
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
Create Project (path relative to /var/www/)
$ php composer.phar create-project symfony/framework-standard-edition grupo76/final/ Symfony 2.5.*
1st Problem: I have to add /web/ to paths
So, now I have to test the configuration by hitting
http://localhost/grupo_76/final/config.php
I get a 404, and have to change the address to
http://localhost/grupo_76/final/web/config.php
It complains about permissions on /app/logs and /app/cache but that's ok. I ran
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
2nd Problem: app_dev.php is completely broken
Then, the book suggest I should hit
http://localhost/grupo_76/final/app_dev.php
but, again, I need to add /web/
http://localhost/grupo_76/final/web/app_dev.php
The page renders, but full of errors like this No route found for "GET /"
Please, see attachment.
I'm running:
Apache2
PHP 5.5.9-1ubuntu4
Ubuntu 14.04
1st Problem
You have to configure a web server. There is an official documentation for configuring web server for both Apache and Nginx. What you are doing is accessing app_dev.php or config.php from your localhost host relatively. See: http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
2nd Problem
You have to define routing for your / path (homepage, in other words). See: http://symfony.com/doc/current/book/routing.html
A virtual-host won't help you, I already tried that, my working solution is:
1- Delete the old Symfony installation
2- Install a fresh one as root (sudo su)
3- Grant permissions to /var/www/html with chmod 777 -R /var/www/html"
Just take in count that you may want to readjust the folder's permissions when you get it working, to your specific scenario.

Symfony 2 : dev to pro = blank page

first I took you to excuse my English.
I made a website with Symfony which works fine locally. (dev and prod).
But as soon as I put online the dev version works but the prod version displays a blank page.
Lighttpd does not give me error.
PHP does not give me error.
Cache and log are writable.
I do not understand anything.
OK, this is highly undesirable situation as you need to squash bugs one-by-one. It could be as benign as missing php module or some major httpd misconfiguraion.
Some steps that should shed some light on the issue:
Fire up the Terminal (you do have ssh access, right?)
Check logs (both httpd and symfony)
httpd log:
tail -f /var/log/httpd/error_log
... and refresh your page
Symfony
tail -f /path-to-your-symfony-app/app/logs/prod.log
... again, refresh your page
In your comment, you said you encountered HTTP500 error. Is that Apache's or Symfony's HTTP500?
If you do not hape ssh access upload your app_dev.php and run it directly. Be sure to add your IP address to list of allowed (within the file)
Change your permission on cache and log folders in 777.
chmod 777 cache/ log/ -R
php symfony cc
As the documentation says:
Instead of the Welcome Page, you may see a blank page or an error page. This is caused by a directory permission misconfiguration. There are several possible solutions depending on your operating system. All of them are explained in the Setting up Permissions section.
And then you should follow these steps:
rm -rf app/cache/*
rm -rf app/logs/*
HTTPDUSER=' ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1'
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:'whoami':rwX app/cache app/logs
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:'whoami':rwX app/cache app/logs
I did the deployment as guided in this official docs
In my case the update of symfony before deploying was not BC with an old Bundle, so the clear:cache --env=prod was giving some error. So what I did was to manually delete the prod cache and then clear:cache with the prod environment and it worked: so to resume:
#rm -rf app/cache/prod/* #removes only production cache
rm -rf app/cache/ #removes prod and any other stage cache
php app/console cache:clear --env=prod

Categories