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.

I am working on a PHP script that transfers files using FTP functions. It has always worked on my production server (which is a hosting service). The development server I have just setup (I am a novice to servers) is Debian Lenny with Apache2, PHP5, and MySQL5.

The file transfer works correctly, but once the file has been written to the server, it has permissions of 600. This makes it impossible for me to view the file (JPEG) in the web browser, as permission is denied. I have scoured the internet and even broken my server installation and reinstalled it trying to figure this out (which has been fun, nonetheless!).

I know it is unwise to set 777 permissions on public accessible files, but even that will not solve the problem. The only thing that works is if I chmod 777 thefile.jpg after it has been transferred, which is not a working solution.

I tried changing the owner of my site files to www-data per this post, but that also does not work.

My user is mike, and it still does not work whether the owner of the files is mike or root.

Would somebody point me in the right direction? Thanks! And, of course, let me know if I can clarify anything.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

this is more related to your ftp server configuration, and not so much to apache. which server are you using for ftp? i believe vsftpd is the default ftp server for Debian Lenny, the options you might need to look at in your vsftpd.conf file are: chown_upload_mode and file_open_mode

the default for chown_upload_mode is 0600 which makes me think that might be what you need to set, and then restart your FTPd and try again.

share|improve this answer
    
Ahhh that makes sense! Yes, I installed vsftpd so that I could transfer files from my workstation to the server. I'm looking at it now... –  Mike Moore May 18 '10 at 0:53
    
Do you think it would be a good idea to try and use the same FTP application that my hosting service uses? –  Mike Moore May 18 '10 at 0:56
    
you will want to set chown_upload_mode and file_open_mode to 644 which allows read/write to the owner, read to group, and read to world. permissions work like this: 1 = execute, 2 = write, 4 = read. 7 = read/write/execute, and so on. –  cpbills May 18 '10 at 0:57
    
i don't know, but whatever you use to send files to your web server, you'll need to look at the umask and permissions options in the configuration. also, most FTP clients will allow you to set permissions on files you have uploaded, so check the settings of your ftp client as well. –  cpbills May 18 '10 at 0:59
    
I ended up having internet connection difficulties with Debian and finally reinstalled Ubuntu 10.04 Server Edition. The internet problem was resolved. I started everything over and ended up only uncommenting write_enable=YES and local_umask=022 in /etc/vsftpd.conf. This allows my PHP FTP script to transfer files with permissions of 0644. This allows my other scripts to actually be able to read the images for displaying them in the browser. Thank-you for pointing me towards vsftpd.conf, as that was the solution. –  Mike Moore May 18 '10 at 13:29
add comment

It sounds like Apache is not configured to serve files out of the directory that the file is being saved to. You need to ensure there is a Directory directive in you Apache configuration that permits access to the directory that the file is in. For example:

<Directory "/home/mike/uploads">
  Order allow,deny
  Allow from all
</Directory>

Also the www-data user needs to have permission to traverse the directory tree leading to the directory. E.g. if the file is at /home/mike/uploads then the home, mike and uploads must have execute permisson allowed for other users.

share|improve this answer
    
I added the Directory directive that you shared to apache2.conf. Then I set the permissions for each of these directories: /var/www/test/images to 755. But the problem still exists. Did I add the correct directory directive? –  Mike Moore May 17 '10 at 23:20
    
this did not work for my problem, but thanks for your help. I am still so new to this that this could very well be another solution for my problem and I don't even know it! If anything, it helped me dive a little deeper into Apache. –  Mike Moore May 18 '10 at 15:37
add comment

I seem to have solved the problem. I would still like to know if this is an okay solution or not.

Apache User and Group

I changed User ${APACHE_RUN_USER} to User mike and Group ${APACHE_RUN_GROUP} to Group www-data, then I restarted apache with /etc/init.d/apache2 restart.

PHP5 Owner

Next, I changed owner from root to mike on /var/lib/php5 by:

chown -R mike /var/lib/php5

Owner, Group, and Permissions for public folder

Following this article, I did the following:

Added owner 'mike' to the www-data group:

sudo usermod -a -G www-data mike

Changed the group for /var/www to www-data:

sudo chgrp -R www-data /var/www

Then, I changed permissions for /var/www:

sudo chmod -R 2750 /var/www

And, I did the following to the upload directory

sudo chmod -R 2770 /var/www/test/images

Tried this...

I originally added @Wes suggestion (below) to apache2.conf, but removed it after doing the above, which works. (Adding or removing this did not seem to affect anything):

<Directory "/var/www">
  Order allow,deny
  Allow from all
</Directory>

But...

The JPEG image files, after being transfered to the server via the PHP script, still have permissions of -rw-------, but I am able to view them from the web browser. Any thoughts?

share|improve this answer
    
it might work, but it is highly inadvisable. running a system service as a trusted user mike is not a very good idea, and changing ownership, recursively no less, of a system directory to mike is also not a great idea. you could look into implementing suphp or a similar product suphp.org/Home.html, suphp allows php scripts to run as a user, in this case mike allowing you to do what you want. sudo chmod -R 2750 /var/www makes me wish i was there to stop you... for what you want, files should be 644 –  cpbills May 18 '10 at 0:55
    
and i'm not saying sudo chmod -R 2644 /var/www would have been a solution either, a recursive chmod like that is just... disaster waiting to happen. –  cpbills May 18 '10 at 0:56
    
@cpbills I'm cleaning up some of the stuff I did. Any comments about how I changed User ${APACHE_RUN_USER} and Group ${APACHE_RUN_GROUP} ? –  Mike Moore May 18 '10 at 1:16
    
you would change that in your apache configuration, and i would keep it as www-data and www-data –  cpbills May 18 '10 at 1:17
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.