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 | Show 3 more comments2 Answers
Reset to default 0As 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.
php -v | awk '{print $2}' | head -n 1
– rozsazoltan Commented Mar 10 at 18:49php
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. Usetype php
to get the path, and use that in the script. – Barmar Commented Mar 10 at 19:22