Ftp – How to revoke ownership of a directory from apache service account after an error

ftppermissions

Basically, I was trying to fix an issue where I couldn't upload images to my newly migrated WordPress site. The site is on a VPN and I found this article, http://2surge.com/how-to-fix-the-uploaded-file-could-not-be-moved-to-wp-content-error-message/, which gave the following solution:
Execute the following command as root user to give Apache user ownership of WordPress files:

chown -R nobody /home/<username>/public_html

But I think I should have given ownership to the WP directories within my main directory. Instead, I gave the Apache user ownership of my public_html folder. And now I can't access public_html via ftp. I get the error: Failed to Return Directory Listing.

Can I simply change the owner back to the original system user using the same command like so:

chown -R originaluser /home/<username>/public_html

Or is that oversimplifying a concept I have a very rudimentary and patchy grasp of?

Best Answer

Yes, you could change the user back, no problem. However, this probably will block the uploading of images again.

I guess that your problem is that you have 2 services - Apache and FTP, running under different users (e.g. apache running from user nobody).

In this case I would advise you that you should create a user group and give a permissions to that user group on the folder you need (I suggest that it should be /wp-content/uploads/).

For example:

# create new group:
groupadd webservices
# add apache user (nobody) to this new group
usermod -a -G webservices nobody
# add ftp user (?) to this new group
usermod -a -G webservices <ftp user>
# add yourself to this group (in case you need to access the folder from non-root account)
usermod -a -G webservices <myself>

Now you need to set a user and group for this folder and give correct group permissions to that folder (recursively).

For example:

# change the folder ownership
chown -R nobody:webservices /path/to/wp-content/uploads/
# and set group permissions to be the same with user permissions:
chmod -R g=u /path/to/wp-content/uploads/

Now both apache and ftp users will have the same rights for this folder and its contents, since both users are in the webservices group (and the webservices group has correct permissions, which are copied user permissions).

See also:

What permissions should my website files/folders have on a Linux webserver?

https://serverfault.com/a/284478/118677

man chown

man chgrp

man chmod

Related Topic