vhost-manager/bin/vhost-add

169 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
SCRIPT_NAME="$(realpath "$0")"
SCRIPT_PATH="$(dirname "$SCRIPT_NAME")"
PATH="$SCRIPT_PATH/../share/bin/:$PATH"
export SCRIPT_NAME
export SCRIPT_PATH
export PATH
cd "$SCRIPT_PATH/../"
. ./etc/config
exit_if_empty() {
if [ -z "$1" ]; then
if [ -n "$2" ]; then
printf "%s\\n" "$2"
fi
exit 1
fi
}
##########################
# Collect of information #
##########################
TITLE="Deblan network - New website"
DOMAIN="$(form_input -t "$TITLE" -l "Domain" -r)"
exit_if_empty "$DOMAIN" "Aborded! (DOMAIN_EMPTY)"
DOMAIN_ALIASES="$(form_input -t "$TITLE" -l "Aliase(s)")"
WEB_HTTP="$(form_yes_no -t "$TITLE" -l "Support of HTTP (recommanded)")"
WEB_HTTPS="$(form_yes_no -t "$TITLE" -l "Support of HTTPS (recommanded)")"
if [ "$WEB_HTTP" = "yes" ] && [ "$WEB_HTTPS" = "yes" ]; then
WEB_HTTPS_FORCE="$(form_yes_no -t "$TITLE" -l "Redirect HTTP to HTTPS (recommanded)")"
else
WEB_HTTPS_FORCE="no"
fi
#if [ "$WEB_HTTPS" = "yes" ]; then
# WEB_HTTPS_GENERATE_CERTIFICATE="$(form_yes_no -t "$TITLE" -l "Retrieve certificate using LE")"
#fi
DOCUMENT_ROOT="$(form_input -t "$TITLE" -l "Document Root" -d "/var/www/service-web/www/$DOMAIN/web")"
PHP_ENABLED="$(form_yes_no -t "$TITLE" -l "Support of PHP")"
exit_if_empty "$DOCUMENT_ROOT" "Aborded! (SYSTEM_USERNAME_EMPTY)"
if [ "$PHP_ENABLED" = "yes" ]; then
PHP_VERSION="$(form_choices -t "$TITLE" -l "Version of PHP" \
"7.3" "PHP 7.3 (recommanded)" \
"7.2" "PHP 7.2" \
"7.1" "PHP 7.1" \
"5.6" "PHP 5.6 (not recommanded)")"
exit_if_empty "$PHP_VERSION" "Aborded! (PHP_VERSION_EMPTY)"
fi
USER_PASSWORD="$(tr -dc "0123456789!@#$%()[]*@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" < /dev/urandom | head -c 20)"
SYSTEM_USER_USERNAME="$(form_input -t "$TITLE" -l "System username" -d "web" -r)"
SYSTEM_USER_GROUP="webgroup"
SYSTEM_USER_PASSWORD=$(
PASSWORD="$USER_PASSWORD"
SALT=$(date +'%S')
export PASSWORD
export SALT
crypt
)
exit_if_empty "$SYSTEM_USER_USERNAME" "Aborded! (SYSTEM_USERNAME_EMPTY)"
################
# Confirmation #
################
export DOMAIN
export DOMAIN_ALIASES
export DOCUMENT_ROOT
export SYSTEM_USER_USERNAME
export SYSTEM_USER_GROUP
export PHP_ENABLED
export PHP_VERSION
export WEB_HTTP
export WEB_HTTPS
export WEB_HTTPS_FORCE
TMP_FILE="/tmp/vhost-add-$(tr -dc "qwertQWERTasdfgASDFGzxcvbZXCVB" < /dev/urandom | head -c 16)"
template summary > "$TMP_FILE"
chmod 600 "$TMP_FILE"
whiptail --textbox "$TMP_FILE" 25 80
rm "$TMP_FILE"
CONTINUE="$(form_yes_no -t "$TITLE" -l "Do you confirm?")"
if [ "$CONTINUE" = "no" ]; then
printf "Aborded!\\n"
exit 0
fi
###############################
# User and directory creation #
###############################
useradd -G "$SYSTEM_USER_GROUP" -s /bin/zsh -m -p "$SYSTEM_USER_PASSWORD" -d "/services/web/www/$DOMAIN" "$SYSTEM_USER_USERNAME" -k /etc/skel/
chgrp www-data "/services/web/www/$DOMAIN"
chmod o-r "/services/web/www/$DOMAIN"
#######################
# Make configurations #
#######################
SERVICES_TO_RELOAD="apache2"
SYSTEM_USER_ID="$(id -u "$SYSTEM_USER_USERNAME")"
PHP_FPM_PORT=$((SYSTEM_USER_ID + 12000))
if [ "$WEB_HTTP" = "yes" ]; then
VHOST_FILE_SA="/etc/apache2/sites-available/${DOMAIN}.${WEB_HTTP_PORT}.conf"
VHOST_FILE_SE="/etc/apache2/sites-enabled/${DOMAIN}.${WEB_HTTP_PORT}.conf"
PORT=$WEB_HTTP_PORT template vhost-http > "$VHOST_FILE_SA"
ln -rs "$VHOST_FILE_SA" "$VHOST_FILE_SE"
fi
if [ "$WEB_HTTPS" = "yes" ]; then
VHOST_FILE_SA="/etc/apache2/sites-available/${DOMAIN}.${WEB_HTTPS_PORT}.conf"
VHOST_FILE_SE="/etc/apache2/sites-enabled/${DOMAIN}.${WEB_HTTPS_PORT}.conf"
PORT=$WEB_HTTPS_PORT template vhost-https > "$VHOST_FILE_SA"
ln -rs "$VHOST_FILE_SA" "$VHOST_FILE_SE"
fi
if [ "$PHP_ENABLED" = "yes" ]; then
if [ "$PHP_VERSION" = "5.6" ]; then
SERVICES_TO_RELOAD="$SERVICES_TO_RELOAD php5-fpm"
PHP_FPM_FILE="/etc/php5/fpm/pool.d/${SYSTEM_USER_USERNAME}.conf"
else
SERVICES_TO_RELOAD="$SERVICES_TO_RELOAD php${PHP_VERSION}-fpm"
PHP_FPM_FILE="/etc/php/$PHP_VERSION/fpm/pool.d/${SYSTEM_USER_USERNAME}.conf"
fi
export PHP_FPM_PORT
template php-fpm > "$PHP_FPM_FILE"
fi
for SERVICE in $SERVICES_TO_RELOAD; do
service $SERVICE reload
done
export USER_PASSWORD
TMP_FILE="/tmp/vhost-add-$(tr -dc "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" < /dev/urandom | head -c 16)"
template result > "$TMP_FILE"
chmod 600 "$TMP_FILE"
whiptail --textbox "$TMP_FILE" 25 80
rm "$TMP_FILE"