Guest User

Untitled

a guest
Jan 22nd, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #in this scrip we will add user($username) and pass($password) to Linux,then we move to Nginx and make a Virtualhost for it(just for HTML index)
  4. #then we move to mysql and create the same username with a database for it with the name of user_db.
  5. #then we can check in our terminal by logging in to our user and logging in to mysql with the same password and
  6. #accessing to our Database
  7. #Here we go :
  8.  
  9. # Script to add a user to Linux system
  10. #we will just add a user and a pass but remember we made this user pass to work all along the way
  11. #meaning this user will be same as the Virtual host in nginx for it then used for mysql database too
  12. #so remember what username and password u make
  13.  
  14. if [ $(id -u) -eq 0 ]; then
  15. read -p "Enter username : " username
  16. read -s -p "Enter password : " password
  17. egrep "^$username" /etc/passwd >/dev/null
  18. if [ $? -eq 0 ]; then
  19. echo "$username exists!"
  20. exit 1
  21. else
  22. pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
  23.  
  24. #if you are wondering what is the "perl" line doing here i have to say its just helps to check security of the password
  25.  
  26. useradd -m -p $pass $username
  27. [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
  28. fi
  29. else
  30. echo "Only root may add a user to the system"
  31. exit 2
  32. fi
  33.  
  34. #now we get to the part of Nginx
  35. #the part to creat virtual host for NGINX
  36.  
  37. domain=$username
  38.  
  39. root="/var/www/$domain/html"
  40. block="/etc/nginx/sites-available/$domain"
  41.  
  42. # Create the Document Root directory
  43. sudo mkdir -p $root
  44.  
  45. # Assign ownership to your regular user account since it must be the root or the creator of the Virtualhost just
  46. #to be able to access it
  47.  
  48. sudo chown -R $USER:$USER $root
  49.  
  50. # Create the Nginx server block file:
  51.  
  52. sudo tee $block > /dev/null <<EOF
  53. server {
  54. listen 80;
  55. listen [::]:80;
  56.  
  57. root /var/www/$domain/html;
  58. index index.html index.htm;
  59.  
  60. server_name $domain www.$domain;
  61.  
  62. location / {
  63. try_files $uri $uri/ =404;
  64. }
  65. }
  66.  
  67.  
  68. EOF
  69.  
  70. # Link to make it available
  71. sudo ln -s $block /etc/nginx/sites-enabled/
  72.  
  73. # Test configuration and reload if successful
  74. sudo nginx -t && sudo service nginx reload
  75.  
  76.  
  77. #The part to make database in mysql with the user name and password we made up there
  78.  
  79. # take the $pass in the pass making part
  80.  
  81. PASSWDDB=$password
  82.  
  83. #take the same username as $username
  84. MAINDB=$username
  85.  
  86. # If /root/.my.cnf exists then it won't ask for root password
  87. if [ -f /root/.my.cnf ]; then
  88.  
  89. mysql -e "CREATE DATABASE ${MAINDB}_db /*\!40100 DEFAULT CHARACTER SET utf8 */;CREATE USER ${MAINDB}_user@localhost IDENTIFIED BY '${PASSWDDB}';GRANT ALL PRIVILEGES ON ${MAINDB}_db.* TO '${MAINDB}_user'@'localhost';FLUSH PRIVILEGES;"
  90.  
  91. # If /root/.my.cnf doesn't exist then it'll ask for root password
  92. else
  93. #echo "Please enter root user MySQL password:"
  94. read -s -p "Enter your mysql user password:" rootpasswd
  95. mysql -uroot -p${rootpasswd} -e "CREATE DATABASE ${MAINDB}_db /*\!40100 DEFAULT CHARACTER SET utf8 */;CREATE USER ${MAINDB}_user@localhost IDENTIFIED BY '${PASSWDDB}';GRANT ALL PRIVILEGES ON ${MAINDB}_db.* TO '${MAINDB}_user'@'localhost';FLUSH PRIVILEGES;"
  96.  
  97. fi
Add Comment
Please, Sign In to add comment