Skip to main content
3 of 3
deleted 128 characters in body

How to run password with sudo command using php?

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');