Execution failing while running from browser - php

I am building online compiler for C and C++ using php, so we are using gcc compiler to execute using "shell_exec" function ,
test.php
<?php
$output = shell_exec("gcc /var/www/test/main.c 2>&1");
print_R($output);
?>
If I execute in terminal like php test.php, is working fine and it is creating a.out compiled file.
But If I try to run in the browser like localhost/test.php, its giving below error
gcc: error trying to exec 'cc1': execvp: No such file or directory
I gave full permission (0777) to test.php,
$ whereis cc1 // cc1:
Please find below version I am using
gcc version 5.4.0
PHP 7.0.32
OS: ubuntu 16.04
server : Nginx
Is there any alternate to run C and C++ using PHP or how to resolve this issue whille running from the browser.

Reading the error message, the problem comes from two issues:
php cannot find gcc since $path variable is not correctly set, in php file, you should type something like PATH=/usr/bin && gcc /var/www/test/main.c. In this example, gcc and other stuff are in /usr/bin directory. To know where are located cc1 for instance, type find / -name cc1 and add it to $PATH: PATH=/some/path:/some/other/path
Current directory of php could no be writtable, so indicate where the generated file must be created: gcc /var/www/test/main.c -o /var/www/test/main
Directory /var/www/test must be writable by user running php program.
Note that the user running the php program is not necessary the root...
So your php file should looks like:
<?php
$output = shell_exec("PATH=/usr/bin && gcc /var/www/test/main.c -o/var/www/test/main 2>&1");
print_R($output);
?>

Related

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.

PHP exec function on nginx doesn't recognize system environment variables

I try to use exec function in PHP to compile a source file with gcc with the following code.
<?php
exec("gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
I got the following output when calling via web browser (I use nginx as a web server).
gcc: error trying to exec 'cc1': execvp: No such file or directory
However, if I run gcc -o hello hello.c on the command shell directly or call with php my_file.php on the shell directly, both ways compile successfully.
If I append the absolute path to gcc in my PHP code like this:
<?php
exec("/usr/bin/gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
I got the following output.
collect2: fatal error: cannot find 'ld'
So, I think the problem is my webserver (nginx) doesn't know the system path environment variable to find /usr/bin which gcc and other gcc-dependencies resides in.
How can I let PHP exec function recognize system environment variables on nginx?
OS: Ubuntu 14.04
nginx: 1.6.2
PHP 5.5.9
I'm stuck on the same problem (exactly the same...) using nginx 1.10 and PHP 5.6 on Arch Linux.
The same PHP code was working on Apache/Debian.
When trying the Arch/Nginx server, I had the cc1 error... I replaced gcc by /usr/bin/gcc.... and I am now stuck on "collect2: fatal error: cannot find 'ld'"
The same compilation works with a shell... and ld is in /usr/bin. It just does not work when using "exec" in PHP.
Not satisfying (but working...) solution
By running gcc -v .....(just add -v to your compilation line) I could see :
...
COLLECT_GCC_OPTIONS='-v' '-D' 'exit=noexit' '-D' '_exit=noexit' ...
/usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/collect2 -plugin ... (<= very long line)
collect2: fatal error: cannot find 'ld'
Then I did :
cd /usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/ # <- path to collect2 (see above)
ln -s /usr/bin/ld ld
Now ld prog is available in the same dir as collect2.
And it works.
I am now looking for a better solution... :)
Set the PATH variable for PHP, as it may rely on its own environment variables and ignore the system's PATH variable.
For example, I have the following line at the bottom of my .env file:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
With this, executing gcc (without giving the full path /usr/bin/gcc) works correctly.
Duplicate of How compile GCC from php. Thought I'd post the answer here as well, since this question received a lot more attention than the other one.

Calling ansi2html.sh from php exec gives gawk error on OSX

I'm trying to get formatted coloured output from a command line utility presented on a php web page.
I'm using ansi2html.sh from http://www.pixelbeat.org/scripts/ansi2html.sh
It works fine if I run the same command from the command line. I am on OSX.
The command I am running is:
exec ("vendor/bin/phinx migrate -e development | ../../phinx_upgrades/ansi2html.sh 2>&1", $phinx_output, $phinx_return);
The output I get is:
../../phinx_upgrades/ansi2html.sh: line 38: gawk: command not found
So I assume its running the script, but it cant find gawk. I did brew install gawk but its already installed.
I can run gawk from command line, gawk --version gives me GNU Awk 4.1.1, API: 1.1
If I ask which gawk I get /usr/local/bin/gawk
When I echo $PATH I see /usr/local/bin as one of my paths.
I'm not familar with OSX, but i think that you have to add gawk folder to your environment variable PATH
Also, check directly on your bash if you can launch gawk, if it is work fine in your bash, so restart your Apache server
Hope that helps :)
Answer PHP exec $PATH variable missing elements should give hints for setting up the right $PATH for the php call. As a quick fix you could edit the ansi2html.sh script to reference /usr/local/bin/gawk directly

