Create vHost with user and MySQL database


/ Published in: Bash
Save to your folder(s)

With this script a new user gets created and gets his own VirtualHost and database. The username is used for building the domain, so the username "test" leads to the domain test.local.

The password for the MySQL account used for creating the database has to be filled in for the script to work. Also the script has to be run by root.


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. # Script to add a user to Linux system
  3. if [ $(id -u) -eq 0 ]; then
  4. read -p "Enter username : " username
  5. read -s -p "Enter password : " password
  6. egrep "^$username" /etc/passwd >/dev/null
  7. if [ $? -eq 0 ]; then
  8. echo "$username exists!"
  9. exit 1
  10. else
  11. echo "Creating user"
  12. pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
  13. useradd -m -p $pass $username
  14. [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
  15.  
  16. echo "Creating directory public_html"
  17. mkdir /var/www/$username
  18. ln -s /var/www/$username /home/$username/public_html
  19. chown -R $username /var/www/$username
  20. chgrp -R $username /var/www/$username
  21.  
  22. echo "Creating vHost for " $username ".local"
  23. domain=$username.local
  24. cat <<EOF > /etc/apache2/sites-available/$domain
  25. <VirtualHost *:80>
  26. ServerAdmin $username@localhost
  27. DocumentRoot /var/www/$username
  28. ServerName $domain
  29.  
  30. <Directory />
  31. Options FollowSymLinks
  32. AllowOverride None
  33. </Directory>
  34. <Directory /var/www/$username>
  35. Options Indexes FollowSymLinks MultiViews
  36. AllowOverride None
  37. Order allow,deny
  38. allow from all
  39. </Directory>
  40.  
  41. ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  42. <Directory "/usr/lib/cgi-bin">
  43. AllowOverride None
  44. Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  45. Order allow,deny
  46. Allow from all
  47. </Directory>
  48.  
  49. ErrorLog \${APACHE_LOG_DIR}/$domain.error.log
  50. # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
  51. LogLevel warn
  52. CustomLog \${APACHE_LOG_DIR}/$domain.access.log combined
  53.  
  54. Alias /doc/ "/usr/share/doc/"
  55. <Directory "/usr/share/doc/">
  56. Options Indexes MultiViews FollowSymLinks
  57. AllowOverride None
  58. Order deny,allow
  59. Deny from all
  60. Allow from 127.0.0.0/255.0.0.0 ::1/128
  61. </Directory>
  62. </VirtualHost>
  63. EOF
  64.  
  65. echo "Enabling site"
  66. a2ensite $domain
  67.  
  68. echo "Creating database and user"
  69. echo "CREATE DATABASE " $username "; GRANT ALL ON " $username ".* TO " $username "@localhost IDENTIFIED BY '" $password "';" | mysql --user=root --password=
  70.  
  71. echo "Adding " $domain " to /etc/hosts"
  72. echo "127.0.0.1 " $domain >> /etc/hosts
  73.  
  74. echo "Restarting Apache"
  75. /etc/init.d/apache2 reload
  76.  
  77. fi
  78. else
  79. echo "Only root may add a user to the system"
  80. exit 2
  81. fi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.