53

I think for a development machine, its more convenient to run Apache as the current user to simplify permissions problem? How do I do that? I think its suexec but how do I configure it in Ubuntu+Apache?

8 Answers 8

68

Edit the following file as root: /etc/apache2/envvars

using the command:

sudo nano /etc/apache2/envvars

change the user and group to yourself if there is only one user and you will never have permissions problems again.

I.E., if you are only logging in and running the server as user 'big_dog':

export APACHE_RUN_USER=big_dog
export APACHE_RUN_GROUP=big_dog

Heck, for that matter you could change that user to the current user I'm sure somehow. Then, install user_dir you all have webs only you can have full control of (unless you modify this).

Restart the server (if unsure, just reboot or goole) and you are good to go.

5
  • 3
    This works as a charm! thank you. the only I would add: sudo chown big_dog.big_dog /var/lock/apache2
    – Kostanos
    Sep 11, 2013 at 17:14
  • 3
    Also probably you will need sudo chown big_dog.big_dog -R /var/log/apache2
    – Kostanos
    Dec 18, 2013 at 13:14
  • On OpenSUSE-Tumbleweed (and probably on some other) the file is at /etc/apache2/uid.conf
    – Dilawar
    Nov 7, 2016 at 11:34
  • 1
    You don't have to necessarily change the envvars file, I just updated the environment variables before starting Apache with a script. This way, you are not locked to a single user but who ever runs the script, runs the apache. I used export APACHE_RUN_USER=#$(id -u) to give the user id and similarly use id -g for group in the script and at the end I called apache to run. Apr 12, 2019 at 16:36
  • But now I get Warning: SessionHandler::read(): open(/var/lib/php/sessions/sess_ft772juh1blqnqjvhli7tkem3l, O_RDWR) failed: Permission denied EDIT: I solved it, by deleting the file /var/lib/php/sessions/sess_ft772juh1blqnqjvhli7tkem3l
    – Black
    Mar 3, 2021 at 13:52
30

I myself would add the user to the www-data group with...

sudo adduser {username} www-data

Simple and effective. No messing with config files or permissions.

7
  • 11
    I did it all the time, but this will not fix some of permission denied issues, as the group rights are not the same as user's rights for all files.
    – Kostanos
    Sep 11, 2013 at 17:10
  • 3
    This will not fix if the CHMOD didn't allow group to write.
    – GusDeCooL
    Nov 13, 2013 at 19:14
  • the issue for me is that I have local installation. When I create files with my IDE they have my 'user/group' and sometime apache is not able to read those files Feb 7, 2014 at 9:45
  • 1
    this alone does not solve a lot of permissions problems because your primary group is normally not www-data and some php apps require group write permissions. (joomla, wordpress)
    – Eric
    Aug 23, 2017 at 4:05
  • 2
    This doesn't solve the problem where you create files and www-data can't remove/edit them because your main group isn't www-data
    – billy
    Oct 19, 2017 at 12:46
2

You can do this with the mpm_itk_module module.

Install on Ubuntu 20.04:

sudo apt install libapache2-mpm-itk

Create a Virtual Host for sites in your home folder

<VirtualHost codealfa:*>
    ServerName codealfa
    ServerAlias codealfa
    DocumentRoot "/home/codealfa/www"

    <IfModule mpm_itk_module>
        AssignUserId codealfa codealfa
    </IfModule>

    <Directory /home/codealfa/www>
        Options Indexes FollowSymLinks Includes ExecCGI
        DirectoryIndex index.html index.php
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Add your domain (in this case I'm using the username as domain) to /etc/hosts

127.0.0.1    codealfa

Restart apache

sudo systemctl restart apache2

Install all your sites under subfolders under /home/codealfa/www and access them in your browser from http://codealfa/. For eg., install one site at /home/codealfa/www/site1 and you can view it in your browser at http://codealfa/site1/.

1

why would you do this,, I was strugnling with that perm issues in the past, but this is my dev process from today:

  • install new linux box (virtual, or local like laptop)
  • run standard sudo apt-get install lamp-server^ proc to get lamp up
  • make sym link to my home dir where projects are like this:

    ln -s /home/user/html /var/www/html

  • make sym link to hosts config file

    ln -s /home/user/html/Apache-VirtualHosts.conf /etc/apache/sites-enabled/000-default

that's it :)

It works as aspected, no issues with user perms or something similar with apache server,, for mysql I have little longer process..

hth, cheers

2
  • That doesn't address any of the permission issues addressed in most of the other answers.
    – RichieHH
    Sep 3, 2018 at 12:45
  • That is another way that avoids some of the permissions problems for new users... also enabling user directories avoids some of that- which everyone seems to forget these days. ;)
    – Eric
    Dec 3, 2020 at 2:57
1

If anyone trying to achieve this on a docker container, This worked for me.

create your own user (non-root user hence the id 1000) and give permissions:

ENV MY_USER myUserName
RUN useradd -M -u 1000 $MY_USER \
    && chown -R $MY_USER:$MY_USER /var/run/apache2 \
    && chown -R $MY_USER:$MY_USER /var/log \
    && chown -R $MY_USER:$MY_USER /etc/apache2 \
    && chown -R myuser:myuser /var/lib/apache2
USER $MY_USER
CMD apache2ctl -D FOREGROUND

Note: Add this line only after installing all the apache modules you needed.

1
  • chown -R $MY_USER:$MY_USER ... can be written as chown -R $MY_USER: ...
    – Piterden
    Jun 27, 2023 at 13:25
0

Sidestepping the "should you do this" issue, you can find where the user for apache is set by running:

grep www- /etc/apache2/apache2.conf

It's then a case of editing those instances in /etc/apache2/apache2.conf and reloading Apache (sudo /etc/init.d/apache2 reload).

3
  • apache2.conf doesn't contain www- for me
    – Jiew Meng
    Jan 23, 2012 at 14:15
  • I have a feeling you meant something like grep -HR "www-" /etc/apache2/. If you already know it's in apache2.conf then just edit that file. In my case, it wasn't there, but in /etc/apache2/envvars as Eric's answer points out.
    – Mike
    Nov 5, 2014 at 18:21
  • That's because apache.conf looks for ${APACHE_RUN_USER} and ${APACHE_RUN_GROUP}. Not www-data directly. Aug 9, 2016 at 12:06
0

create a new usergroup webdev and add the www-data user to it.

sudo addgroup webdev
sudo adduser www-data webdev

Then go ahead and add your user to that group and make it the default group for yor user.

sudo adduser {your-user} webdev
sudo addgroup webdev

Last thing to do is to make sure, the group webdev has rwx rights on the files

sudo chmod 775 -R /path/to/project

Now the only thing that can occur is that for e.g. www-data creates a new log file and you can't rwx it.

2
  • 1
    What is the advantage of this, over just adding my own user to the www-data group, specially since we are talking about a development environment?
    – Dan
    Apr 24, 2015 at 10:19
  • well if you do it this way, and set the 775 rights to e.g public_html or /var/www you won't have issues with the rights on your dev machine. the other aproaches, leave you with additional steps to do or aren't as effective as i wan't them. So in my opinion, this is the best way to deal with it on a dev machine. for productive servers i would recomend a different setup. also with this aproach, you can add users easily. Apr 24, 2015 at 10:41
0

For those who just want to spawn a web-server for local development

You can start PHP's in-built server

php -S localhost:8080 -t /home/sysadmin/Documents/wordpress

You must log in to answer this question.

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