Below is the example, I was trying to execute the command using PHP function.
Not getting any error or not happen anything after execute this code.
$siteConfig = <<<EOL
server {
listen 80;
server_name test.local;
root /var/www/html/test.local;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/test.local_error.log;
access_log /var/log/nginx/test.local_access.log;
}
EOL;
$filePath = '/etc/nginx/sites-available/test.local';
shell_exec("echo Test@123 | sudo -S echo '$siteConfig' && sudo tee $filePath");
shell_exec('"echo Test@123 | sudo systemctl restart nginx');
sudo
in non-interactive scripts unless you set it up to work without a password which is a very, very bad idea. Such things are usually done by running the script itself withsudo
but that can also be dangerous. So if you explain what your final goal is (do you want a webpage with a button that does things? Will you be running this manually? Something else?) we can try and give you a good solution.sudo
?