Add path to PATH environment variable for www-data

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.


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.