Laravel Process can't find Node - php

I am developing a Laravel (8.83.24) app and testing with Laravel's built in server "php artisan serve". I am using Process and am trying to debug a memory allocation error which has lead me to here:
$process = new Process(['node', '-v']);
$process->run();
print_r($process);
That leads me to a line in the output:
[command] => cmd /V:ON /E:ON /D /C (node -v) 1>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.out" 2>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.err"
and sf_proc_01.err contains:
'node' is not recognized as an internal or external command,
operable program or batch file.
Clearly node can't be found. I am on Windows 10. Node is set in my in System and User PATH. Running "node -v" from cmd.exe works and returns the version number. So Laravel's server doesn't seem to be able to find node. However, I can't check the path as running this:
$process = new Process(['echo', '%PATH%']);
$process->run();
print_r($process);
Just leads to an output of
"%PATH%"
How can I make Node findable by Laravel / Process?
Many thanks for your help.

I eventually solved this by setting the path for node in my Laravel .env file:
NODE_PATH='C:\PROGRA~1\nodejs\node.exe'
NPM_PATH='D:\Work\Project\node_modules'

Related

PHP fails to access the docker socket via curl

My Code fails to reach the Docker Socket
$client = new GuzzleHttp\Client();
$test = $client->request('GET','http://v1.40/containers/json',[
'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
]);
I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.
So just in case someone stumbles upon this in the future and has the same or a similar problem.
Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)
user = root
group = root
you will have to append the -R flag to the run command to allow running the workers as root.

How to run node module in php and display output?

I have a use case where i need to run a node module in php script and display it's output back on the page. Here's the steps I followed:
I have installed the module npm install -g md2gslides
I am using PHP's exec() for running the module
exec('node_modules/md2gslides/bin//md2gslides.js --version 2>&1', $output);
But it's not working. The error is below:
However, it runs in ubuntu's terminal without issues.
What is wrong here?
The error not because of php but because of the path.join() in line 33 of md2gslides.js
const STORED_CREDENTIALS_PATH = path.join(USER_HOME, '.md2googleslides', 'credentials.json');
It tries to find the credentials.json in users home directory. But I think it's unable to.
So I moved the file to node_modules/md2gslides/bin and modified path.join() as path.join(__dirname, '.md2googleslides', 'credentials.json')
This solved the issue.

Run mjml cli using symfony process

