0

I have a simple scripts that create folder for every user that sign up, it works perfectly fine on local but after pushing to live server it does not work... I discovered that it require sudo password. I google around and all suggested that I should edit sudoer by running visudo but nothing work.

 $sudoPassword = \Config::get('app.sudoPassword');

 $sudo = "echo $sudoPassword | sudo -S ";

 $createDir =  "cd ../../merchants && mkdir ".$merchant->identity." && cd ".$merchant->identity;

echo exec($createDir);

my question is, how should I make exec to run as root? I'm using centOS in digitalocean

2 Answers2

2

I don't recommend running the script as sudo. A better alternative is to give permission to the web server (www-data user) to write to the ../../merchants directory.

First add yourself to the www-data group by running the command below.

usermod -a -G www-data <your-username>

Then give the ownership of the merchants folder to the www-data group using the command below.

chgrp www-data merchants

Lastly give the correct permissions to the merchants folder using the command below.

chmod g+rwxs merchants

Hope this helps you solve your issue.


EDIT

On CentOS, you should use apache instead of www-data.

tamrat
  • 1,815
  • 13
  • 19
1

This is not a direct answer to your question, just a suggested improvement to your code. Instead of using sudo I would stick to @tamrat's solution. This is more ment as a comment but is too long to be a comment.

Instead of using php's exec function I would recommend you using Symfony's process component which is already available in your Laravel project.

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

Use:

$createDir =  "mkdir /full/path/to/merchants/".$merchant->identity." && cd /full/path/to/merchants/".$merchant->identity;

$process = new Process($createDir);
$process->run();

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

I also suggest that you use full paths rather then relative paths (if possible for your use-case) so you don't do actions in another directory when you re-use the code from another file. If not make sure you allways are in the correct path by validating properly.

Andrew Larsen
  • 1,257
  • 10
  • 21