Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
<?PHP

print "hello";

?>

I write this code and save as "1.php";

Then I upload this PHP script to my server.

I have 8 diffrent free hosting server's accounts.

And I noticed that there are 2 types of server settings.

(1)
"type A"
for exapmle, sqweebs.

We need to set the PHP file permission as 640.
This means that sqweebs server requires us
to give group permission for PHP script running.

If I set 604,then the server generate such a errors.like,

Warning: Unknown: 
failed to open stream:
 Permission denied in Unknown on line 0

Fatal error: Unknown: 
Failed opening required 
'/www/sqweebs.com/1.php' 
(include_path='.:/blahblah') 
in Unknown on line 0


(2)
On the other hand ,there are other type
servers on this world.

"Type B",
for example, izfree.

On this server, I found that
I can make PHP script work if I give it
604 as the permission.


So I want to know why there are many server settings,
and what is the reason, and some other related
opinions.

like which server should I use ,or etc,etc.

share|improve this question
add comment

2 Answers 2

In order to execute your PHP script the web server needs to be able to read the script. Generally, your web server (usually Apache) runs as a non-root user for security reasons.

So, to resolve your issue, we first need to figure out which user your web server runs as. This differs from one Linux distribution to the next, but if you open your httpd.conf (or apache2.conf file on Debian) and look for the line beginning with "User" you'll see the username. On Debian you'll see "User www-data" or on RH derived distros it's usually "User apache".

Now you need to construct your permissions such that this user has "read" access to your php scripts.

The simplest way is to set permissions to 644, which will ensure the user web server has access via the "world" permission.

Another option is to change the owner of your script to your www-data or apache user (as determined above), and set permissions to 400. This renders the script inaccessibly to all users except the web server.

A final option which is especially useful if you need to provide FTP access is to change the group owner to either www-data or apache, and set the permissions to 640. This gives the user owner full read write access to the script and Apache the read access it needs while locking everyone else out.

In your examples above, using 604 will prevent PHP from opening the files on the sqweebs server if the apache user had group ownership over the script. Likewise 640 on your izfree server would fail if the Apache user has neither user or group ownership of the files. I'd guess in both circumstances 644 would work, but it's hard to tell without also knowing what users/groups own the scripts.

share|improve this answer
    
thanks,but loooooooong –  aaa Jun 16 '09 at 12:57
    
thank you for writing but, it is too difficult for me to understand. –  aaa Jun 16 '09 at 18:40
    
Ok, short version. It's hard to know what's causing your problem without also knowing the user/group owner of the files, but 644 will probably work in both cases. –  Jim OHalloran Jun 16 '09 at 23:26
add comment

it is expected, by the webserver, that the file has to have the permission for the user that the webserver runs as, to open and run it.

So, if the webserver (say Apache) runs as www, then www should have read access to the file. (some run apache as www, and some run it as apache, or nobody).

When you upload the file, depending on how the umask is set, the file permission is set so. (so, on one host, the file could have permission 655 or other could be 600, when permission is not set explicitly).

It always helps if you know a bit about the OS you normally deploy your applications on. Mostly, PHP is deployed on *nix system, and permission scheme is nearly (almost always) same across all the *nix systems.

Try getting hold of "Unix system Administration Handbook" (by Evi Nemeth & Co). Its quite fun to read and easy to understand (it is an old edition .. but unix permissions have not changed)

Also, make sure your server can handle DOS end of line settings (are you developing code on windows ?). Make sure, when you save, you save it as "Unix format" EOL (end of line). (most editors like Dreamweaver provide the feature). Most of the hosting (PHP) providers run some form of *nix (freeBSD, Linux etc), and webservers on these cause issue with the Windows EOL format

share|improve this answer
    
umask? . –  aaa Jun 16 '09 at 12:54
    
Yes. Checkout the book that I mentioned (or any unix book would cover that). You dont really have to know, as a developer. But, it really helps if you know it. –  Ram Prasad Jun 16 '09 at 14:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.