2

I have only one variable in bash script ${PHP_V} and trying to pass in nginx config file like:

cat <<'EOF' > /etc/nginx/sites-available/default
server {
    listen 80 default_server; 
    listen [::]:80 default_server; 

    listen 443 ssl default_server; 
    listen [::]:443 ssl default_server; 

    root /vagrant/webroot; 

    index index.php; 

    server_name _; 

    ssl_certificate /etc/nginx/certs/vagrantbox.crt; 
    ssl_certificate_key /etc/nginx/certs/vagrantbox.key;  

    location / { 
        try_files $uri $uri/ /index.php?$args; 
    } 

    location ~ \.php$ { 
        try_files $uri =404; 
        include fastcgi_params; 
        fastcgi_pass unix:/run/php/php${PHP_V}-fpm.sock; 
        fastcgi_index index.php; 
        fastcgi_intercept_errors on; 
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name; 
    }
}
EOF

but without success. How to do?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Salines
  • 123
  • 5
  • 1
    See [passing and setting variables in a heredoc](https://unix.stackexchange.com/questions/405250/passing-and-setting-variables-in-a-heredoc) – steeldriver May 31 '19 at 16:17

1 Answers1

7

You need to unquote EOF:

If any part of word is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself.

If no part of word is quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion.

See : 2.7.4 Here-Document


Note however that there are several things in your here document that will need to be individually escaped to prevent expansion:

cat <<EOF > /etc/nginx/sites-available/default
server {
    listen 80 default_server; 
    listen [::]:80 default_server; 

    listen 443 ssl default_server; 
    listen [::]:443 ssl default_server; 

    root /vagrant/webroot; 

    index index.php; 

    server_name _; 

    ssl_certificate /etc/nginx/certs/vagrantbox.crt; 
    ssl_certificate_key /etc/nginx/certs/vagrantbox.key;  

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

    location ~ \.php\$ { 
        try_files \$uri =404; 
        include fastcgi_params; 
        fastcgi_pass unix:/run/php/php${PHP_V}-fpm.sock; 
        fastcgi_index index.php; 
        fastcgi_intercept_errors on; 
        fastcgi_param SCRIPT_FILENAME 
        \$document_root\$fastcgi_script_name; 
    }
}
EOF
jesse_b
  • 35,934
  • 12
  • 91
  • 140