Laravel permissions on nginx - php

I am deploying a project to Ubuntu on Digital Ocean, using these
instructions.
I am running into permissions issues whereby if I give these permissions
sudo chown -R www-data.www-data /var/www/project/storage sudo chown -R www-data.www-data /var/www/project/bootstrap/cache
I get access to my login page (though a 500 error on every other route), but I also get The stream or file "/var/www/project/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied whenever I try to run php artisan optimize, route:clear, etc.
If I set the permissions to this
sudo chown -R $USER /var/www/project/storage sudo chown -R $USER /var/www/project/bootstrap/cache
I get the opposite problem, wherein I can run artisan commands and write to the log, but no pages are accessible.
Any help would be appreciated

In addition to chmod -R www-data.www-data /var/www/project which sets the group and owner, you might also need to change the specific dir permissions.
sudo chmod -R 775 /var/www/project/storage/
sudo chmod -R 775 /var/www/project/bootstrap/cache
If you're trying to perform operations in your /var/www/project directory on the server, ensure that the user you are logged onto the server as is in the www-data group.
You can check the groups you're in simply by typing groups in your shell. If you don't see www-data in the list returned, add yourself.
usermod -aG www-data $USER
Once you've added yourself to the www-data group you will need to relog for the change to take effect.

You need to change boostrap cache folder and laravel.log file into read, write, and execute. Go to your laravel project, and put this code
sudo chmod 777 bootstrap/cache/
sudo chmod 777 storage/logs/*.log

Related

The stream or file "/var/www/alphacomodate/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied

Hello i created a linux server on microsoft Azure to host my Laravel application , it works perfectly on my local machine but it doesn't work on the server. I changed the permission to 0755 but nothing seems to be working. i still get the same error. please can someone give me any suggestions or a suitable alternative.
Thanks in advance
Do not change octal permission numbers. By default Laravel have setted up that. You need to just give the right ownership for "storage/" folder, and for "bootstrap/cache" folder as well for addition. Like this:
# One-time command for your PC
# give sudo-privileges your current username
sudo usermod -a -G www-data $USER
# Here keep in mind that you need to replace "www-data" to the actual webserver name.
# For some many cases it's the "www-data", but it could be "apache", "httpd" etc for other cases for Apache, or something else
#
# Setup ownerships/permissions
sudo chown -R $USER:www-data storage/ bootstrap/cache/
sudo chgrp -R www-data storage bootstrap/cache/
sudo chmod -R ug+rwx storage bootstrap/cache/
For detailed explanation read this

Wordpress plugin install: Could not create directory

I'm using WordPress on centos 6.
I try to install a plugin. But I got this error:
Installing Plugin: bbPress 2.5.9
Downloading install package from https://downloads.wordpress.org/plugin/bbpress.2.5.9.zip…
Unpacking the package…
Could not create directory.
How can I resolve this?
P/S: I run this command:
sudo -u root touch /var/www/html/wordpress/wp-content/plugins/test.txt
and it works. But I still get that error.
You only need to change the access permissions for your WordPress Directory:
chown -R www-data:www-data your-wordpress-directory
You can fix this by using the following commands. You should first be in the root folder of Wordpress.
sudo chown -R www-data:www-data wp-content/plugins/
sudo chmod 775 wp-content
sudo chown -R www-data:www-data wp-content/
The user that is running your web server does not have permissions to write to the directory that Wordpress is intending to create the plugin directory in. You should chown the directory in question to the user that is running Wordpress. It is most likely not root.
In short, this is a permissions issue. Your touch command is working because you're using it as root, and root has global permissions to write wherever it wants.
A quick solution would be to change the permissions of the following:
/var/www/html/wordpress/wp-content
/var/www/html/wordpress/wp-content/plugins
Change it to 775.
After installation, don't forget to change it back to the default permissions.. :D
I had to give ownership of /plugins and /upgrade to the server, nothing else.
$ cd /var/www/wordpress/wp-content
$ sudo chown www-data:www-data /plugings
$ sudo chown www-data:www-data /upgrade
Running Apache server on Ubuntu 18.04. Maybe more dirs will need to be changed later. Anyways, I plan to restore permissions once I finish editing, as suggested in this anwser.
If you have installed wordpress using apt, the config files are split in multiple directories. In that case you need to run:
sudo chown -R -h www-data:www-data /var/lib/wordpress/wp-content/
sudo chown -R -h www-data:www-data /usr/share/wordpress/wp-content/
The -h switch changes the permissions for symlinks as well, otherwise they are not removable by user www-data
To solve permission issue on plugins and themes on localhost or production quickly, you just run this
sudo chmod 757 wp-content/themes
sudo chmod 757 wp-content/plugins
if take care permission on production, you can run
sudo chown -R www-data:www-data wp-content/themes
sudo chown -R www-data:www-data wp-content/plugins
If you are mac user , using XAMP
Go to the htdocs folder and open the terminal on the folder , as shown in the screenshot
Then Type the following command on the Terminal
**sudo chmod -R 777 <your wordpress folder Name>/**
E.g sudo chmod -R 777 wordpress/
CentOS7 or Ubuntu 16
1.
WordPress uses ftp to install themes and plugins.
So the ftpd should have been configured to create-directory
vim /etc/pure-ftpd.confg
and if it is no then should be yes
# Are anonymous users allowed to create new directories?
AnonymousCanCreateDirs yes
lastly
sudo systemctl restart pure-ftpd
2.
Maybe there is an ownership issue with the parent directories.
Find the Web Server user name and group name if it is Apache Web Server
apachectl -S
it will print
...
...
User: name="apache" id=997
Group: name="apache" id=1000
on Ubuntu it is
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
then
sudo chown -R apache:apache directory-name
3.
Sometimes it is because of directories permissions.
So try
sudo chmod -R 755 directory-name
in some cases 755 does not work. (It should & I do not no why) so try
sudo chmod -R 777 directory-name
4.
Maybe it is because of php safe mode.
So turn it off in the root of your domain
vim php.ini
then add
safe_mode = Off
NOTE:
For not entering FTP username and password each time installing a theme we can configure WordPress to use it directly by adding
define('FS_METHOD','direct');
to the wp-config.php file.
If anyone using shared hosting and get same problem it may be disk space issue. In that case contact with your hosting support and ask them to increase disk space of your acocunt.
You need to change the permission of the directory
At first change the user HTML folder (replace yourcomputerusername)
sudo chown yourcomputerusername:yourcomputerusername /var/www/html
Next change the permission for the user
cd /var/www/html
sudo chmod -R goa=rwx wordpress
or
sudo chmod -R 777 wordpress/wp-content
I was on XAMPP for linux localhost and this worked for me:
sudo chown -R my-linux-username wp-content
What I end up doing is every time I create a WordPress project. in /www/html
I run below command
sudo chown www-data:www-data wordpress_folder_name -R
hope this will help someone.
For me the problem was FTP server that WP is using to upload update. It had writting disabled in configuration, so just enabling it fixed the problem.
Shame on WordPress for providing such misleading error message.
You could try
sudo chmod goa=rwx -R /var/www/html
None of the above work for me except this one.
sudo chown daemon:daemon /opt/bitnami/apache/htdocs/
Don't forget to change /opt/bitnami/apache/htdocs/ to your directory
Webserver user must have write access to directories to perform such operations, so you can try to change owner of files to webserver user (apache in this example, but can be differ from yours)
chown -R apache YOUR_BLOG_DIRECTORY
If you are using some app that wraps http, you have to set these user in the command.
sudo chown -R [desireduser]:[desireduser] wp-content/
By example, if you are using lampp, the users that init httpd is "daemon" and the command that works will be:
sudo chown -R [desireduser]:[desireduser] wp-content/
You can search in your httpd.conf file
To solve permission issue on ubuntu server, you just run this
sudo chmod 777 -R 'wordpress wp-content file location'
for example.
sudo chmod 777 -R /usr/share/wordpress/wp-content
wordpressProject is the project name.
/var/www/html/wordpressProject sudo chmod -R 777 wp-content
Thanks. It will work.
Absolutely it must be work!
Use this
chown -Rf www-data:www-data /var/www/html

How to fix Error: mkdir(): Permission denied when running composer

I have a ec2 image and get the following error when trying to create a new laravel project.
[ErrorException] mkdir(): Permission denied
Here is the command:
composer create-project laravel/laravel mydir
I can write to the folder with my ec2-user username but do I need to add permission for composer to write?
Quick Answer:
For security reasons, lets keep root as the owner and just allow ourselves to read, write, and execute within the area which we will be working in.
Change directories
cd /var/www/
Change group ownership
sudo chown -Rv root:$USER .
Add priviledges to our group
sudo chmod -Rv g+rw .
For the grand finale, go ahead and create your new laravel project
composer create-project laravel/laravelprojectName--prefer-dist
Congratulations, We're done. :)
In-Depth Explanation
Explanation behind the solution:
By default you have two users; root and $USER(this is the global variable, but the actual name is whatever you put as your username). Every user created comes with a group as well. So if my username is john, I have a group called john.
There are many ways to solve this problem but I perfer this route for a few reasons:
I still want root to be the owner of the files for security reason.
I am working in a localhost so there shouldn't be any other (guest)users writing on my files.
In order to achieve this we will need to edit the group ownership and add the proper permissions. We will leave the others as is and only allow them to read and execute the files.
So lets get started:
I like being in the directory that I am making changes to in order to avoid mistakes so first we will need to enter the the directory which we will be working in.
cd /var/www/
We will need to change the group ownership of the directories which we will be working in. Starting from the directory which we are in and all the future directories and files we will create underneath this directory. So basically any children directories from now on will be own by our group.
sudo chown -Rv root:$USER .
chown = command to change owner.
-R = Recursive - This is basically stating to perform the same command to all the directories within this directory.
-v = verbose - we are stating here to keep us updated by showing us what is actually happening.
root:$USER = This is were we are setting the ownership. The first word before the colon(:) states the root will be the single owner. The second word after the colon(:) states that the group owner will be $USER(the current user).
. = This just means 'here, in this directory'.
Now we are going to add the right privileges to our group. Keep in mind this is where we allow root to be the primary owner while ONLY allowing ourself's to create changes(of course, besides root). As I mentioned before, this does not allow other users to create any changes what so ever.
sudo chmod -Rv g+rw .
chmod = command to change modifications, in this case, privileges.
-Rv = Recursive & Verbose
g = this states who will receive the modifications. In our case g-group Other options are u-user and o-other.
+ = symbolizes add
r = symbolizes read
w = symbolizes write
Now we are done. Try creating a new laravel project.
composer create-project laravel/laravelprojectName` --prefer-dist
Note: If this is not working, enter the command ls -al | grep $USER while inside the /var/www/ directory.
If you get this:
drwxrw-r-x
You are missing the executable(x) privilege for the group. The first x is for the user and last x is for others. There should be middle x for the group.
Run the following command and you should be all set to go:
sudo chmod -Rv g+x .
Now if you run ls -al | grep $USER, you should get:
drwxrwxr-x
add "www" group and add your user to this group
sudo groupadd www
sudo usermod -a -G www ec2-user
logout/login
set ownership and write permissions
sudo chown -R root:www /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} +
find /var/www -type f -exec sudo chmod 0664 {} +
Referencing: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-LAMP.html
I ran into the similar ErrorException, when creating an additional project inside the Homestead/Vagrant environment.
I had previously added folders and path to the Homestead.yaml.
The answers of Oscar and Jens A. Koch pointed into the right direction, but go too far on my dev-machine.
Working narrower solution inside Homestead-Box:
sudo mkdir <ProjectDir>
sudo chgrp vagrant <ProjectDir>
sudo chmod g+wx <ProjectDir>
composer create-project --prefer-dist laravel/laravel <Project>
The gist of the problem
Installing Laravel inside /var/www/html folder is a common requirement. However, the system defaults usually sets the user and group for this folder and its parent(www) to root And composer will complain if you run it as root.
Solution
Find the user corresponding to your apache/nginx server. It usually has names like www,www-data etc. You can run the below command to get a clue
cat /etc/group | grep www
With the knowledge of the server user(from above), you can run the below commands. I am assuming that your server user is www-data
# Under your user account, change directories
cd /var/www/
# Change group ownership
sudo chown -Rv root:www-data .
# Give all the privileges to the group
sudo chmod -Rv g+rwx .
# Add your user name to www-data group, use 'who' command to get your username
sudo usermod -a -G www-data your_user_name
# Verify that your user name is in the www-data group by typing
group your_user_name
# Very important, logout of the system and come back to this folder
# Do the the laravel install
composer create-project --prefer-dist laravel/laravel projectName
# Not finished yet! recursive make the www-data the group owner of projecName which the folder in which Laravel site resides by now
sudo chown -R :www-data projectName
Make sure that you have read/write folder permission. It works to me.
Like in ubuntu
var/www/html
So if you want to make dir in html folder then first give permission to html folder. Then you are done. Thanks.
This Problem with a Vagrant box
If you tried to install something with composer in a new vagrant box, it is possible that when installing the vagrant you have specified the path you are allowed to use.
So check your personal .yml file in order to check this under
\ansible\vars
there you have something like this
vagrant_local:
vm:
name: oxideshop
hostname: oxideshop.local
aliases:
- www.oxideshop.local
app_shared_folder:
source: oxideshop
target: /var/www/oxideshop
You have to make sure, that the app_shared_folder target is the same you use for the composer installation.
Example:
composer create-project oxid-esales/oxideshop-project
/var/www/oxideshop dev-b-6.1-ce
Make sure that you have read/write folder permission.
LIKE: sudo chown -R USER NAME:www-data /PROJECT PATH/USER NAME/PROJECT NAME/
Example:
$ sudo chown -R tubai:www-data /home/tubai/firstlara/
Run the following commands
cd /var/www
sudo chown -R $USER:www-data html/
sudo chmod -R 775 html/
I had the same problem and executing the command using sudo just worked for me:
sudo composer create-project laravel/laravel mydir
cd to the directory where you currently want to install laravel and change directory access with
sudo chmod -R 777 ./
type your password and you are good to resume to the installation

How to fix Error: laravel.log could not be opened?

I'm pretty new at laravel, in fact and I'm trying to create my very first project. for some reason I keep getting this error (I haven't even started coding yet)
Error in exception handler: The stream or file "/var/www/laravel/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/laravel/bootstrap/compiled.php:8423
I've read this has something to do with permissions but chmod -R 775 storage didn't help at all.
Never set a directory to 777. you should change directory ownership. so set your current user that you are logged in with as owner and the webserver user (www-data, apache, ...) as the group.
You can try this:
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
then to set directory permission try this:
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Update:
Webserver user and group depend on your webserver and your OS. to figure out what's your web server user and group use the following commands. for nginx use:
ps aux|grep nginx|grep -v grep
for apache use:
ps aux | egrep '(apache|httpd)'
Never use 777 for directories on your live server, but on your own machine, sometimes we need to do more than 775, because
chmod -R 775 storage
Means
7 - Owner can write
7 - Group can write
5 - Others cannot write!
If your webserver is not running as Vagrant, it will not be able to write to it, so you have 2 options:
chmod -R 777 storage
or change the group to your webserver user, supposing it's www-data:
chown -R vagrant:www-data storage
To fix this issue, you need to change the ownership of the directory to the unix user that the webserver uses.
Get out of the VM
Using the console, go to your synced folder (vagrant)
sudo chown -R $USER:www-data storage
chmod -R 775 storage
Even though I created the project within the VM using the VM user, the folder belonged to the user in the real computer; so, when trying to
Now it's working.
Thanks to all those that helped me figure this thing out
EDIT:
Actually, it still wasn't working, it still gave me a "permission denied" problem.
Here's what I did, I modified my Vagrantfile like this:
config.vm.synced_folder "./app","/var/www/", create:true,
:owner => "vagrant",
:group => "www-data",
:mount_options => ["dmode=775","fmode=664"]
It also may be SELinux. (Centos, RedHat)
Determine status of SElinux on terminal:
$ sestatus
If status is enabled, write command to disable SElinux
$ setenforce Permissive
Or you may execute this command
$ sudo setenforce 0
You need to adjust the permissions of storage and bootstrap/cache.
cd into your Laravel project.
sudo chmod -R 755 storage
sudo chmod -R 755 bootstrap/cache
You can try 777 if 755 doesn't work. 777 is not secure though!
Depending on how your web server is setup, you may be able to be more specific with your permissions, and only grant them to your web server user. Google WEB SERVER NAME Laravel file permissions for more information.
At the time of writing, this is for Laravel 5.4
It might be late but may help someone, changing directory permissions worked for me.
Assuming that your Laravel project is in /var/www/html/ directory. Goto this directory.
cd /var/www/html/
Then change permissions of storage/ and bootstrap/cache/ directories.
sudo chmod -R 775 storage/
sudo chmod -R 775 bootstrap/cache/
If permission 775 does not work, try setting 777. (Warning! This is the most relaxed permission, use with care).
sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/
cPanel: If you are on cPanel and don't have terminal available you can change permission by right clicking on the mentioned directory and it's sub-directories.
Add to composer.json
"scripts": {
"post-install-cmd": [
"chgrp -R www-data storage bootstrap/cache",
"chmod -R ug+rwx storage bootstrap/cache"
]
}
After composer install
Run following commands and you can add sudo at starting of command depends on your system:
chmod -R 775 storage/framework
chmod -R 775 storage/logs
chmod -R 775 bootstrap/cache
1- ‍The nginx user and php-fpm user and app owner-user must be the same:
run command sudo vi /etc/nginx/nginx.conf change like bellow:
user nginx nginx;
run command sudo vi /etc/php-fpm.d/www.conf change like bellow:
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
then restart nginx and php-fpm service
run below command
sudo chown nginx:nginx -R "your_project_path"
2- change file SELinux security context by run the following commands in the project path
chcon -R -t httpd_sys_content_t .
chcon -R -t httpd_sys_rw_content_t .
For all Centos 7 users on a Laravel context, there is no need to disable Selinux, just run these commands:
yum install policycoreutils-python -y # might not be necessary, try the below first
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/storage(/.*)?" # add a new httpd read write content to sellinux for the specific folder, -m for modify
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/bootstrap/cache(/.*)?" # same as the above for b/cache
restorecon -Rv /var/www/html/ # this command is very important to, it's like a restart to apply the new rules
Lastly, make sure your hosts, ips and virtual hosts are all correctly for remote accessing.
Selinux is intended to restrict access even to root users, so only the necessary stuff might be accessed, at least on a generalist overview, it's extra security, disabling it is not a good practise, there are many links to learn Selinux, but for this case it is not even required.
If you use cmd
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
If you use GUI
First go to the project and right click on the storage and check the properties and go to the Permissions tab
Change the permissions using below code
sudo chmod -R 777 storage
Then your file properties may be
Then check your settings and execute laravel command it will work :)
I stuck on this issue tried different commands but these will help to solve the problem
php artisan route:clear
php artisan config:clear
php artisan cache:clear
Hope it's helped others too.
Just run the following command from Project root Directory -
sudo chmod -R 775 storage
sudo chown -R $USER:www-data storage
In Laravel, you should set ACL on storage and cache directory so that web server user can read/write on the directory. Open a new terminal and run following:
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 bootstrap/cache storage/
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/
References:
https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd
https://linux.die.net/man/1/setfacl
Maximum people's are suggesting to change file permission 777 or 775, which I believe not an appropriate approach to solve this problem. You just need to change the ownership of storage and bootstrap folder.
In below Image you can see all my files/folder are under the root user(except storage and bootstrap, because I changed the ownership ),but I logged in as a administrator(before changing ownership) that's why it always giving permission denied. So I need to change the ownership of this two folder to administrator
So how I did this,
go to your project directory and run below commands.
sudo chown -R yourusername:www-data storage,
sudo chmod -R ug+w storage,
sudo chown -R yourusername:www-data bootstrap,
sudo chmod -R ug+w bootstrap
try this
cd /var/www/html
setenforce 0
service httpd restart
This is what I do if I'm running Apache:
sudo chown -R $USER:www-data my_laravel_project/
sudo chmod -R 775 my_laravel_project/storage
sudo chmod -R 775 my_laravel_project/bootstrap/cache
cd my_laravel_project
php artisan optimize:clear
This solution is specific for laravel 5.5
You have to change permissions to a few folders:
chmod -R -777 storage/logs
chmod -R -777 storage/framework
for the above folders 775 or 765 did not work for my project
chmod -R 775 bootstrap/cache
Also the ownership of the project folder should be as follows (current user):(web server user)
I managed to fix it as I was only granting permission to via this command:
Copy Code
sudo chmod -R 775 storage
The fix was to add this:
Copy Code
sudo chmod -R ugo+rw storage
I wasn't too keen on changing my folder permissions to 777. Here's how I went about fixing this issue.
First, I changed the user who is running the web server on my local machine(I run nginx, but the principles apply everywhere):
$> sudo vim /etc/nginx/nginx.conf
user <my_user> #inside nginx.conf
service nginx reload
Afterwards, I created another index.php file under the public/ folder to find out who was running my php-fpm version and where I would go about changing that:
<?php
phpinfo();
?>
Reloading the page, I found out that www-data was the user(under the environment section). I also found out I was running php 7.1. I proceeded to change the user:
$> sudo vim /etc/php/7.0/fpm/pool.d/www.conf
#Look for www-data or the following variables: user, group, listen.user, listen.group.
Finally, I gave the following permissions to folders:
sudo chmod -R 775 ./storage/
Now, I made sure that I was the owner of the folders by using a simple:
ls -al
If you set the server and php-fpm users to yourself and the folders are owned by root for example, then you will keep encountering this issue. This can happen if you did a sudo laravel new <project> as root. In that case, make sure you use a recursive chown command on your project to change the user:group settings. In most default cases, www-data is the main setting for the server and php, in that case it's a matter of making sure the folder isn't out of www-data's reach.
My project is setup in my home directory. On Ubuntu 16.04 and Laravel 5.5.
In my particular case I had a config file generated and cached into the bootstrap/cache/ directory so my steps where:
Remove all generated cached files: rm bootstrap/cache/*.php
Create a new laravel.log file and apply the update of the permissions on the file using:
chmod -R 775 storage
Tried anything suggested here without success.
What worked for me was:
sudo chmod -R ugo+rw storage
sudo chmod -R ugo+rw storage/logs
In Linux
sudo chown -R www-data:root /var/www/name-project-Laravel
sudo chmod 755 /var/www/name-project-Laravel/storage
In Centos & Rockylinnux
chown root:nginx FOLDER_PROJECT -Rf
chmod 775 FOLDER_PROJECT -Rf
cd FOLDER_PROJECT
chmod 777 storage -Rf
setenforce 0
please rate :)
Mac OS solution
I fixed this problem simply by giving the necessary permission to my folders.
Right Click on the logs folder and click on Get Info
At the bottom, you will see Sharing & Permissions. Now give Read & Write access to the folder.
Follow the attachment.
Next step👇🏻
as you probably already know this issue is caused due to absence of write permission on the log folder which is a sub folder of storage.
To solve this problem walkedthrough these sequence of steps
Update composer
sudo composer self-update
Change storage folder write permission
sudo chmod -R ugo+rw storage
Now storage folder should have permission drwxrwxrwx
To check permissions run the following command from project root
ls -l
Also if you face the following error after the step above
ErrorException chdir(): No such file or directory (errno 2)
Just create a folder named public on the project root folder using
sudo mkdir public
ps. For more information about the chmod commands check this
below command would work for sure.
sudo chmod -R ugo+rw storage
Not write any command or not gives any permission simplest way to solved this issue
just restart your system and try it again
it's work for me

file_put_contents(meta/services.json): failed to open stream: Permission denied

I am new to Laravel. I was trying to open http://localhost/test/public/ and I got
Error in exception handler.
I googled around and changed the permission of storage directory using chmod -R 777 app/storage but to no avail.
I changed debug=>true in app.php and visited the page and got Error in exception handler:
The stream or file "/var/www/html/test/app/storage/logs/laravel.log"
could not be opened: failed to open stream: Permission denied in
/var/www/html/test/bootstrap/compiled.php:8423
Then I changed the permissions of storage directory using the command chmod -R 644 app/storage and the 'Error in exception handler' error was gone and a page is loaded. But in there I am getting this:
file_put_contents(/var/www/html/laravel/app/storage/meta/services.json):
failed to open stream: Permission denied
Suggestion from vsmoraes worked for me:
Laravel >= 5.4
php artisan cache:clear
chmod -R 775 storage/
composer dump-autoload
Laravel < 5.4
php artisan cache:clear
chmod -R 775 app/storage
composer dump-autoload
For those facing this problem with Laravel 5, this is a permission issue caused by different users trying to write at the same log file within the storage/logs folder with different permissions.
What happens is your Laravel config probably is setup to log errors daily and therefore your web server (Apache/nginx) might create this file under a default user depending on your environment it can be something like _www on OSX or www-data on *NIX systems, then the issue comes when you might have run some artisan commands and got some errors, so the artisan will write this file but with a different user because PHP on terminal is executed by a different user actually your login user, you can check it out by running this command:
php -i | grep USER
If your login user created that log file your web server you will not be able to write errors in it and vice-versa because Laravel writes log files with 655 permissions by default which only allows the owner to write in it.
To fix this temporary you have to manually give permissions for the group 664 to this file so both your login user and web server user can write to that log file.
To avoid this issue permanently you may want to setup a proper permissions when a new file is create within the storage/logs directory by inheriting the permissions from the directory this answer https://unix.stackexchange.com/a/115632 can help you to tackle with that.
You should not give 777 permissions. It's a security risk.
To Ubuntu users, in Laravel 5, I sugest to change owner for directory storage recursively:
Try the follow:
sudo chown -R www-data:www-data storage
In Ubuntu based systems, www-data is apache user.
For everyone using Laravel 5, Homestead and Mac try this:
mkdir storage/framework/views
some times SELINUX caused this problem;
you can disable selinux with this command.
sudo setenforce 0
NEVER GIVE IT PERMISSION 777!
go to the directory of the laravel project on your terminal and write:
sudo chown -R your-user:www-data /path/to/your/laravel/project/
sudo find /same/path/ -type f -exec chmod 664 {} \;
sudo find /same/path/ -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
This way you're making your user the owner and giving privileges:
1 Execute, 2 Write, 4 Read
1+2+4 = 7 means (rwx)
2+4 = 6 means (rw)
finally, for the storage access, ug+rwx means you're giving the user and group a 7
Problem solved
php artisan cache:clear
sudo chmod -R 777 vendor storage
this enables the write permission to app , framework, logs Hope this will Help
For vagrant users, the solution is:
(in vagrant) php artisan cache:clear
(outside of vagrant) chmod -R 777 app/storage
(in vagrant) composer dump-autoload
Making sure you chmod in your local environment and not inside vagrant is important here!
Try again with chmod -R 755 /var/www/html/test/app/storage. Use with sudo for Operation not permitted in chmod. Use Check owner permission if still having the error.
As per Laravel 5.4 which is the latest as I am writing this, if you have any problem like this, you ned to change the permission.
DO NOT LISTEN TO ANYONE WHO TELLS YOU TO SET 777 FOR ANY DIRECTORY.
It has a security issue.
Change the permission of storage folder like this
sudo chmod -R 775 storage
Change bootstrap folder permission like this
sudo chmod -R 775 bootstrap/cache
Now please make sure that you're executing both commands from your application directory. You won't face problems in future regarding permission. 775 doesn't compromise any security of your machine.
Suggest the correct permission, if for Apache,
sudo chown -R apache:apache apppath/app/storage
FOR ANYONE RUNNING AN OS WITH SELINUX: The correct way of allowing httpd to write to the laravel storage folder is:
sudo semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/www/storage(/.*)?'
Then to apply the changes immediately:
sudo restorecon -F -r '/path/to/www/storage'
SELinux can be a pain to deal with, but if it's present then I'd STRONGLY ADVISE you learn it rather than bypassing it entirely.
If you have Laravel 5 and looking permanent solution , applicable both php artisan command line usage and Apache server use this:
sudo chmod -R 777 vendor storage
echo "umask 000" | sudo tee -a /etc/resolv.conf
sudo service apache2 restart
See detailed explanation here.
I had the same issue and the below steps helped me fix the issue.
Find out the apache user - created a test.php file in the public folder with the code
<?php echo exec('whoami'); ?>
And run the file from the web browser. It would give the apache user. In my case, it is ec2-user as I was using the aws with cronjob installed in /etc/cron.d/. It could be different user for others.
Run the below command on the command line.
sudo chown -R ec2-user:<usergroup> /app-path/public
You need to identify and use the right "user" and "usergroup" here.
I had the same problem but in the views directory:
file_put_contents(/var/www/app/storage/framework/views/237ecf97ac8c3cea6973b0b09f1ad97256b9079c.php): failed to open stream: Permission denied
And I solved it cleaning the views cache directory with the following artisan command:
php artisan view:clear
Xampp for use:
cd /Applications/XAMPP/htdocs
chmod -R 775 test/app/storage
From Setting Up Laravel 4.x on Mac OSX 10.8+ with XAMPP
Any time I change app.php I get a permission denied writing bootstrap/cache/services.json so I did this to fix it:
chmod -R 777 bootstrap/cache/
rm storage/logs/laravel.log
solved this for me
Setting permission to 777 is definitely terrible idea!
... but
If you are getting permission error connected with "storage" folder that's what worked for me:
1) Set "storage" and its subfolders permission to 777 with
sudo chmod -R 777 storage/
2) In browser go to laravel home page laravel/public/ (laravel will create necessary initial storage files)
3) Return safe 775 permission to storage and its subfolders
sudo chmod -R 775 storage/
If using laradock, try chown -R laradock:www-data ./storage in your workspace container
In my case solution was to change permission to app/storage/framework/views and app/storage/logs directories.
After a lot of trial and error with directory permissions I ended up with an epiphany...there was no space left on the disk's partition. Just wanted to share to make sure nobody else is stupid enough to keep looking for the solution in the wrong direction.
In Linux you can use df -h to check your disk size and free space.
This issue actually caused by different users who wants to write/read file but denied cause different ownership. maybe you as 'root' installed laravel before then you login into your site as 'laravel' user where 'laravel' the default ownership, so this is the actually real issue here. So when user 'laravel' want to read/write all file in disk as default, to be denied, cause that file has ownership by 'root'.
To solving this problem you can follow like this:
sudo chown -hR your-user-name /root /nameforlder
or in my case
sudo chown -hR igmcoid /root /sublaravel
Footnote:
root as name first ownership who installed before
your-user-name as the default ownership who actually write/read in site.
namefolder as name folder that want you change the ownership.
If you use Linux or Mac, even you can also run in ssh terminal. You can use terminal for run this command,
php artisan cache:clear
sudo chmod -R 777 storage
composer dump-autoload
If you are using windows, you can run using git bash.
php artisan cache:clear
chmod -R 777 storage
composer dump-autoload
You can download git form https://git-scm.com/downloads.
If anyone else runs into a similar issue with fopen file permissions error, but is wise enough not to blindly chmod 777 here is my suggestion.
Check the command you are using for permissions that apache needs:
fopen('filepath/filename.pdf', 'r');
The 'r' means open for read only, and if you aren't editing the file, this is what you should have it set as. This means apache/www-data needs at least read permission on that file, which if the file is created through laravel it will have read permission already.
If for any reason you have to write to the file:
fopen('filepath/filename.pdf', 'r+');
Then make sure apache also has permissions to write to the file.
http://php.net/manual/en/function.fopen.php
Just start your server using artisian
php artisian serve
Then access your project from the specified URL:
I have the same issue when running vagrant on mac. solved the problem by changing the user of Apache server in https.conf file:
# check user for php
[vagrant] ubuntu ~ $ php -i | grep USER
USER => ubuntu
$_SERVER['USER'] => ubuntu
[vagrant] ubuntu ~ $
Run apache under php user instead of user daemon to resolve file access issue with php
# change default apache user from daemon to php user
sudo sed -i 's/User daemon/User ubuntu/g' /opt/lampp/etc/httpd.conf
sudo sed -i 's/Group daemon/Group ubuntu/g' /opt/lampp/etc/httpd.conf
now, php created cache file can be read and edit by apache without showing any access permission error.
I got same errors in my project...
But found out that I forgot to put enctype in my form.
<form method="#" action="#" enctype="multipart/form-data">
Hopes it helps somewhere somehow...
While working on Windows 10 with Laragon and Laravel 4, it seemed to me there was no way to change the permissions manually, since executing chmod-commands in the Laragon-in-built-terminal had no effect.
However, it was possible in this terminal to go to the storage folder and manually add the desired folders like this:
cd app/storage
mkdir cache
mkdir meta
mkdir views
mkdir sessions
The cd-command in the terminal brings you to the folder (you might need to adjust this path to suit your file structure).
The mkdir-command will create the directory with the given name.
I did not have the opportunity to test this approach in Laravel 5, but I expect that a similar approach should work.
Of course there might be a better way, but at least this was a reasonable workaround for my situation (fixing the error: file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream).
First, delete the storage folder then again create the storage folder.
Inside storage folder create a new folder name as framework.
Inside framework folder create three folders name as cache, sessions and views.
I have solved my problem by doing this.

Categories