Install 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 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 65 окт 17 15:53 1.php -rw-r--r-- 1 webuser webuser 0 окт 17 15:55 1.txt 

As you can see the rights are 644 .

How to solve this problem?? Does this mean that I have to issue 777 permissions to the files?

I look at a hosting, files with the rights 644 are created there, and it is easily edited through php

please clarify

  • File 1.txt must be writable, and read and write permissions are required. - mix
  • Is it accessible for writing? Except 644 rights do not give write access to the owner? - cmd
  • From what user does the php script run? What is his working directory? Will something change if you specify the absolute path to the file? - MrClon
  • php is running it root; same where the site is; nothing has changed - cmd
  • The rights must be for writing and reading by the user under whom the Apache is running. In addition, he must have the rights to write to the directory. - YuS

1 answer 1

If you work under Linux and edit files using SFTP, then check on behalf of whom the file was created (and who owns them, respectively). Usually in FPT clients with SFTP support, files are created as root. Naturally, the user www-data (under which both Apache / nginx and PHP / PHP-FPM must be running) 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 
  • Thanks, it was soaring for 24 hours, there was darkness, in my case the chown -R command www-root helped: www-root / var / www - Valeriy Timof