简体   繁体   中英

Install composer using php shell_exec function

I'm trying to install composer using PHP shell_exec command as described in composer official site .

My server linux is centOS 6 .

shell_exec('curl -sS https://getcomposer.org/installer | php');
/*
 * OR
 */
shell_exec('php -r "readfile(\'https://getcomposer.org/installer\');" | php');

but there's permission error :

> Downloading... 

>Could not create file /myfolder/composer.phar
>
> fopen(/myfolder/composer.phar): failed to open stream: Permission denied Download failed:
>
> fopen(/myfolder/composer.phar): failed to open stream: 
>
> Permission denied fwrite() expects parameter 1 to be
> resource, boolean given 

>Downloading... Could not create file to open stream: composer.phar:
>
>etc...

So, I tried to change user to root using this command :

shell_exec('su -root; myPass; whoami');
//And
shell_exec('su - root; myPass; whoami');
//And
shell_exec('su -u root -p myPass; whoami');

And again whoami return apache

It seems user apache do not have right permission for install composer and create directories, so I tried to install composer after previous commands but still there's same problem.

Would you please tell me there's something wrong with my command or what's the problem?

Thanks in advance

You cannot provide su a password this way. In fact I don't think there is a way at all ( there is a way for sudo )

But elevating to root is a very poor idea if only for security reasons. Instead you should give apache write permissions on /myfolder/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM