Unable to write files on CentOS

6,645

CentOS is one of those operating systems which often comes with some form of SELinux installed. There are several holy wars as to whether using SELinux is a good thing or a waste, but it is a choice you will have to make.

If you are still a beginner I would recommend that you spend time with SELinux turned off, and then once you have everything running you can optionally enable it and start reconfiguring.

NB: The real world way of doing things is the opposite: start with a completely locked down system and one by one enable things that you need.

Share:
6,645

Related videos on Youtube

aborted
Author by

aborted

Updated on September 18, 2022

Comments

  • aborted
    aborted over 1 year

    I have a VPS running on CentOS 6 and apache2 as webserver. I have several PHP scripts running in my server, and everything goes smooth, except to file writing. For some reason, my PHP scripts are unable to write or create files.

    I am using the default location /var/www/html/ which is owned by root:root.

    I have tried temporarily CHMODing directories and files, without success.
    I also tried setting the folder permissions to 777, same result - no success.

    This is quite strange, and I'm a beginner when it comes to managing VPSes.

    I added the PHP error log line to php.ini and told it to store the log in /var/log/

    • ganesh
      ganesh over 10 years
      1) Under which user ID to the PHP scripts run? 2) To which directories are you trying to write? 3) Chmod 777 is a really bad idea. Please undo. 4) Any errors in the logs?
    • aborted
      aborted over 10 years
      1) I don't know, how do I find out? 2) /var/www/html 3) It was just a try, I undid it immediately 4) No, if the logs are in /var/log/ then there is no error log at all.
    • aborted
      aborted over 10 years
      1) I'm using apache2, sorry for not mentioning that. 2) It appears that root is the owner of /var/www/html (I used la -l, to check that, two columns keep saying "root"). 3) I added the PHP error log line myself on php.ini, and I told it to store the log in /var/log/
    • ganesh
      ganesh over 10 years
      Do you have an apache2.conf? Does it have these two lines and what is the exact content of those lines? User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP}
    • aborted
      aborted over 10 years
      If apache2.conf is the same as httpd.conf, then I do have it. If you're asking exactly for apache2.conf, I honestly don't know if I have it. My httpd.conf doesn't have those two lines.
    • Lawrence
      Lawrence over 10 years
      What errors come up when you try to use php to write a file ?
    • aborted
      aborted over 10 years
      @Lawrence, none at all, it just doesn't work -- nothing happens.
    • LuckySpoon
      LuckySpoon over 10 years
      @Dugi I think @Hennes meant that there'll be a line that starts User followed by a user name, and a line that starts Group that's followed by a group name. E.g. User apache Group apache - chown -R the directory to that user/group combo - e.g. chown -R apache:apache /var/www/html Edit: Also 777 should only be used where needed - 644 should get you through most places, and just 777 what needs to be written to, and even then only if 644 and 755 doesn't do it.
    • ganesh
      ganesh over 10 years
      I meant it slightly more detailed. And 777 (WORLD WRITABLE!) is not a good idea. It is even such a bad idea that you may find web servers actually blocking the directory containing the HTML files because it is unsafe. But other than that, yes, that is where I was trying to guide him to.
  • aborted
    aborted over 10 years
    I'm running an image hosting website, and I need to store all images inside a folder. I also tend to store logs in .log text files instead of the database, and I can't seem to be able to append text to the files.
  • harrymc
    harrymc over 10 years
    Use PHP as above to create a directory and a file in it, then examine its owner and permissions. That will give you all the info you need.
  • Dave Hollingworth
    Dave Hollingworth about 10 years
    Thank you so much for this - I've had to migrate a Debian server to CentOS and for the life of me I couldn't work out why the exact same configuration wouldn't work (I was getting file permission errors with PHP all over the place), until I turned SELinux off... if it's useful for anyone reading this I followed the instructions here to switch it off