Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know file permissions have been covered on here before, but im struggling to get my head around the concept for my scenario.

  • I created the files on an old ubuntu installation.
  • Ive copied the files into my new ubuntu installation and put them in my webroot.
  • When i attempt to run the files (theyre PHP files) i get an error relating to permissions

in an attempt to fix this, i assumed that they must still be owned by the previous owner, so i ran chown -R on the directory, with my username as an argument, in order to take ownership of all of the files in the directory. It should be noted that the usernames between new and old ubuntu installations were the same.

When i attempt to run the files again, same problem: 500 error due to permissions problems. Can anyone tell me what other steps i should take?

The webroot for my apache installation is inside my home folder. If i create new files in my webroot, they also work as expected, its only the old files that are causing the problem.

share|improve this question
    
Ok, i have solved my problem of the files not executing, but i did it by simply running chmod -R 777 dir on the directory in question. I cant help thinking theres a better way of doing it though –  richzilla Feb 17 '11 at 18:47
3  
777 permissions in a file will allow everybody to write your files which may result in damage to your pages or hacking. You should try with 755 which is normally used for php files. Make sure you don't require to enable the "allow execution" for a file, in which case you can run chmod [filename] -x. Information about the chmod command can be reached by clicking the next link: catcode.com/teachmod/chmod_cmd.html –  Geppettvs D'Constanzo Feb 17 '11 at 19:38
    
What does the Apache error log show? –  Kees Cook Feb 18 '11 at 6:44
add comment

3 Answers

up vote 5 down vote accepted

The directories above your webroot should have the execute bit set to allow Apache descend into the directories.

If you have your webroot located at /home/user/htdocs, the /, /home, /home/user and /home/user/htdocs should have the execute bit set.


The above solution "works", but it's not ideal. If you've created a folder, Apache cannot write to it. The reverse happens too.

This can be "fixed" by setting umask 0007 and adding yourself to the Apache group (www-data if I'm not mistaken), so that newly created files and folders are writeable by the group.

Alternatively, you can install an alternative Apache MPM: Apache2 MPM ITK (info on configuring) and adjust the configuration so Apache runs under your user.

share|improve this answer
    
For explicit instructions on how to accmplish this see Marco's answer below –  hobs Aug 16 '12 at 21:46
2  
@hobs Even better, askubuntu.com/a/46371/6969 –  Lekensteyn Aug 16 '12 at 22:04
add comment

If your server documents are in /home/$USER/public_html directory you need to run

sudo chown -R www-data:www-data /home/$USER/public_html

to give ownership of the DocumentRoot folder to the user www-data and group www-data.

Then you can add yourself to the group www-data

sudo adduser $USER www-data

Finally, you need to make the DocumentRoot folder writable by owner (www-data user) and to your self (as part of the www-data group):

sudo chmod -R 775 /home/$USER/public_html

For convenience you can make script named public_html_fix.sh with content:

#!/bin/bash

sudo adduser $USER www-data
sudo chown -R www-data:www-data /home/$USER/public_html
sudo chmod -R 775 /home/$USER/public_html

Save it inside /home/$USER/bin and make it executable using:

sudo chmod +x /home/$USER/bin/public_html_fix.sh

Then you call it whenever you need, from wherever on the file system you happen to find yourself like this:

public_html_fix.sh
share|improve this answer
    
worked for me.... –  Okeke Emmanuel Oluchukwu Oct 8 '12 at 13:48
add comment

I'm not sure that file permissions are copied when the file is, can someone confirm that?

Otherwise, would it be possible to give the files to some other user (or root), and then reclaim them?

share|improve this answer
1  
If you use cp -a, permissions are retained. –  Scott Severance May 13 '11 at 16:35
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.