Below is the example, I was trying to execute the command using phpPHP function.
Not getinggetting 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;
// Specify the file path
$filePath = '/etc/nginx/sites-available/test.local';
// Use shell commands to create the file
shell_exec("echo Test@123 | sudo -S echo '$siteConfig' && sudo tee $filePath");
// You may need to restart Nginx for changes to take effect
shell_exec('"echo Test@123 | sudo systemctl restart nginx');