2

This question already has an answer here:

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?

marked as duplicate by muru, Thomas, Jeff Schaller Jun 1 at 14:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.