dongxie3352 2011-12-10 19:35
浏览 55
已采纳

我的MAMP测试服务器上的PHP脚本无法访问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.

  • 写回答

2条回答 默认 最新

  • duancifu6769 2012-06-04 22:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题