Whenever I upload a new file to my server, I have to chown -R apache:apache /dir
in order for it to be accessible.
Is there an easier way around this?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityMost likely, what is actually needed for the files to be accessible is that their group is set to apache
. You can easily set it by default for new created files by running :
find /path/to/root/directory/of/website -type d -print0 | xargs -0 chmod g+s
This will set the setgid
flag on all sub-directories too. With this flag set, any new file created in there will inherit the group of its parent directory. (-print0
and -0
options are designed to handle correctly spaces in filenames)
Make sure ownerships of files are correct before running it. If unsure, you can fix it the same way :
find /path/to/root/directory/of/website -print0 | xargs -0 chown apache:apache
setgid
works fine (+1). If the ftp-program does set the group... well... then there is nothing else to do then change its configuration. I did just test it with VSFTPD and with that it works. (The group is not set to the one defined in the vsftpd.conf but takes it from its parent directory)
root
user) ? When displaying the file permissions (with ls -l
), you should see an S
instead of the x
for group permissions on directories.
Here is what you could do with VSFTPD:
You can set the following 2 options in you vsftpd.conf
:
guest_enable=YES
guest_username=apache
guest_enable
, if enabled, makes sure all files are uploaded as user/group apache
.
Please note that this will only work for files uploaded via FTP. If you copy a file in Linux itself to your www directory, this will not adjust the permissions.
That's why, if you're also accessing that directory via the filesystem, it is best to use the chmod g+s
-method Levans suggested.
(i.e.)
find /var/www/site/public_html -print0 | xargs -0 chown apache:apache
find /var/www/site/public_html -type d -print0 | xargs -0 chmod g+s
guest_enable
If enabled, all non-anonymous logins are classed as "guest" logins. A guest login is remapped to the user specified in the guest_username setting.Default: NO
guest_username
See the boolean setting guest_enable for a description of what constitutes a guest login. This setting is the real username which guest users are mapped to.Default: ftp
apache:apache
because your webserver needs access. (Even if user apache has no password/ftp access the files can still be written with that user as long as vsftpd assigns it to that user. That's where these lines are for)
guest_enabled
is a bit confusing because it has nothing to do with 'guest'-access but makes vsftpd write a different user (remap) for 'normal' users.
apache:apache
then yes, you need to do this, be it manually or via some sort of FTP client/server configapache
(if you've got a password for that). Otherwise you need to change the config of your ftp-server. Is this your own server? And what server-daemon are you running? VSFTPD or something else?