下面是示例,我尝试使用 PHP 函数执行命令。
执行此代码后没有出现任何错误或没有发生任何事情。
$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');