I have a need to create files in a folder that is located in the web root on an apache server using mod_php rather than cgi. Scripts creating files are running under the apache user, typically "nobody", and do not have authority to write in folders unless permissions are set to 777. This is obviously not very secure, especially when these folders are in the web root.
The solution I came up with was to do this:
Create a temp folder outside the web root with permissions set to 777. Then in the script I will be using, have a function write the necessary file to that folder, and then use PHP's built in FTP functions to copy that file, with a connection using the site owner, to the appropriate folder inside the web root that has properly set, secure permissions. When that is completed then the function continues on to delete the originally generated file in the temp folder and return success or failure.
That seems like a fairly secure way to handle this situation and I've tested it out successfully, but I'm wondering if anyone can point out flaws, or provide an alternate solution.
Edit: One thing I just thought of to enhance security is I can telnet in as superuser and chown the temp directory to the apache user and then chmod it to 600. Then only the apache user has access and the only worry there would be from other users on teh server, but since the server is dedicated, it only has the one server user anyway which is the site owner.
The solution I came up with was to do this:
Create a temp folder outside the web root with permissions set to 777. Then in the script I will be using, have a function write the necessary file to that folder, and then use PHP's built in FTP functions to copy that file, with a connection using the site owner, to the appropriate folder inside the web root that has properly set, secure permissions. When that is completed then the function continues on to delete the originally generated file in the temp folder and return success or failure.
That seems like a fairly secure way to handle this situation and I've tested it out successfully, but I'm wondering if anyone can point out flaws, or provide an alternate solution.
Edit: One thing I just thought of to enhance security is I can telnet in as superuser and chown the temp directory to the apache user and then chmod it to 600. Then only the apache user has access and the only worry there would be from other users on teh server, but since the server is dedicated, it only has the one server user anyway which is the site owner.