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.
Related
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);
?>
Problem:
I am trying to use wp cli to do stuff. As an example update wordpress:
wp core update
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
path\to\wp-includes\wp-db.php:1564
Stack trace:
#0 path\to\wp-includes\wp-db.php(592): wpdb->db_connect()
#1 path\to\wp-includes\load.php(404):
wpdb->__construct(details)
#2 path\to\public\wp-settings.php(106): require_wp_db()
#3 phar://path/to/wp-cli.phar/php/WP_CLI/Runner.php(1182): require('C:\\path\\to\\...')
#4 phar://path/to/wp-cli.phar/php/WP_CLI/Runner.php(1107): WP_CLI\Runner->load_wordpress()
#5 phar://path/to/wp-cli.phar/php/WP_CLI/Bootstrap/LaunchRunner.php(23): WP_CLI\Runner->start()
#6 phar://path/to/wp-cli.phar/php/bootstrap.php(75): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#7 phar://path/to/wp-cli.phar/php/wp-cli.php(23): WP_CLI\bootstrap()
#8 phar://C:/ in path/to\wp-includes\wp-db.php on line 1564
As far as I can see the error is in mysql_connect().
I have read through the following answers:
Undefined function mysql_connect() - This seemed to suggest downloading some packages. I am reticent to do this cos I don't understand what they do (and currently I'm running php using MAMP so I am not sure if this will cause me more problems) but this did suggest to me that the issue was with php.ini which informs an attempted solution below.
Fatal error: Call to undefined function mysql_connect() - I don't think there is an error in my login details (the site itself works) so this doesn't seem to be the problem
Attempted solution - php.ini
When I check which php.ini wp cli is using via the
wp--info
command. It prints the following:
OS: Windows NT 10.0 build 17134 (Windows 10) i586
Shell: C:\Program Files\Git\usr\bin\bash.exe
PHP binary: C:\MAMP\bin\php\php7.2.1\php.exe
PHP version: 7.2.1
php.ini used:
WP-CLI root dir: phar://wp-cli.phar
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: C:\path\to\public
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.5.1
So it seems no php.ini has been used here. So I think I need to fix that. In order to do that I've found $WP_CLI_PHP_ARGS which I'm trying to put in. Now I'm no coding superstar, but it seems I need to build a bash script to act as a wrapper because they don't work in the .phar version so I have combined two wrappers I found on the web to create this:
#!/usr/bin/env sh
dir=$(d=${0%[/\\]*}; cd "$d"; pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m $dir);
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
"${WP_CLI_PHP}" $WP_CLI_PHP_ARGS "${dir}/wp-cli.phar" "$#"
When I run this it variously complains. I imagine I have made some kind of basic error. (I've also put "export WP_CLI_PHP_ARGS=/C/MAMP/bin/php/php7.2.1/php.ini-production" in my .bash_profile).
mysql_connect() is deprecated, as of PHP 5.5, and removed in PHP 7. PHP 5.5 is not a supported version of PHP, so the author should update their code.
Use mysqli_connect() instead.
Right, okay.
So the issue was flagged by wp-db and it looked as if mysql wasn't working but it was mysqli. Eventually I got the php.ini file working but I thought I'd post all the various solutions that might help others. Number 4 is what worked for me.
1) Turn off mysql_connect so as to force wordpress to use mysqli_connect. Go to wp-config.php and add the line
define('WP_USE_EXT_MYSQL', false);
2) Check your php.ini file is "--with-mysqli=shared" present in the configure command box?
3) Update your mysqli. I didn't do this one, but following this, the advice seems to be to run this in your shell.
sudo apt-get install mysql-server mysql-common php7.0 php7.0-mysql
I'm running git-bash in on windows so that threw up a whole load of nonsense for me. If you're on linux this could work.
4)As above I notice php.ini wasn't give in the wp --info. I found the right file using (just create a file with that, and visit it from your server). This turned out to be in a different place than I was expecting. Then I fiddled with the bash wrapper above and eventually got this, which made the error disappear:
#!/usr/bin/env sh
dir=$(d=${0%[/\\]*}; cd "$d"; pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m $dir);
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
php -c $WP_CLI_PHP_ARGS "${dir}/wp-cli.phar" "$#"
If I've done something stupid let me know, thanks for your time. Feel free to ask for clarifications.
Looks like your wordpress installation is trying to use mysql_connect in wp-db.php. Wordpress defaults to using mysql_connect if it cannot find mysqli installed OR is overridden.
mysqli comes installed with php 7 (and you're using php 7).
So, check wp-config.php and confirm that WP_USE_EXT_MYSQL is defined to false
I'm using Windows 10, PHP 8.0.7.
Installed wp-cli via phar file.
And got this error on every command:
Call to undefined function mysql_connect()...
So I've uncommented this string in my php.ini file:
extension=mysqli
And all worked fine for me. Hope it will be helpful for someone.
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.
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
Normally when I want to run a php script from the command line I just create a php page, add a shebang pointing to the php binary, then ./file.php to run it. Since I have php installed as an apache module, I'm not even sure what my shebang should look like. Any ideas?
The CLI version of PHP had been part of the default installation since 4.3 and has to be explicitly turned off when PHP is being built. If you have access to the command line try
$ php -v
If you don't get a command not found error then you should be ready to go.
To actually run a php file from the command line do this:
$ php -f file.php
If it's just an Apache module, I don't think you can do it… At least, not without using a script like this:
$ cat run_php_with_apache
#!/bin/sh
cp "$1" /var/www/
curl "http://localhost/`basename "$1"`"
rm "/var/www/`basename "$1"`"