Installed apache2
, php7
on a dedicated server. I manually created the file 1.txt
and try to write data to it – file_put_contents ("1.txt", data);
.. but it gives an error:
PHP Warning: file_put_contents (1.txt): failed to open stream: Permission denied in / var / www / html /1.php on line 3
also fails to create file via php
ls -l
:
- rw-r - r-- 1 webuser webuser Oct 65 17 15:53 1.php
-rw-r - r-- 1 webuser webuser Oct 0 17 15:55 1.txt
as you can see, the rights are set to 644
.
how to solve this problem ?? does this mean that I have to give the files the rights 777
?
I look at the hosting, files with 644
rights are created there, and can be easily edited via php
please clarify
Answer 1, authority 100%
If you work under Linux and edit files using SFTP, then check on behalf of whom the file was created (and who, accordingly, owns it). Usually, in FPT clients with SFTP support, files are created as the root user. Naturally, the www-data user (under which both Apache / nginx and PHP / PHP-FPM must be run) cannot access the file owned by root. To solve this problem, you need to assign a new owner to the file / folder:
sudo chown -R www-data: www-data / var / www