What permissions for PHP scripts/directories? What permissions for PHP scripts/directories? sql sql

What permissions for PHP scripts/directories?


Directories must have execute permissions to be usable. Usually this is 0755. PHP scripts run via mod_php are not executed but rather read; 0644 will suffice for this. Directories that must be written to need to be owned by the user the web server is running as. There may be additional concerns regarding permissions, e.g. SELinux, but the above will get you through the basics.

Documents that must not be accessed by other users or external clients should be 0600, owned by the web server user, and located outside the DocumentRoot. Note that running mod_php in Safe Mode will prevent scripts from ever including anything outside the DocumentRoot; a lamentable flaw.


Set php files to 640

For maximum security you should set minimum permissions, which is 640.

  • The owner 6 would be the one uploading the files.
  • The group 4 would be the one serving the file. Make apache a group member.
  • The nobody 0 means no other users can read this file. It's important since php scripts sometimes have passwords and other sensitive data.

Never allow php scripts to be read by everyone.

Useful commands:

chmod 640 file.phpchown user:group file.phpusermod -a -G group apache

What these commands are doing:

  1. Change ownership of file.php so user can read and write, group read.
  2. Change ownership of file.php, to chosen user name and group name.
  3. Add apache to the group, so that apache can serve the file. Otherwise 640 will not work.


1) Files that end with a .php extension are handed off to the PHP compiler by Apache. If the proper configuration is not set up to do so, PHP files get served up as text files by the server. The Apache configuration line "AddHandler php5-script php" in the httpd.conf file is the PHP5 method of setting this up.

2) register.php needs to be accessible at http://www.example.com/php/register.php, as the java app is looking for it, so in the Apache htdocs folder, there needs to be a "php" folder with the register.php file in it.

3) PHP files need read access by the user that's running the Apache service. Using PHP as an Apache module has no 'service' to speak of that's separate for PHP. Instead the Apache service, when it gets a request for a PHP file, makes a shell call to the PHP binary to parse the file and hand the Apache service the result, which it serves to the client. Only if you were using PHP from the command line (CLI setup) would the scripts need execute permission, and start with a #!/path/to/php-bin line.

4) The requested file (register.php) needs to be in htdocs in order to be served by Apache. If PHP is running with "Safe Mode" disabled, register.php could include a file that was outside the htdocs folder.

5) The path "../inc/db_login.php" is relative to the PHP script that was originally fetched (register.php), so, since register.php is in htdocs/php/register.php, that would put db_login.php at htdocs/inc/db_login.php.