Skip to content

Instantly share code, notes, and snippets.

@StuMason
Created June 1, 2018 10:15
Show Gist options
  • Save StuMason/c94c62b67a2e6e8c196a1a8973bcfd83 to your computer and use it in GitHub Desktop.
Save StuMason/c94c62b67a2e6e8c196a1a8973bcfd83 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create a NGINX file, index.php and mysql DB for $DOMAIN passed in first argument e.g
# sudo bash create.sh domain.co.uk
# needs /root/.my.conf set up with [client] root user and password to create DB
# needs certbot installed to do certbot
DOMAIN=$1
cp /etc/nginx/sites-available/basic /etc/nginx/sites-available/${DOMAIN}
sed -i "s/:domain/${DOMAIN}/g" /etc/nginx/sites-available/${DOMAIN}
ln -s /etc/nginx/sites-available/${DOMAIN} /etc/nginx/sites-enabled/${DOMAIN}
printf "created /etc/nginx/sites-available/${DOMAIN}"
mkdir -p /var/www/${DOMAIN}/public
printf "public folder here /var/www/${DOMAIN}/public"
printf "<?php echo 'Domain ${DOMAIN} waiting for your magic'; ?>" > /var/www/${DOMAIN}/public/index.php
chown -R www-data:www-data /var/www/${DOMAIN}
PASSWDDB="$(openssl rand -base64 12)"
mysql --defaults-extra-file=/root/.my.conf -e "CREATE DATABASE ${DOMAIN//[^a-zA-Z_-]/_} /*\!40100 DEFAULT CHARACTER SET utf8 */;"
mysql --defaults-extra-file=/root/.my.conf -e "CREATE USER ${DOMAIN//[^a-zA-Z_-]/_}@localhost IDENTIFIED BY '${PASSWDDB}';"
mysql --defaults-extra-file=/root/.my.conf -e "GRANT ALL PRIVILEGES ON ${DOMAIN//[^a-zA-Z_-]/_}.* TO '${DOMAIN//[^a-zA-Z_-]/_}'@'localhost';"
mysql --defaults-extra-file=/root/.my.conf -e "FLUSH PRIVILEGES;"
printf "Database: ${DOMAIN//[^a-zA-Z_-]/_}\nUsername:${DOMAIN//[^a-zA-Z_-]/_}\nPassword:${PASSWDDB}" > /var/www/${DOMAIN}/config
printf "Database config here: /var/www/${DOMAIN}/config"
sudo systemctl reload nginx
sudo systemctl restart nginx
certbot -n --redirect --nginx -d ${DOMAIN} -d www.${DOMAIN}
@StuMason
Copy link
Author

StuMason commented Jun 1, 2018

/etc/nginx/sites-available/basic is available here:
https://gist.github.com/StuMason/0623ff9ab1a3ea7d80641b6a713dab43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment