# Nginx-WordPress conf

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


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

• Hi, thanks ! Please elaborate on "Each of your location section has a different indentation. Keep it consistent". – user9303970 Mar 9 '18 at 5:33
• @user9303970 What I meant was, two of your location sections have a 4-space further indented closing }, whereas the last one doesn't. – hjpotter92 Mar 9 '18 at 10:36
• Oh yes, this is a mistake and I found myself a few days ago and fixed. Good you noted that anyway. – user9303970 Mar 9 '18 at 10:37
• 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 <<-. – user9303970 Mar 9 '18 at 10:39
• 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?" – hjpotter92 Mar 9 '18 at 10:41