10

I'm trying to add /usr/local/texlive/2012/bin/x86_64-linux to the PATH environment for my Apache user (www-data) after installing TeX Live 2012 manually.

I edited my /etc/environment

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/texlive/2012/bin/x86_64-linux"

to include this path within my system-wide PATH environment variable for all users.

However, if I execute sudo -u www-data printenv PATH I'm only getting:

# sudo -u www-data printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I can't understand the source of this issue and appreciate any help.

DigNative
  • 343

2 Answers2

11

I had a similar problem where I needed a specific export for www-data to use when running PHP exec command and managed to cobble together this solution:

  • Edit /etc/apache2/envvars:

    sudo nano /etc/apache2/envvars
    
    • Add your export to the end of the file and save it.

      export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/texlive/2012/bin/x86_64-linux"
      
    • Restart Apache:

      sudo service apache2 restart
      

Now if you execute the following PHP

<?php
    $descriptorspec = array(
       0 => array("pipe", "r"),  // stdin
       1 => array("pipe", "w"),  // stdout
       2 => array("pipe", "w"),  // stderr
    );
    $process = proc_open('env', $descriptorspec, $pipes, dirname(__FILE__), null);
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    var_dump($stdout);
?>

You should see your environmental path as set in the file. It seems that apache spawns commands under www-data using the contents of that config file only and not from and of the bash.bashrc etc type files? I Can't say for certain because I'm new to Linux.

Not sure if this is exactly what you are trying to achieve but hope it helps.

Seth
  • 58,122
Del Hyman-Jones
  • 111
  • 1
  • 3
  • On Ubuntu 12.04 with Apache 2.2.22 /etc/init.d/apache2 has the following:
    ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
    if [ "$APACHE_CONFDIR" != /etc/apache2 ] ; then
     ENV="$ENV APACHE_CONFDIR=$APACHE_CONFDIR"
    fi
    if [ "$APACHE_ENVVARS" != "$APACHE_CONFDIR/envvars" ] ; then
     ENV="$ENV APACHE_ENVVARS=$APACHE_ENVVARS"
    fi
    
    

    This overrides anything in /etc/environment and suggests that /etc/apache2/envvars is the best place to add/edit vars required globally for apache. You can set environment vars locally within a VirtualHost config using SetEnv.

    – George Feb 03 '14 at 14:00
  • It's also worth noting that it looks like it's only LANG and PATH that are set in this way, so any other vars added to /etc/environment are included just fine. – George Feb 03 '14 at 14:01
  • This doesn't work for me. I add export PATH=$PATH:/var/www/owncloud (which works manually as www-data) but it has no effect after service restart. Also tried with out export.. – geotheory Dec 12 '16 at 01:12
  • @George is parcial right, forgetting that www-data user can be used also for running services, so editing trough apache won't work. Sometimes the user needs some specific vars, for e.g. to run dotnet – Leandro Bardelli Sep 18 '20 at 19:31
3

Your /etc/environment doesn't need the export statement in front of the key/value pairs. As stated in the wiki:

It is not a script file, but rather consists of assignment expressions, one per line.

See this other question on how the format works.

  • I know, I tried both variants, but none of them worked out. In my case it was sufficient to add the additional path to /etc/apache2/envvars. However, a sudo -u www-data printenv PATH still does not recognize the additional path using /etc/environmnent, so the original question is no solved. – DigNative Jan 13 '13 at 09:31