最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Getting PHP Version from Bash script not working - Stack Overflow

programmeradmin1浏览0评论

I'm developing a Node.js application that automates the creation of Nginx server blocks on a DigitalOcean droplet. I need to dynamically determine the installed PHP version on the droplet so I can correctly configure the fastcgi_pass directive in my Nginx server block.

Here's the Node.js function I'm using to generate the server block script:

function createNginxServerBlockScript(domain, rootDir = "/var/www") {
  const sitePath = `${rootDir}/${domain}`;

  return `
    echo "Creating Nginx server block..."

    # Get PHP version dynamically
    PHP_VERSION=$(php -r 'echo PHP_VERSION;')

    # Print PHP version to check if it's set correctly
    echo "PHP Version: \${PHP_VERSION}"

    cat > /etc/nginx/sites-available/${domain} <<EOF
    server {
      listen 80;
      listen [::]:80;
      server_name ${domain};
      root ${sitePath};
      index index.php index.html index.htm;

      # WordPress permalinks
      location / {
        try_files \\\$uri \\\$uri/ /index.php?\\\$args;
      }

      # Pass PHP scripts to FastCGI server
      location ~ \\.php\\\$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php\${PHP_VERSION}-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME \\\$document_root\\\$fastcgi_script_name;
      }

      # Static files (caching images, CSS, JS)
      location ~* \\\\.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|eot)\\\$ {
        expires max;
        log_not_found off;
      }

      # Deny access to hidden files
      location ~ /\\\\. {
        deny all;
      }

      # Log files
      access_log /var/log/nginx/${domain}.access.log;
      error_log /var/log/nginx/${domain}.error.log;
    }
EOF
  `;
}

function completeScript(domain) {
  return `#!/bin/bash
      
    ${createNginxServerBlockScript(domain)}
      
  `;
}

Result:

