SHARE
TWEET

create user, website, database

a guest Jun 29th, 2018 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. USERNAME=""
  4. PASSWORD=""
  5. PASSWORD_CONFIRM=""
  6. DOMAIN_NAME=""
  7. VALID_DOMAIN=0
  8.  
  9. echo "Domain, website, user and database setup"
  10.  
  11. while [ ${#USERNAME} -lt 5 ]; do
  12.     read -p 'Username: ' USERNAME
  13. done
  14.  
  15. while [ $VALID_DOMAIN -eq 0 ]; do
  16.     read -p "Domain name: " DOMAIN_NAME
  17.  
  18.     host $DOMAIN_NAME 2>&1 > /dev/null
  19.     if [ $? -eq 0 ]; then
  20.         VALID_DOMAIN=1
  21.     fi
  22. done
  23.  
  24. while ! echo "$PASSWORD" | grep -P '(?=^.{8,255}$)(?=^[^\s]*$)(?=.*\d)(?=.*[A-Z])(?=.*[a-z])'
  25. do
  26.     read -sp "Please enter a complex password: " PASSWORD
  27. done
  28.  
  29. read -sp 'Confirm Password: ' PASSWORD_CONFIRM
  30.  
  31. if [ "$PASSWORD" != "$PASSWORD_CONFIRM" ]; then
  32.     echo "Password didn't match"
  33.     exit
  34. fi
  35.  
  36. # each user will have website
  37. useradd -m $USERNAME -p $PASSWORD
  38.  
  39. if [ $? -ne 0 ]; then
  40.     echo "Failed to add user, quitting"
  41.     exit
  42. fi
  43.  
  44. mkdir -vp /home/$USERNAME/www/
  45. chown -Rv $USERNAME:$USERNAME /home/$USERNAME/www/.
  46.  
  47. echo "Creating /etc/apache2/sites-available/001-$DOMAIN_NAME.conf"
  48. cat <<EOT >> /etc/apache2/sites-available/001-$DOMAIN_NAME.conf
  49. <VirtualHost *:80>
  50.     ServerName $DOMAIN_NAME
  51.     ServerAlias *.$DOMAIN_NAME
  52.  
  53.     ServerAdmin webmaster@$DOMAIN_NAME
  54.     DocumentRoot /home/$USERNAME/www/
  55.  
  56.     ErrorLog ${APACHE_LOG_DIR}/error.log
  57.     CustomLog ${APACHE_LOG_DIR}/access.log combined
  58.  
  59.     <Directory />
  60.       Options Indexes FollowSymLinks Includes ExecCGI
  61.       AllowOverride All
  62.       Require all granted
  63.       Allow from all
  64.     </Directory>
  65.  
  66.     <IfModule mpm_itk_module>
  67.         # AssignUserId user group
  68.         AssignUserId $USERNAME $USERNAME
  69.     </IfModule>
  70.  
  71. </VirtualHost>
  72. EOT
  73.  
  74. ln -v -s /etc/apache2/sites-available/001-$DOMAIN_NAME.conf /etc/apache2/sites-enabled/001-$DOMAIN_NAME.conf
  75.  
  76. systemctl restart apache2
  77.  
  78. echo "Creating mysql username and database"
  79. cat <<EOT >> /tmp/$DOMAIN_NAME.sql
  80. create database $USERNAME;
  81. create user '$USERNAME'@'localhost' identified by '$PASSWORD';
  82. grant all privileges on $USERNAME.* to '$USERNAME'@'localhost';
  83. flush privileges;
  84. EOT
  85.  
  86. mysql -u root < /tmp/$DOMAIN_NAME.sql
  87.  
  88. echo "Installing wordpress into /home/$USERNAME/www/"
  89. wget https://wordpress.org/latest.tar.gz -O /tmp/latest.tar.gz
  90. tar xzf /tmp/latest.tar.gz -C /tmp/
  91. mv /tmp/wordpress*/* /home/$USERNAME/www/
  92.  
  93. find /home/$USERNAME/www/ -type d -exec chmod 775 {} \;
  94. find /home/$USERNAME/www/ -type f -exec chmod 664 {} \;
  95.  
  96. chown -R $USERNAME:$USERNAME /home/$USERNAME/
  97.  
  98. echo "Cleaning up"
  99. rm -rvf /tmp/*
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top