For some reason when I upload to my website, Apache saves the file as user www-data
and group www-data
, rather than user myusername
and group www-data
.
How do I fix this?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityApache does saves files as www-data, because it is the user it runs with for security reasons.
What you can do is create a specific user and group for your site, and use mod-ruid2 to configure your vhost to use that user.
For installing it:
sudo apt-get install libapache2-mod-ruid2
And for configuring it in the vhost:
<Directory "/var/www/mydir">
RMode config
RUidGid my_new_user my_new_group
Each vhost can also have different users, which in multi vhost servers can be convenient, either for multi-host configurations with multiple users, or for knowing whether vhost is sending spam, for instance.
https://www.jamroom.net/brian/documentation/guides/1202/configuring-apache-with-mod-ruid2
As for the ownership of files, read on.
In the directory of your vhost, you also configure it with setuid or SetGid directory, for any file that you leave there as your normal user that also belongs to that group, to be forced to that group or user and not your normal group.
For forcing user ownership, follow the next steps:
chown my_new_user.my_new_group /var/www/mydir
setUID for directory
chmod u+s /var/www/mydir
setGID for directory
chmod u+g /var/www/mydir
RGroups nobody
, though. When I removed that line it worked fine. Was I supposed to replace "nobody" with something specific to my installation? Either way... glad to know my itty bitty personal blog is super secure now.
Dec 5, 2015 at 18:12
You should be able to just specify a different User
in your configuration, wherever you want to use it at.
Example:
<VirtualHost 12.34.56.78>
User myusername
</VirtualHost>
sudo nano /etc/apache2/envvars
Edit the following line: export APACHE_RUN_USER=myusername
Dec 5, 2015 at 17:37
User
set. Setting it in the configuration file allows you to limit it to a specific host, etc. I, personally, would prefer to do it in the configuration so that I wouldn't forget that I have Apache, or a specific host under Apache, running as a different user.
Well, I just found a solution. Not sure which is best, but here's what I did and it seems to be working fine:
sudo nano /etc/apache2/envvars
Edit the following line:
export APACHE_RUN_USER=myusername