I use the following script to do the following actions in my environment:
- Creating an Nginx site conf.
- Creating a corresponding Let'sEncrypt SSL certificate.
- Creating a symlink between my
sites-default
dir to my site conf. - Restarting the server.
My code:
#!/bin/sh
for domain; do
> "/etc/nginx/sites-available/${domain}.conf" cat <<EOF
server {
root /var/www/html/${DOMAIN};
server_name ${DOMAIN} www.${DOMAIN};
location ~ /\.ht {
deny all;
}
location / {
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
expires 365d;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/${domain} /etc/nginx/sites-enabled/
#########################################################################
certbot --nginx -d ${DOMAIN} -d www.${DOMAIN}
done
systemctl restart nginx.service # Create Webapp Substrate.
How and why I want to improve this code:
Where the long row of hashmarks, I want to insert the following command:
echo 'create database ${DOMAIN}; create user "${DOMAIN}"@"localhost" identified by "mypassword"; grant all privileges on ${DOMAIN}.* to ${DOMAIN};' | mysql -u root -p
Adding this command will change the script to do 5 steps instead 4:
- Creates an Nginx site conf.
- Creates a corresponding Let'sEncrypt SSL certificate.
- Creating a symlink between my
sites-default
dir to my site conf. - Adding an all privileged, and authenticated DB user suitable only for localhost, with a DB with the same name.
- Restarting the server.
My question:
As you can see, I used my parameter ${DOMAIN}
inside the echo
. I did that because I must pass the same argument there as well, otherwise, the DB username and it's associated DB both won't fit my site conf and my Wordpress based site won't boot.
Is this operation logical in Bash or you would take another approach?
To clarify, I ask this to understand if passing arguments based on parameters is valid when the parameter is inside an echo
.
mypassword
on place, without writing anything likemypassword
orMps59azopp1
? inside my script? (Of course,Mps59azopp1
isn't really my password, it's just a random example I made up to demonstrate that I prefer not to keep my DB particular password inside my script. – Arcticooling Nov 10 '17 at 04:02mypassword
and change it manually after the script finished running withSET PASSWORD FOR 'user-name-here'@'hostname-name-here' = PASSWORD('new-password-here');
while the user-name is what I've putted in$domain
. – Arcticooling Nov 10 '17 at 04:04-p
without apassword
string, and when executing the script, it will ask for a password. Try to test this approach – Egor Vasilyev Nov 10 '17 at 06:18identified by "mypassword";
it becomesidentified by -p;
? – Arcticooling Nov 16 '17 at 05:57