1
\$\begingroup\$

This is how I create am Nginx virtual host for a WordPress webapp. I did best to keep it as minimal as I can while having a working webapp. Please review it:

#!/bin/bash
domain="$1" && test -z "$domain" && exit 2
drt="/var/www/html" 

cat <<-EOF > "$s_a/$domain.conf"
    server {
        root ${drt}/${domain};
        server_name ${domain} www.${domain};
        index index.php index.html index.htm;
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
            }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            }
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
            expires 365d;
        }
    }
EOF
\$\endgroup\$
1
\$\begingroup\$

You are putting your server_name to be $domain and www.$domain. What if, someone puts in www.example.com for the script argument? You will thne be serving www.example.com and www.www.example.com. Instead; replace possible www. from the domain variable.

domain="${domain#www\.}"

Each of your location section has a different indentation. Keep it consistent. Since server section is the root level, you do not need to indent it out. I understand that you are flushing the left indentation from the conf, but I would suggest keeping it, since one might want to peek at them in future, and would like to tweak it?


#!/bin/bash
domain="$1" && test -z "$domain" && exit 2
domain="${domain#www\.}"
drt="/var/www/html" 

cat <<EOF > "$s_a/$domain.conf"
server {
    root "${drt}/${domain}";
    server_name ${domain} www.${domain};
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    location ~* \.(jpe?g|png|gif|ico|css|js|ttf|woff|pdf)$ {
        expires 365d;
    }
}

EOF
\$\endgroup\$
  • \$\begingroup\$ Hi, thanks ! Please elaborate on "Each of your location section has a different indentation. Keep it consistent". \$\endgroup\$ – user9303970 Mar 9 '18 at 5:33
  • \$\begingroup\$ @user9303970 What I meant was, two of your location sections have a 4-space further indented closing }, whereas the last one doesn't. \$\endgroup\$ – hjpotter92 Mar 9 '18 at 10:36
  • \$\begingroup\$ Oh yes, this is a mistake and I found myself a few days ago and fixed. Good you noted that anyway. \$\endgroup\$ – user9303970 Mar 9 '18 at 10:37
  • \$\begingroup\$ It was important for me inident the server in this case because the heredocument will de-indent it in execution. It will de-indent everything in exeuction to be honest, due to the hyphen in <<-. \$\endgroup\$ – user9303970 Mar 9 '18 at 10:39
  • \$\begingroup\$ which is why I mentioned: "I understand that you are flushing the left indentation from the conf, but I would suggest keeping it, since one might want to peek at them in future, and would like to tweak it?" \$\endgroup\$ – hjpotter92 Mar 9 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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