0

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.

alk
  • 69,737
  • 10
  • 105
  • 255
Abhi
  • 1,105
  • 2
  • 12
  • 29
  • It becouse gcc is root's program. In terminal you can run it (your profile can run root's programs). But brouser starts other user (like www-data, or other). And it user cant run root's programs – Vasyl Zhuryk Oct 20 '18 at 08:01
  • @Vasyl Zhuryk, thanks for the reply, is there anyway to fix this or any alternate method for this? – Abhi Oct 20 '18 at 08:03
  • That is not secure, but you can try: `ln -s /usr/bin/gcc /usr/local/bin/gcc` It creates symbolic link to program without root privileges – Vasyl Zhuryk Oct 20 '18 at 08:09
  • Does this `gcc /var/www/test/main.c` complete successfully when executed from a shell on the server itself? – alk Oct 20 '18 at 08:17
  • @alk, yes it execute successfully while running from terminal – Abhi Oct 20 '18 at 08:20
  • @Vasyl, I created sym link in /usr/local/bin/gcc, still error gcc: error trying to exec 'cc1': execvp: No such file or directory – Abhi Oct 20 '18 at 08:21
  • @Abhi can you try this code? `shell_exec("/usr/bin/gcc /var/www/test/main.c 2>&1");` – Vasyl Zhuryk Oct 20 '18 at 08:24
  • @Vasyl , its giving now error as , collect2: fatal error: cannot find 'ld' compilation terminated – Abhi Oct 20 '18 at 08:32
  • @Abhi can you show your `main.c` file? – Vasyl Zhuryk Oct 20 '18 at 09:17
  • @ Vasyl Zhuryk , #include int main() { printf("Hello...."); return 0; } – Abhi Oct 20 '18 at 11:38
  • @Abhi I tried to do it in my computer - all done right. In your case can help this: https://stackoverflow.com/questions/11912878/gcc-error-gcc-error-trying-to-exec-cc1-execvp-no-such-file-or-directory – Vasyl Zhuryk Oct 20 '18 at 12:22
  • @Vasyl Zhuryk - Can I know your gcc and ubuntu version – Abhi Oct 21 '18 at 10:26
  • @Abhi `$ gcc -v ... Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)` – Vasyl Zhuryk Oct 21 '18 at 11:42
  • @Abhi `PHP 7.1.23-2+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Oct 15 2018 11:36:43) ( NTS )` Also I added my user to apache user (in your case nginx user) – Vasyl Zhuryk Oct 21 '18 at 12:00

1 Answers1

0

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);
?>
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Hello purplepsycho, still I am getting same error gcc: error trying to exec 'cc1': execvp: No such file or directory – Abhi Oct 20 '18 at 08:36
  • @ purplepsycho, I updated path as , PATH=/usr/bin:/usr/lib/gcc/x86_64-linux-gnu/5/cc1, still the same error – Abhi Oct 20 '18 at 08:53
  • @Abhi Do not put the `cc1` in path – Mathieu Oct 20 '18 at 08:54
  • still the same for shell_exec("PATH=/usr/bin:/usr/lib/gcc/x86_64-linux-gnu/5/ && gcc /var/www/test/compiler/test/main.c -o/var/www/test/compiler/test/main 2>&1"); – Abhi Oct 20 '18 at 08:56