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
Answers
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.Then give the ownership of the
merchants
folder to thewww-data
group using the command below.Lastly give the correct permissions to the
merchants
folder using the command below.Hope this helps you solve your issue.
EDIT
On CentOS, you should use
apache
instead ofwww-data
.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:
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.