What is the correct way to run an application on a linux server using PHP?

Asked

Viewed 377 times

1

Good afternoon, I need to run an application built natively for linux servers using php.

I have a php script that runs an application on the linux server creating some files needed to continue the application.

When executing the script by the terminal the program runs correctly generating the required files. But when running it from the browser the same script does not run the application.

I have already used the functions system, exec, shell_exec, passthru but none executed properly the application. I would like to know what is the correct way to run applications through php and how to get and handle the respective returns.

Example I am using :

<?php
   exec('/home/dados/controll/ncolinux');
?>

1 answer

4


Hello! When you run the script by the browser, it is the web server user that runs this script. For example: If you are using an Apache server, it runs with a user named apache. Then you need to give enough privileges so that this user can run the application.

To get the return of what was executed, use the function shell_exec(), it returns a string, which is the result of the executed command.

I hope I’ve helped.

  • You can let me know how I change these apache 2 user settings and permissions ?

  • 1

    Changing apache permissions settings may not be a good one. Try changing the permission of the file that needs to be run , using chmod

  • 1

    Lucas, I already gave permission for the file. Thank you.

  • Complementing Lucas' response, Oce needs to add the user (probably www-data) that php uses in the group that is allowed to run the application. In your code you can pass the shell command "whoami" to see which user php uses. And for the application you can access its folder and list using ls-lah to see which group is allowed on that file.

  • @Jaoassy, actually the user php is using is www-data. However I have enabled the permissions to the maximum.

  • Have you added www-data to your application’s corresponding permission group? if yes, it is only necessary to restart the machine, maybe re-log solve.

  • @Jaoassy, I switched the folder’s permission with the command chown www-data nome_pasta and restarted the machine. Unfortunately it has not solved.

  • No need to change the owner of the folder, just add the www-data in the group, execute the ls -lah command you will see 2 names, from left to right, the first is the user and the second is the group. Now run the groups www-data command to see which groups www-data belongs to, if it does not have in the file group add with the following command: useradd -G name www-data, restart and test again.

  • @Jaoassy, there is the possibility to run the file with the user www-data through the terminal using the command $ php arquivo.php ?

  • sudo -H -u www-date application

  • The command line worked @Jaoassy. But the browser does not yet run.

  • put the application permissions, the groups www-date is the error if you have when Voce tries to run via php

  • @Jaoassy, the file permission -rwxrwxrwx. The user group www-data : www-data root. Error did not return any. Sorry for the delay in reply.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.