Giving PHP write permission in Apache
Asked Answered
G

5

6

I'm relatively new to configuring Apache.

I have a PHP script that writes a JSON file based on values retrieved from $_GET.

<?php

    file_put_contents('State.json', "{ do: '" . $_GET['do'] . "' }");

    echo "Success";

?>

I run that code by create an XHR request.

Ally.xhr('/Cream/Foam?do=someCommand');

The page it returns says failed to open stream: Permission denied on line 3.

<Directory "~/Dropbox/Web">
    Options Indexes FollowSymLinks MultiViews

    AllowOverride None

    Order allow,deny
    Allow from all
</Directory>

Those are the permissions given to the root server folder.

What do I need to change to allow PHP to write the file?

(I have pretty much no idea what the block above means.)

Guesswork answered 18/3, 2011 at 21:1 Comment(4)
What about the chmod permissions of the file/directory it is writing to?Carlile
No very good idea to put a webfolder to the root folder. Put it to /var/www/ like the other files. There you have no problems with the rights.Lard
..chmod? I have no idea what that is. Maybe that's what I'm missing.Guesswork
chmod is a POSIX function to set the use mode permissions (ch-change mod-mode). If you use FTP, you can see how these are done.Albumin
G
13

You need to check if the user under which runs apache has permission to write into the directory.

So it's like this:

Your apache server is process. The process runs under some user (say www). The PHP runs under apache. So if you try to write into a directory in PHP it is the same as if the user www logs into the server and tries to create a file in the same directory. So check who is owner of that directory and which permission do it have. You can do it e.g. via ls -la command. If www will be owner of that directory, you will be 100% safe ...

Giralda answered 18/3, 2011 at 21:6 Comment(3)
It runs under the default _www. So if I give it permission this should work?Guesswork
chown www-data:www-data ~/Dropbox/Web and chmod 777 ~/Dropbox/WebLard
Please don't chmod 777 anything on your public-facing web server.Bukharin
E
1

You can try to set the permissions with

chmod function for php and set your directory to /var/www there you have normally enough permissions.

Eusebiaeusebio answered 18/3, 2011 at 21:6 Comment(0)
C
0

Check the file permission either in command line using:

ls -l /path/filename

Or through your ftp client if you have ftp access to the file/dir. If not, you could change the location like Stony said above.

Carlile answered 18/3, 2011 at 21:8 Comment(0)
L
0

Check the file/directory permissions that it is trying to write to. Make sure that it is writable by the user and/or group that the Apache process is running as.

Also check to see if SELinux is enabled by checking the contents of /selinux/enforce. If it is, either disable it or make sure the proper labels are set on the path that you are writing to.

Loganiaceous answered 18/3, 2011 at 21:10 Comment(0)
A
0

Know that sometimes there is absolutely no way to get around this using PHP only.

The two solutions to this are:

  1. Configure PHP and Apache permissions manually (warning: can get dirty very quickly).
  2. Use FTP to change ownership to 0777 (full access) and then revert after running changes.

I've often found the latter option to work best.

Albumin answered 18/3, 2011 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.