Skip to main content
deleted 128 characters in body
Source Link

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

Below is the example, I was trying to execute the command using php function.

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

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

deleted 1 character in body
Source Link

Below is the example, I was trying to execute the command using php function.

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

Below is the example, I was trying to execute the command using php function.

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

Below is the example, I was trying to execute the command using php function.

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

Source Link

How to run password with sudo command using php?

Below is the example, I was trying to execute the command using php function.

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