Howto create an alias for Zend Framework to work with Cygwin in Windows with XAMPP

I'm using Zend Framework at the office in a Windows7+XAMPP environment; I'm not able to change this even if I want to, so I need to adapt. SO, I naturally installed Cygwin and Console2 in order to work in a more Linux-like environment.
The PHP and MySQL paths are in the environmental variables of Windows, so if I do in CMD this:
php --version
PHP 5.4.4 (cli) (built: Jun 13 2012 21:27:06)
There is a reponse. If I do the same in my Cygwig environment I also got response, so Cygwin can find the PHP path.
About Zend Framework, I downloaded the zip file and extracted the contents of the ZF bin directory to C:\xampp\php (that means zf.sh, zf.bat and zf.php) and all the library folder content to C:\xampp\php\pear\Zend so those are in the right path to be used.
In Windows CMD I managed to create something similar to an alias this way:
#cmd_aliases.txt
zf=php C:\xampp\php\zf.php $*
#cmd_autorun.cmd
#echo off
cls
doskey /macrofile=C:\Users\hector.ayala\Documents\cmd_aliases.txt
#cmd_autorun_install.cmd
reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d C:\cmd_autorun.cmd
And now in CMD I can do:
>zf show version
Zend Framework Version: 1.11.12
...as intended. HOWEVER I can't do something similar in CygWin...
I did this in Cygwin:
$ cd ZendFramework-XX
$ mv bin/* /usr/local/bin
$ chmod +x /usr/local/bin/zf.sh
$ ln -s /usr/local/bin/zf.sh /usr/local/bin/zf
That means I did a symlink to the zf.sh in order to launch ZF just with zf. However I got this:
$ zf show version
Could not open input file: /usr/local/bin/zf.php
Then I said, Oh well! Maybe it's ZF problem... My PHP returns me it's version so PHP works, surely any kink of php file will work:
$ php simple_test.php
Could not open input file: simple_test.php
What the...? Why PHP works but at the same time it doesn't?
Any ideas what can I do to just call Zend Framekork CLI with a simple custom zf as I did with CMD?
I've found way!!!!
I didn't knew about cygpath It's a command line utility for converting between Windows and POSIX paths. Because Windows couldn't understand the path, the zf.sh couldn't process it and because of that I could get PHP version, but PHP couldn't comprehend the path to files to work with them, thus the Could not open input file error. So all PHP needed was to understand the path.
So reading the help of cygpath in the given link, I was able to understand this command and adapt the official zf.sh to my needs. So I changed into it the very last line from this:
"$PHP_BIN" -d safe_mode=Off -f "$PHP_DIR/zf.php" -- "$#"
To this:
"$PHP_BIN" -d safe_mode=Off -f "$(cygpath -aw $PHP_DIR/zf.php)" -- "$#"
Now it works!
$ zf show version
Zend Framework Version: 1.11.12
Hopefully someone will find this useful ;)

Launch simple php daemon on Ubuntu

I want launch simple php daemon on Ubuntu without fork.
source f.php:
#!/usr/bin/php
<?php
file_put_contents('/var/www/3.txt',date("H:i:s", time()) . "\n", FILE_APPEND);
while (true) {
file_put_contents('/var/www/3.txt',date("H:i:s", time()) . "\n", FILE_APPEND);
sleep(1);
}
launch:
root#ubuntu:/var/www# ./f.php &
[3] 10323
On FreeBSD this work well on Ubuntu file 3.txt is not updating :(
permissions are ok (777)
can you check that the path you are pointing to for the binary file of PHP is correct, i mean PHP is really installed in /usr/bin/php, you can check that by using below command
which php
also you can try to run it manually instead of running it as an executable by below command
php -f f.php &
and also check if your php file is giving any errors in apache logs, you can find apache logs in /var/log/apache2/error.log
and also make sure that php5-cli package is installed by executing following command
apt-get install php5-cli
Update:
according to http://www.freelance-it-consultant.com/blog/php-cli-script-running-background-ubuntu, there is some bug in ubuntu when php is run through CLI it expects some input from user, so can you try this method if that works for you?
php -q f.php < /dev/null &

Categories