exec uglifyJS with php - php

I try to exec a simple cmd (which works on my terminal server), but not in my php script.
I use UglifyJS2.
It install in my /home/MY_USER/node_modules/uglify-js/
I have chmod the symlink like here: calling node and uglifyjs from Php context.
But
exec(/usr/bin/node /usr/bin/uglifyjs ...);
or
exec(/usr/bin/uglifyjs ...);
doesn't work.
I haven't chmod my /home/ of course, but it's maybe the reason why doesn't works?
How can I do exec uglify by my php?

After fixing the syntax errors the apache (or web server) user will need exec permissions for the dir and bin. Why not install it in /usr/bin?

Related

Link php interpreter from Docker to bash somehow

I'm trying to use my docker php as an interpreter in my terminal. What I need:
I don't keep my dev envs on my host os. That's important to keep it isolated
It should be available as $php or $/usr/bin/env php
I'd like to be able to run something like phpcs in my vim. It requires that thingy above.
I've tried this:
alias php='docker-compose exec php php'
But it's not available through /usr/bin/env
/usr/bin/env tries to locate the executable via the $PATH variable, see https://unix.stackexchange.com/a/12749
So if you put your command in to a script called PHP and add it to your $PATH before other PHP executables, it might work.

PHP exec on local script

Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)

Compiling C from PHP with exec error trying to exec 'cc1'

I am trying to compile a C program from PHP with exec, and with the Laravel Framework. But I dont think this is the problem, because I can compile and execute C programs from terminal without problems. And if you know from tinker in Laravel 5, so the problem is from PHP. But I can`t find the error I think the problem is form different versions of GCC but why let me compile from terminal.
I get this error when I do that from PHP. If I compile from terminal it works but from php not.
$path = public_path("testing/cosas.out");
exec("gcc testing/pruebaC.c -o testing/from.out 2>&1",$output,$status);
dd($output,$status); //is like var_dump
AND I GET THIS !!
gcc: error trying to exec 'cc1': execvp: No such file or directory"
I checked the permissions and are right (in fact I did chmod 777 in my desperation).
Also I tried to reinstall everything, but it does not work.
The problem is that your application when invoked through a browser functions through the user that is processing the Apache instance. If this is not the root (or another privileged user), then it may not have access. Also, this will likely dictate what directory the application attempts to execute from.
When you execute from the CLI, the user is whomever owns the instance of the terminal (unless su'd of course).
Here's a minimal example of how to make this work:
First, create a new directory and cd to it. In that directory, create index.php with this content:
<?php
exec("gcc /var/www/html/test.c -o /tmp/a.out 2>&1",$compile_output,$compile_status);
var_dump($compile_output);
var_dump($compile_status);
exec("/tmp/a.out 2>&1",$run_output,$run_status);
var_dump($run_output);
var_dump($run_status);
?>
And create test.c with this content:
#include <stdio.h>
int main(void) {
puts("Hello from C compiled by PHP!");
return 0;
}
Then do docker run -p 8080:80 -v /whatever/directory/you/created:/var/www/html php:apache. Finally, go to http://localhost:8080 and the PHP script will compile and run that C program.
If this works in Docker but not in a "real" environment, then your environment is somehow set up incorrectly. In particular, check the PATH to make sure you're using the gcc that you think you are, and check the output of gcc -print-search-dirs and make sure that cc1 can indeed be found somewhere that it's looking.
If it works from the terminal but not from PHP, then put the debugging commands in the PHP script until you find the difference that's breaking it.
If you're missing cc1 entirely, then do sudo apt --reinstall install build-essential, or whatever the equivalent is to reinstall gcc and its dependencies on your distro.

How do I run a C++ binary on OpenShift via PHP?

I SSH'd into my OpenShift application and compiled the C++ file using gcc and then downloaded it to my computer (for backup). I added it to git repo and pushed it.
How do I execute it in the current directory? I have tried changing permissions to 777 using chmod. I've tried exec(), shell_exec(), passthru(), system() in PHP with no luck. None of them gives me the output of the program.
Commands I used
Compiling C++: gcc code.cpp -o code.out
Inside run.php: chmod 777 code.out && ./code.out input-file (also tried chmod("code.out", 777);)
input-file is also pushed together with the code.out in same directory.
After a bit of testing, I found that it returns code 126 which is Permission problem or command is not an executable, but the permissions is 777 and it is in fact an executable.
Am I missing something?
(I'm sorry, but I don't have any experience with this)
Change the permission of the file so all can execute.
chmod a+rwx file
I prefer to use this instead of using 0777. That depends on your preference though.

travis-ci script

I'm trying to setup phing to work with travis-ci, but I can't get it to run a setup script to get all the dependencies installed.
My .travis.yml file is:
language: php
php:
- 5.2
script: ./.travis-phing.sh
In travis, I get the error:
/home/travis/build.sh: line 105: ./.travis-phing.sh: Permission denied
What is causing that?
Solved
The script to be set to execute. I used:
chmod a+x .travis-phing.sh
Then simply commit, and push back to github.
Run the script using bash
Another option would be to run the script using bash, this would omit the need to modify the files' permissions.
bash path/to/file.sh
Alternatively:
sh path/to/file.sh
Note that
In this case you're not executing the script itself, you're executing bash or sh which then runs the script. Therefore the script does not need to be executable.
Make sense?
I've found this solution incredibly useful myself. I'm mainly running node & npm projects on travis-ci, those builds make use of the npm test command which you can configure to be anything.
I'm order to modify file permission I need to use sudo chmod ... on my local machine. But you can't always use sudo on travis-ci.
sh file.sh allows me to run my tests both locally and on travis-ci without having to manually update permissions.

Categories