简体   繁体   English

我的MAMP测试服务器上的PHP脚本无法访问node_module:更少

[英]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. 我正在尝试将本地开发环境配置为读取.less文件,以便可以在开发过程中编辑.less文件,并且仅在上线时才转换为.css。 Make sense? 说得通?

I'm running MAMP as my local testing server. 我正在运行MAMP作为本地测试服务器。 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 我正在按照在这里找到的说明进行操作: 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. 简而言之,计划是使用htaccess文件捕获对.css文件的请求,并将其定向到PHP脚本,该脚本编译同名的.less文件并返回css代码。

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. 我可以从命令行编译一个.less文件,它吐出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. 我知道我的重写规则正在运行,因为我可以在浏览器中键入url并查看我的php脚本的输出。 For example, if my PHP script calls echo shell_exec('pwd'); 例如,如果我的PHP脚本调用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. 问题是,除非以root用户身份SSH到本地主机,否则我无法运行较少的脚本。 When I exit SSH and run the command I get "Permission denied". 当我退出SSH并运行命令时,我得到“权限被拒绝”。 I suspect this is what happens when my PHP script tries to call this... so it's returning nothing. 我怀疑这是我的PHP脚本尝试调用此操作时发生的情况...因此它什么也不返回。

I guess the question boils down to how can I get my PHP script to run the less compiler? 我想这个问题归结为我该如何让我的PHP脚本运行较少的编译器?

UPDATE! 更新! I solved the problem... 我解决了这个问题

It turns out that the less command ( path/path/lessc ) needed to be sudo'ed. 事实证明,需要对less命令( path/path/lessc )进行sudo'ed处理。 PHP wasn't doing this, so the shell_exec() command wasn't returning anything. PHP并未执行此操作,因此shell_exec()命令未返回任何内容。 That's why my echo statements DID work. 这就是为什么我的echo语句DID起作用的原因。

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. 有很多方法可以避免这种情况,但是我认为使用sudo visudo编辑sudoer列表是我的最佳选择。 There was a lot of helpful tips on this post . 这篇文章上有很多有用的技巧。 Through trial and error, I figured out that PHP uses the www-data account. 通过反复试验,我发现PHP使用了www-data帐户。 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(). 需要记住的一点是,您仍然必须将sudo添加到提供给shell_exec()的命令中。 Hope this is helpful to someone else. 希望这对其他人有帮助。

It turns out that the less command (path/path/lessc) needed to be sudo'ed. 原来,需要对less命令(path / path / lessc)进行sudo'ed。 PHP wasn't doing this, so the shell_exec() command wasn't returning anything. PHP并未执行此操作,因此shell_exec()命令未返回任何内容。 That's why my echo statements DID work... 这就是为什么我的echo语句DID起作用的原因...

See my edits to the question above. 请参阅我对以上问题的编辑。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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