Ubuntu – Add path to PATH environment variable for www-data

environment-variablespaths

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.

Best Answer

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.

Related Question