简体   繁体   中英

PHP script on my MAMP testing server can't access node_module: less

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.

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.

如果您使用lesscss的PHP实现,也许会更容易: lessphp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM