title: PHP script on my MAMP testing server can't access node_module: less
body: <p>I'm trying to configure my local development environment to read .less files so that I can edit .less files during development and only convert to .css when it's time to go live. Make sense?</p>
  
  <p>I'm running MAMP as my local testing server. I'm following the instructions I found here:
  <a href="http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920" rel="nofollow noreferrer">http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920</a></p>
  
  <p>In short, the plan is to use an htaccess file catch requests to .css files and direct them to a PHP script which compiles the .less file of same name and returns the css code.</p>
  
  <p>Everything seems to be working from the command line. I can compile a .less file from the command line and it spits out the css. I know my rewrite rule is working because I can type the url into a browser and see the output of my php script. For example, if my PHP script calls <code>echo shell_exec('pwd');</code> I will see a path printed in the browser.</p>
  
  <p>THE PROBLEM is that I can't get the less script to run unless I SSH to the localhost as root. When I exit SSH and run the command I get "Permission denied". I suspect this is what happens when my PHP script tries to call this... so it's returning nothing.</p>
  
  <p>I guess the question boils down to how can I get my PHP script to run the less compiler?</p>
  
  <h1><strong>UPDATE! I solved the problem...</strong></h1>
  
  <p>It turns out that the less command (<code>path/path/lessc</code>) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work. </p>
  
  <p>There are a lot of ways to sidestep this, but I determined that editing the list of sudoers with <code>sudo visudo</code> was the best for my purposes. There was a lot of helpful tips on <a href="https://stackoverflow.com/questions/3166123/how-to-call-shell-script-from-php-that-requires-sudo">this post</a>. Through trial and error, I figured out that PHP uses the www-data account. Adding this line fixed my problem:</p>
  
  <pre><code>www-data ALL=(ALL) NOPASSWD: /var/root/node/npm/node_modules/less/bin/lessc
  </code></pre>
  
  <p>Something to remember is that you STILL have to add sudo to the command that gets fed to shell_exec(). Hope this is helpful to someone else.</p>