Making file in user's homedir accessible from web/webserver

5,473

The symbolic link will always have these permissions: lrwxrwxrwx. The "l" indicates that it is a link. Therefor, all users can follow the link because the effective permissions are those of the file to which the link points. It is never useful to try changing the permissions of a symbolic link.

The whole path from the root to the file in question must be readable by the Apache user. So... take a look the permissions of the home folder:

ls -la /

The home directory should be owned by root, group owned by root, with 0755 permission mask by default: drwxr-xr-x. If not, change it:

sudo chown root:root /home; sudo chmod 0755 /home

Now take a look at your user's directory:

ls -la /home

If the username in question is sambo, the user's home directory should be owned by sambo, group owned by sambo, with 0755 permission mask by default: drwxr-xr-x. If not, change it:

sudo chown sambo:sambo /home/sambo; sudo chmod 0755 /home/sambo

Repeat this as many times as needed, for however many directories exist until you reach the directory that contains the file. Then, don't forget about the file itself.

sudo chown sambo:sambo /home/sambo/path/to/file/file.txt; sudo chmod 0644 /home/sambo/path/to/file/file.txt

Then use PHP to open it:

<?php
    $file = '/home/sambo/path/to/file/file.txt';
    $fp = fopen($file, 'r');

You could use the symbolic link at this point, too.

Share:
5,473

Related videos on Youtube

Sourabh
Author by

Sourabh

senior software and android developer

Updated on September 17, 2022

Comments

  • Sourabh
    Sourabh over 1 year

    I have a txt-file one of my user's homedir which is regularly updated there by a script. I now want to be able to access (read) this file from the web.

    /home/user/folder/file.txt
    

    So what I tried now is to log in as root, go into my webservers httpdocs folder

    /var/www/path/to/domain/httpdocs
    

    and there I tried to create a symbolic link with

    ln -s /home/user/foler/file.txt /var/www/path/to/domain/httpdocs/file.txt
    

    But this didn't work... I already tried changing the chmod of the symlink (which changes the ones from the original file of course) and also a chown to the user from webserver, but no matter what I tried I cannot open the file from the web or from a php-script (which is what I want to do)

    Can anybody help me and tell me what I need to do? What rights do I need to give? Or is there another way of achieving this?

  • Sourabh
    Sourabh about 13 years
    Well I've tried that... I set the whole path from home to the directory of the file to chmod 0755 and the file itself too. But still the script cannot read the file nor can I directly open it yet...
  • Javier J. Salmeron Garcia
    Javier J. Salmeron Garcia about 13 years
    Are you running PHP in safe_mode? (You would have to to chown your file to www-data. 'chown www-data file.txt'). Any errors in Apache log file?
  • Sourabh
    Sourabh about 13 years
    I went the other way now which worked for me... I had teh script write it directly into the /var/www/... path, but I needed your chmod changes here to have it writable there... thank you!