1

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?

3 Answers 3

2

Apache 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

http://www.toptip.ca/2010/03/linux-setgid-on-directory.html

2
  • Ok. I tried your approach. I got an error with line 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. Commented Dec 5, 2015 at 18:12
  • My fault nobody is because I use NFS, I am taking it out. Commented Dec 5, 2015 at 18:13
1

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>
2
  • Well, I just found another 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 Commented Dec 5, 2015 at 17:37
  • That's pretty much doing the same thing. That will run Apache as your username, making it the default for everything that doesn't have an explicit 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.
    – Sly
    Commented Dec 5, 2015 at 17:46
-1

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
1
  • 3
    And then if they compromise PHP/Apache they have access to all your files. Apache runs under a different user for a reason. Commented Dec 5, 2015 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .