Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ##########################
  4. ####### Setup ############
  5. ##########################
  6.  
  7. #
  8. # save this code to file ubuntu_virtualhost.sh or someyhing else as you wish
  9. # sudo sh ubuntu_setup_virtualhost.sh $USER project_folder_path project_name
  10. # Exemple: sudo sh ubuntu_setup_virtualhost.sh $USER Projects test_project
  11. # Path to files: /home/$USER/Projects/test_project
  12. #
  13.  
  14. ##############################
  15. ####### Init Bash ############
  16. ##############################
  17. ## Set variables
  18. osusername=$1
  19. projectfolder=$2
  20. projectname=$3
  21.  
  22. ## Check OS compatible
  23. unamestr=`gcc --version`
  24. SOURCE="Ubuntu"
  25.  
  26. if echo "$unamestr" | grep -q "$SOURCE"; then
  27. echo "You use Ubuntu!";
  28. else
  29. echo "This bash script just use on Ubuntu!";
  30. exit
  31. fi
  32.  
  33. ## Check params avaliable
  34. if id -u "$1" >/dev/null 2>&1; then
  35. echo "User $1 exists"
  36. else
  37. echo "User name $1 does not exist"
  38. echo "Example:"
  39. echo "sudo sh server_wordpress.sh $User"
  40. exit
  41. fi
  42.  
  43. ## Make sure only root can run our script
  44. if [ "$(id -u)" != "0" ]; then
  45. echo "This script must be run as root! Example: sudo sh server_wordpress.sh" 1>&2
  46. exit 1
  47. fi
  48.  
  49. ## Fix restart issue and change user apache owner
  50. read -r -p " Do you create DB relate for project (y/N)? " response
  51. case $response in [yY][eE][sS]|[yY])
  52. # Mysql init user and db
  53. echo -n " Enter new name for MySQL user (any name): "
  54. read dbuser
  55.  
  56. echo -n " Enter password for this user: "
  57. read dbpass
  58.  
  59. echo -n " Enter name for MySQL database: "
  60. read dbname
  61.  
  62. echo -n " Please enter the password for MySQL root user: "
  63. read dbrootPass
  64.  
  65. user=root
  66.  
  67. BTICK='`'
  68. MYSQL=`which mysql`
  69.  
  70. Q1="DROP DATABASE IF EXISTS $BTICK$dbname$BTICK;"
  71. Q2="CREATE DATABASE IF NOT EXISTS $BTICK$dbname$BTICK CHARACTER SET utf8 COLLATE utf8_general_ci;"
  72. Q3="GRANT USAGE ON *.* TO '$dbuser'@'localhost'; DROP USER '$dbuser'@'localhost';"
  73. Q4="CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';"
  74. Q5="GRANT ALL ON ${BTICK}$dbname${BTICK}.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';"
  75. Q6="FLUSH PRIVILEGES;"
  76.  
  77. mysql --user="$user" --password="$dbrootPass" --execute="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
  78. ;;
  79. *)
  80. echo "No database changes"
  81. ;;
  82. esac
  83.  
  84.  
  85. #########################################
  86. ####### Setup Local Environment #########
  87. #########################################
  88.  
  89. aliaspath="/home/"$osusername"/"$projectfolder
  90.  
  91. if [ -d "$aliaspath" ]; then
  92. rm -rf $aliaspath"/"$projectname
  93. fi
  94.  
  95. mkdir $aliaspath
  96. mkdir $aliaspath"/log"
  97. mkdir $aliaspath"/log/"$projectname
  98. mkdir $aliaspath"/"$projectname
  99.  
  100. chmod -R 0777 $aliaspath
  101. chown -R $osusername:$osusername $aliaspath
  102.  
  103. cd $aliaspath"./"$projectname"/"
  104.  
  105. phpInfoFile=$(cat <<EOF
  106. <?php
  107. phpinfo();
  108. EOF
  109. )
  110.  
  111. indexpath="/home/"$osusername"/"$projectfolder"/"$projectname"/index.php"
  112. echo "$phpInfoFile" >> $indexpath
  113.  
  114. chmod -R 0777 "/home/"$osusername"/"$projectfolder
  115. chown -R $osusername:$osusername "/home/"$osusername"/"$projectfolder
  116.  
  117. ##########################
  118. ####### Setup VH #########
  119. ##########################
  120. wpVHost=$(cat <<EOF
  121. <VirtualHost *:80>
  122.  
  123. ServerAdmin name@mail.com
  124. DocumentRoot "/home/$osusername/$projectfolder/$projectname"
  125.  
  126. DirectoryIndex index.php
  127. ServerName $projectname.local
  128. ServerAlias www.$projectname.local
  129. LimitRequestLine 40949999
  130. LimitRequestFieldSize 40949999
  131.  
  132. SetEnv APPLICATION_ENV "development"
  133.  
  134. <Directory "/home/$osusername/$projectfolder/$projectname">
  135. AllowOverride All
  136. Require all granted
  137. </Directory>
  138.  
  139. ErrorLog /home/$osusername/$projectfolder/log/$projectname/error.log
  140.  
  141. # Possible values include: debug, info, notice, warn, error, crit,
  142. # alert, emerg.
  143. LogLevel warn
  144.  
  145. CustomLog /home/$osusername/$projectfolder/log/$projectname/access.log combined
  146. </VirtualHost>
  147. EOF
  148. )
  149.  
  150. extconfig=".conf"
  151. configspath="/etc/apache2/sites-available/"
  152. hostsfile="/etc"
  153. vhpath=$configspath$projectname$extconfig
  154.  
  155. ## Add localhost DNS domain
  156. cd $hostsfile
  157. echo "127.0.0.1 "$projectname".local www."$projectname".local" >> hosts
  158.  
  159. ## Add VH config and enable
  160. if [ -f $vhpath ]; then
  161. rm $vhpath
  162. fi
  163.  
  164. echo "$wpVHost" >> $vhpath
  165. cd $configspath
  166. a2ensite $projectname$extconfig
  167.  
  168. service apache2 restart
  169.  
  170. echo " All operation complete success!"
  171. echo " All operation complete success!" >> $indexpath
  172. echo " Use next credentials:"
  173. echo " Use next credentials:" >> $indexpath
  174. echo " Database name: "$dbname
  175. echo " Database name: "$dbname >> $indexpath
  176. echo " Database username: "$dbuser
  177. echo " Database username: "$dbuser >> $indexpath
  178. echo " Database user password: "$dbpass
  179. echo " Database user password: "$dbpass >> $indexpath
  180. echo " Path to files: /home/"$osusername"/"$projectfolder"/"$projectname
  181. echo " Path to files: /home/"$osusername"/"$projectfolder"/"$projectname >> $indexpath
  182. echo " Path to log files: /home/"$osusername"/"$projectfolder"/log/"$projectname
  183. echo " Path to log files: /home/"$osusername"/"$projectfolder"/log/"$projectname >> $indexpath
  184. echo " Web url: "$projectname".local/"
  185. echo " Web url: "$projectname".local/" >> $indexpath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement