You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.7KB

  1. #!/bin/sh
  2. SCRIPT_NAME="$(realpath "$0")"
  3. SCRIPT_PATH="$(dirname "$SCRIPT_NAME")"
  4. PATH="$SCRIPT_PATH/../share/bin/:$PATH"
  5. export SCRIPT_NAME
  6. export SCRIPT_PATH
  7. export PATH
  8. cd "$SCRIPT_PATH/../"
  9. . ./etc/config
  10. exit_if_empty() {
  11. if [ -z "$1" ]; then
  12. if [ -n "$2" ]; then
  13. printf "%s\\n" "$2"
  14. fi
  15. exit 1
  16. fi
  17. }
  18. ##########################
  19. # Collect of information #
  20. ##########################
  21. TITLE="VHOST-MANAGER - NEW WEBSITE"
  22. DOMAIN="$(form_input -t "$TITLE" -l "Domain" -r)"
  23. exit_if_empty "$DOMAIN" "Aborded! (DOMAIN_EMPTY)"
  24. DOMAIN_ALIASES="$(form_input -t "$TITLE" -l "Aliase(s)")"
  25. WEB_HTTP="$(form_yes_no -t "$TITLE" -l "Support of HTTP (recommended)")"
  26. WEB_HTTPS="$(form_yes_no -t "$TITLE" -l "Support of HTTPS (recommended)")"
  27. if [ "$WEB_HTTP" = "yes" ] && [ "$WEB_HTTPS" = "yes" ]; then
  28. WEB_HTTPS_FORCE="$(form_yes_no -t "$TITLE" -l "Redirect HTTP to HTTPS (recommended)")"
  29. else
  30. WEB_HTTPS_FORCE="no"
  31. fi
  32. DOCUMENT_ROOT="$(form_input -t "$TITLE" -l "Document root\n$WEB_ROOT_PATH/$DOMAIN/" -d "web/")"
  33. DOCUMENT_ROOT="$WEB_ROOT_PATH/$DOMAIN/$DOCUMENT_ROOT"
  34. PHP_ENABLED="$(form_yes_no -t "$TITLE" -l "Support of PHP")"
  35. if [ "$PHP_ENABLED" = "yes" ]; then
  36. COMMAND="form_choices -t \"$TITLE\" -l \"Version of PHP\""
  37. for PHP_VERSION in $PHP_VERSIONS; do
  38. PHP_VERSION_NORMALISED="$(echo "$PHP_VERSION" | tr -dc 1234567890)"
  39. eval "PHP_LABEL=\$PHP_${PHP_VERSION_NORMALISED}_LABEL"
  40. COMMAND="$COMMAND \"$PHP_VERSION\" \"$PHP_LABEL\""
  41. done
  42. PHP_VERSION="$(eval "$COMMAND")"
  43. exit_if_empty "$PHP_VERSION" "Aborded! (PHP_VERSION_EMPTY)"
  44. fi
  45. USER_PASSWORD="$(tr -dc "0123456789!@#$%()[]*@<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" < /dev/urandom | head -c 20)"
  46. SYSTEM_USER_USERNAME="$(form_input -t "$TITLE" -l "System username" -d "web" -r)"
  47. SYSTEM_USER_PASSWORD=$(
  48. PASSWORD="$USER_PASSWORD"
  49. SALT=$(date +'%S')
  50. export PASSWORD
  51. export SALT
  52. crypt
  53. )
  54. exit_if_empty "$SYSTEM_USER_USERNAME" "Aborded! (SYSTEM_USERNAME_EMPTY)"
  55. ################
  56. # Confirmation #
  57. ################
  58. export DOMAIN
  59. export DOMAIN_ALIASES
  60. export DOCUMENT_ROOT
  61. export SYSTEM_USER_USERNAME
  62. export SYSTEM_USER_GROUP
  63. export PHP_ENABLED
  64. export PHP_VERSION
  65. export WEB_HTTP
  66. export WEB_HTTPS
  67. export WEB_HTTPS_FORCE
  68. export WEB_ROOT_PATH
  69. export WEB_LOG_PATH
  70. export WEB_HTTPS_DEFAULT_CERT_PEM
  71. export WEB_HTTPS_DEFAULT_CERT_PRIVKEY
  72. export WEB_HTTPS_DEFAULT_CERT_CHAIN
  73. export WEB_BIND_ADDRESS
  74. TMP_FILE="/tmp/vhost-add-$(tr -dc "qwertQWERTasdfgASDFGzxcvbZXCVB" < /dev/urandom | head -c 16)"
  75. touch "$TMP_FILE"
  76. chmod 600 "$TMP_FILE"
  77. template summary > "$TMP_FILE"
  78. whiptail --textbox "$TMP_FILE" --title "$TITLE - PREVIEW" 25 80
  79. rm "$TMP_FILE"
  80. CONTINUE="$(form_yes_no -t "$TITLE" -l "Do you confirm?" -n)"
  81. if [ "$CONTINUE" = "no" ]; then
  82. printf "Aborded!\\n"
  83. exit 0
  84. fi
  85. ###############################
  86. # User and directory creation #
  87. ###############################
  88. useradd -G "$SYSTEM_USER_GROUP" -s "$DEFAULT_SHELL" -m -p "$SYSTEM_USER_PASSWORD" -d "$WEB_ROOT_PATH/$DOMAIN" "$SYSTEM_USER_USERNAME" -k "$SKEL_PATH"
  89. chgrp www-data "$WEB_ROOT_PATH/$DOMAIN"
  90. chmod 750 "$WEB_ROOT_PATH/$DOMAIN"
  91. touch "$WEB_LOG_PATH/$DOMAIN.log"
  92. chmod 640 "$WEB_LOG_PATH/$DOMAIN.log"
  93. chgrp "$SYSTEM_USER_USERNAME" "$WEB_LOG_PATH/$DOMAIN.log"
  94. #######################
  95. # Make configurations #
  96. #######################
  97. SERVICES_TO_RELOAD="apache2"
  98. SYSTEM_USER_ID="$(id -u "$SYSTEM_USER_USERNAME")"
  99. PHP_FPM_PORT=$((SYSTEM_USER_ID + 12000))
  100. export PHP_FPM_PORT
  101. if [ "$WEB_HTTP" = "yes" ]; then
  102. VHOST_FILE_SA="$APACHE_VHOST_SITES_AVAILABLE_PATH/${DOMAIN}.${WEB_HTTP_PORT}.conf"
  103. VHOST_FILE_SE="$APACHE_VHOST_SITES_ENABLED_PATH/${DOMAIN}.${WEB_HTTP_PORT}.conf"
  104. PORT=$WEB_HTTP_PORT template vhost-http > "$VHOST_FILE_SA"
  105. ln -rs "$VHOST_FILE_SA" "$VHOST_FILE_SE"
  106. fi
  107. if [ "$WEB_HTTPS" = "yes" ]; then
  108. VHOST_FILE_SA="$APACHE_VHOST_SITES_AVAILABLE_PATH/${DOMAIN}.${WEB_HTTPS_PORT}.conf"
  109. VHOST_FILE_SE="$APACHE_VHOST_SITES_ENABLED_PATH/${DOMAIN}.${WEB_HTTPS_PORT}.conf"
  110. PORT=$WEB_HTTPS_PORT template vhost-https > "$VHOST_FILE_SA"
  111. ln -rs "$VHOST_FILE_SA" "$VHOST_FILE_SE"
  112. fi
  113. if [ "$PHP_ENABLED" = "yes" ]; then
  114. PHP_VERSION_NORMALISED="$(echo "$PHP_VERSION" | tr -dc 1234567890)"
  115. eval "FPM_POOL_PATH=\$PHP_FPM${PHP_VERSION_NORMALISED}_POOL_PATH"
  116. eval "FPM_SERVICE=\$PHP_FPM${PHP_VERSION_NORMALISED}_SERVICE"
  117. SERVICES_TO_RELOAD="$SERVICES_TO_RELOAD $FPM_SERVICE"
  118. PHP_FPM_FILE="$FPM_POOL_PATH/${SYSTEM_USER_USERNAME}.conf"
  119. template php-fpm > "$PHP_FPM_FILE"
  120. fi
  121. for SERVICE in $SERVICES_TO_RELOAD; do
  122. service $SERVICE reload
  123. done
  124. export USER_PASSWORD
  125. TMP_FILE="/tmp/vhost-add-$(tr -dc "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" < /dev/urandom | head -c 16)"
  126. touch "$TMP_FILE"
  127. chmod 600 "$TMP_FILE"
  128. template result > "$TMP_FILE"
  129. whiptail --textbox "$TMP_FILE" --title "$TITLE - FINISH" 25 80
  130. cat "$TMP_FILE"
  131. rm "$TMP_FILE"