I've installed mjml cli using the following command (as described in the mjml documentation):
npm install mjml --save
now if i did node_modules/.bin/mjml in the command line it will run successfully.
the problem is when i use the symfony process component in php i got the following error (even if it's the right path):
The command "/Users/qoraiche/Documents/my-app/node_modules/.bin/mjml" failed. Exit Code: 127(Command not found) Working directory: /Users/qoraicheOS/Documents/my-app/public Output: ================ Error Output: ================ env: node: No such file or directory
Symfony process code:
$process = new Process(base_path('node_modules/.bin/mjml'));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
By the way i have installed mjml globally as well and try it with no luck.
Try providing the full path to node.js when calling the MJML binary from a Symfony Process().
$process = new Process('/your/path/to/node ' . base_path('node_modules/.bin/mjml'));
I use this method in my own apps. Of course, you will need to provide the input and output files to actually transpile anything.

how to solve error QXcbConnection: Could not connect to display when using exec function for phantomJs using PHP

I am working on a project where I want to use PHP and Phantomjs together, I have completed my phantomJs script and trying to run it using php exec function. but the function is returning an array of error list.
below I am writing my code of phantomjs and php
dir: /var/www/html/phantom/index.js
var page = require('webpage').create();
var fs = require('fs');
page.open('http://insttaorder.com/', function(status) {
// Get all links to CSS and JS on the page
var links = page.evaluate(function() {
var urls = [];
$("[rel=stylesheet]").each(function(i, css) {
urls.push(css.href);
});
$("script").each(function(i, js) {
if (js.src) {
urls.push(js.src);
}
});
return urls;
});
// Save all links to a file
var url_file = "list.txt";
fs.write(url_file, links.join("\n"), 'w');
// Launch wget program to download all files from the list.txt to current
// folder
require("child_process").execFile("wget", [ "-i", url_file ], null,
function(err, stdout, stderr) {
console.log("execFileSTDOUT:", stdout);
console.log("execFileSTDERR:", stderr);
// After wget finished exit PhantomJS
phantom.exit();
});
});
dir: /var/www/html/phantom/index.php
exec('/usr/bin/phantomjs index.js 2>&1',$output);
echo '<pre>';
print_r($output);
die;
Also tried with
exec('/usr/bin/phantomjs /var/www/html/phantom/index.js 2>&1',$output);
echo '<pre>';
print_r($output);
die;
After runing this i am getting below error
Array
(
[0] => QXcbConnection: Could not connect to display
[1] => PhantomJS has crashed. Please read the bug reporting guide at
[2] => and file a bug report.
[3] => Aborted (core dumped)
)
But if I run index.php file from the terminal like this:
user2#user2-H81M-S:/var/www/html/phantom$ php index.php
then it works fine.I don't know how to solve it. Please help.
i am using following version
system version: Ubuntu 16.04.2 LTS
PHP version: 5.6
phantomJs version: 2.1.1
Did you tried to set an environment variable on your server ? or added it before calling phantomjs ?
I was in the same situation and found some solutions:
a. define or set variable QT_QPA_PLATFORM to offscreen:
QT_QPA_PLATFORM=offscreen /usr/bin/phantomjs index.js
b. or add this line into your .bashrc file (put it at the end):
export QT_QPA_PLATFORM=offscreen
c. or install the package xvfb and call xvfb-run before phantomjs:
xvfb-run /usr/bin/phantomjs index.js
d. or use the parameter platform:
/usr/bin/phantomjs -platform offscreen index.js
Maybe you don't want / can't make modification on your server and in that case you may try to download the static binary from official website then:
/path/to/the/bin/folder/phantomjs index.js
and / or create an alias in your .bash_aliases file like this:
alias phantomjs=/path/to/the/bin/folder/phantomjs
make sure that phantomjs is not installed already on the system if you decide to use the alias.
if the file .bash_aliases not exist already, feel free to create it or add the alias line at the end of the .bashrc file
Some references:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=817277
https://github.com/ariya/phantomjs/issues/14376
https://bugs.launchpad.net/ubuntu/+source/phantomjs/+bug/1586134
I had the same problem running phantomjs on headless Ubuntu 18.04 (on the default Vagrant vm install of openstreetmap-website). Folloiwng Jiab77's links, it seems the Phantomjs team says the problem is the Debian package but the Debian team closed the bug as wontfix. I needed phantomjs to "just work" so it can be called by other programs that expect it to work normally. Specifically, openstreetmap-website has an extensive Ruby test suite with over 40 tests that were failing because of this, and I didn't want to modify all those tests.
Following Jiab77's answer, here's how I made it work:
As root, cp /usr/bin/phantomjs /usr/local/bin/phantomjs
Edit /usr/local/bin/phantomjs and add the line export QT_QPA_PLATFORM=offscreen so it runs before execution. Here is what mine says after doing so:
#!/bin/sh
LD_LIBRARY_PATH="/usr/lib/phantomjs:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH
# 2018-11-13: added the next line so phantomjs can run headless as explained on
# https://stackoverflow.com/questions/49154209/how-to-solve-error-qxcbconnection-could-not-connect-to-display-when-using-exec
export QT_QPA_PLATFORM=offscreen
exec "/usr/lib/phantomjs/phantomjs" "$#"
After this change, phantomjs can be run from the command line without changing anything else, and all the tests that depend on phantomjs were successfully passed.

AWS Elastic Beanstalk Deployment Order

I'm deploying code to a single-instance web server AWS EB environment that will provision/update my connected RDS database. I've got an .ebextensions file that calls deployment code:
---
container_commands:
01deploydb:
command: /var/www/html/php/cli/deploy-db.php
leader_only: true
On the same deployment, I dropped the deploy-db.php file back one directory into /cli/. On deployment, I get ERROR: [Instance: i-*****] Command failed on instance. Return code: 127 Output: /bin/sh: /var/www/html/php/cli/deploy-db.php: No such file or directory.
container_command 01deploydb in .ebextensions/01_db.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
If I deploy a version that does not include the command, then deploy a second update including the command, there is no error. However, adding the command and the file it calls at the same time produces the error. A similar sequence occurred earlier with a different command/file.
My question is: is there a documented order/sequence for how AWS updates the environment? I would have expected that my new version would have fully deployed (and the .php file installed) before container_commands are called.
The commands: section runs before the project files are put in place. This is where you can install server packages for example.
The container_commands: section runs in a staging directory before the files are put in its final destination. Here you can modify your files if you need to. Current path is this staging directory so you can run it like this (I might get the app directory wrong, maybe it should be php/cli/deploy-db.php)
container_commands:
01deploydb:
command: cli/deploy-db.php
leader_only: true
Reference for above: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
You can also run a post deploy scripts. This is not very well documented (at least it wasn't). You can do something like this (it won't be leader only though, but you could put a file in this directory through a container_commands:):
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_deploy.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
/var/www/html/php/cli/deploy-db.php

Categories