Executing complete script...
stdout: awk: 
stdout: cmd. line:1: {print $1 \".\" $2}
awk: cmd. line:1:           ^ backslash not last character on line

stdout: Setting up site directory...

stdout: Creating Nginx server block...

stdout: PHP Version: 
stdout: 

stdout: Enabling Nginx site...

stdout: Enabled Nginx site configuration.

stdout: Testing Nginx configuration...
stdout: 
stdout: 

stdout: 2025/03/10 18:13:14 [emerg] 75030#75030: invalid number of arguments in "fastcgi_param" directive in /etc/nginx/sites-enabled/test.outreachbuddy:18
nginx: configuration file /etc/nginx/nginx.conf test failed

stdout: Nginx configuration test failed

Remote script finished with exit code: 1
Error executing the complete script: Error: Script failed with code 1
Output:
awk: cmd. line:1: {print $1 \".\" $2}
awk: cmd. line:1:           ^ backslash not last character on line
Setting up site directory...
Creating Nginx server block...
PHP Version: 
Enabling Nginx site...
Enabled Nginx site configuration.
Testing Nginx configuration...
2025/03/10 18:13:14 [emerg] 75030#75030: invalid number of arguments > in "fastcgi_param" directive in /etc/nginx/sites-enabled/test.outreachbuddy:18
nginx: configuration file /etc/nginx/nginx.conf test failed
Nginx configuration test failed

Why is PHP_VERSION=$(php -r 'echo PHP_VERSION;') not working as expected within the generated bash script on the DigitalOcean droplet?

Are there alternative methods to reliably determine the installed PHP version from within the script?

I'm developing a Node.js application that automates the creation of Nginx server blocks on a DigitalOcean droplet. I need to dynamically determine the installed PHP version on the droplet so I can correctly configure the fastcgi_pass directive in my Nginx server block.

Here's the Node.js function I'm using to generate the server block script:

function createNginxServerBlockScript(domain, rootDir = "/var/www") {
  const sitePath = `${rootDir}/${domain}`;

  return `
    echo "Creating Nginx server block..."

    # Get PHP version dynamically
    PHP_VERSION=$(php -r 'echo PHP_VERSION;')

    # Print PHP version to check if it's set correctly
    echo "PHP Version: \${PHP_VERSION}"

    cat > /etc/nginx/sites-available/${domain} <<EOF
    server {
      listen 80;
      listen [::]:80;
      server_name ${domain};
      root ${sitePath};
      index index.php index.html index.htm;

      # WordPress permalinks
      location / {
        try_files \\\$uri \\\$uri/ /index.php?\\\$args;
      }

      # Pass PHP scripts to FastCGI server
      location ~ \\.php\\\$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php\${PHP_VERSION}-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME \\\$document_root\\\$fastcgi_script_name;
      }

      # Static files (caching images, CSS, JS)
      location ~* \\\\.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|eot)\\\$ {
        expires max;
        log_not_found off;
      }

      # Deny access to hidden files
      location ~ /\\\\. {
        deny all;
      }

      # Log files
      access_log /var/log/nginx/${domain}.access.log;
      error_log /var/log/nginx/${domain}.error.log;
    }
EOF
  `;
}

function completeScript(domain) {
  return `#!/bin/bash
      
    ${createNginxServerBlockScript(domain)}
      
  `;
}

Result:

Executing complete script...
stdout: awk: 
stdout: cmd. line:1: {print $1 \".\" $2}
awk: cmd. line:1:           ^ backslash not last character on line

stdout: Setting up site directory...

stdout: Creating Nginx server block...

stdout: PHP Version: 
stdout: 

stdout: Enabling Nginx site...

stdout: Enabled Nginx site configuration.

stdout: Testing Nginx configuration...
stdout: 
stdout: 

stdout: 2025/03/10 18:13:14 [emerg] 75030#75030: invalid number of arguments in "fastcgi_param" directive in /etc/nginx/sites-enabled/test.outreachbuddy:18
nginx: configuration file /etc/nginx/nginx.conf test failed

stdout: Nginx configuration test failed

Remote script finished with exit code: 1
Error executing the complete script: Error: Script failed with code 1
Output:
awk: cmd. line:1: {print $1 \".\" $2}
awk: cmd. line:1:           ^ backslash not last character on line
Setting up site directory...
Creating Nginx server block...
PHP Version: 
Enabling Nginx site...
Enabled Nginx site configuration.
Testing Nginx configuration...
2025/03/10 18:13:14 [emerg] 75030#75030: invalid number of arguments > in "fastcgi_param" directive in /etc/nginx/sites-enabled/test.outreachbuddy:18
nginx: configuration file /etc/nginx/nginx.conf test failed
Nginx configuration test failed

Why is PHP_VERSION=$(php -r 'echo PHP_VERSION;') not working as expected within the generated bash script on the DigitalOcean droplet?

Are there alternative methods to reliably determine the installed PHP version from within the script?

Share Improve this question edited Mar 11 at 7:15 Abdulla Nilam 38.8k18 gold badges79 silver badges104 bronze badges Recognized by PHP Collective asked Mar 10 at 18:43 Ben JonsonBen Jonson 7391 gold badge10 silver badges31 bronze badges 8
  • 1 What result does the mentioned command produce instead of the expected result? – rozsazoltan Commented Mar 10 at 18:48
  • 2 Alternative: php -v | awk '{print $2}' | head -n 1 – rozsazoltan Commented Mar 10 at 18:49
  • Updated the question with output. – Ben Jonson Commented Mar 10 at 18:56
  • You should include Node.js in your tags and might want to drop nginx. Have you tried SSHing to an example machine and running the BASH script directly there? Have you tried pushing the script out as a file and calling the script file from Node.js instead of the method that you're using (which is not clear). Try to reduce the script to the simplest thing that does not work. As is, you're leaving out context, so we can't run your script to see what it would do for us. You're getting an error about awk but don't call awk in your snippet. – mdfst13 Commented Mar 10 at 19:05
  • 4 Is php in the default $PATH? If not, it might not be found when the script runs, so you get no output in the PHP_VERSION variable. Use type php to get the path, and use that in the script. – Barmar Commented Mar 10 at 19:22
 |  Show 3 more comments

2 Answers 2

Reset to default 0

As far as I know, fastcgi_pass on nginx uses only the major and minor versions. (7.3) When you use PHP_VERSION it will include patch number as well. (Ex: 7.3.25)

Either use

PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")

Or

PHP_VERSION=$(php -v | grep -oP '^PHP \K\d+\.\d+' | head -1)

The issue was resolved by changing how the script was executed. Previously, it was run directly through Steam. Now, the script is written to a temporary file, made executable, and then executed with sudo bash. This change, using the following code, fixed the problem:

const command = `
      cat > /tmp/temp_script.sh << 'EOFSCRIPT'
${escapedScript}
EOFSCRIPT
      chmod +x /tmp/temp_script.sh
      sudo bash /tmp/temp_script.sh
      rm /tmp/temp_script.sh
    `;

solved the problem.

发布评论

评论列表(0)

  1. 暂无评论