0

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?

I'm running MAMP as my local testing server. I'm following the instructions I found here: http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920

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.

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 echo shell_exec('pwd'); I will see a path printed in the browser.

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.

I guess the question boils down to how can I get my PHP script to run the less compiler?

UPDATE! I solved the problem...

It turns out that the less command (path/path/lessc) 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.

There are a lot of ways to sidestep this, but I determined that editing the list of sudoers with sudo visudo was the best for my purposes. There was a lot of helpful tips on this post. Through trial and error, I figured out that PHP uses the www-data account. Adding this line fixed my problem:

www-data ALL=(ALL) NOPASSWD: /var/root/node/npm/node_modules/less/bin/lessc

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.

Community
  • 1
  • 1
emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • Hi Emerson, I am the author if the article you were referring to. I had similar problems when I was coming up with this solution, sorry I didn't notice the question before, glad you got it working though. – JD Isaacks Jun 19 '12 at 04:30

2 Answers2

0

It turns out that the less command (path/path/lessc) 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...

See my edits to the question above.

emersonthis
  • 32,822
  • 59
  • 210
  • 375
0

Maybe it would be easier if you'd use the PHP implementation of lesscss: lessphp

toabi
  • 3,986
  • 1
  • 24
  • 24
  • That's an interesting idea. I hadn't heard about it until now. My main hesitation is that I don't want my .less files to work if uploaded to the server accidentally. I want only .less files during development. Then only .css files to go live. If the lessphp is included in the project, there's a chance it could be overlooked and the .less files could go live undetected. – emersonthis Dec 10 '11 at 21:14