vhost-manager/bin/vhost-add

131 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
export SCRIPT_NAME="$(realpath "$0")"
export SCRIPT_PATH="$(dirname "$SCRIPT_NAME")"
export PATH="$SCRIPT_PATH/../helpers/:$PATH"
cd "$SCRIPT_PATH/../"
. ./etc/config
exit_if_empty() {
if [ -z "$1" ]; then
if [ -n "$2" ]; then
printf "$2\n"
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" -a "$WEB_HTTPS" = "yes" ]; then
WEB_HTTPS_FORCE="$(form_yes_no -t "$TITLE" -l "Redirect HTTP to HTTPS (recommanded)")"
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 "12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB" < /dev/urandom | head -c 16)"
SYSTEM_USER_USERNAME="$(form_input -t "$TITLE" -l "System username" -d "web" -r)"
SYSTEM_USER_GROUP="webgroup"
SYSTEM_USER_PASSWORD=$(export PASSWORD="$USER_PASSWORD"; export SALT=$(date +'%S'); crypt)
exit_if_empty "$SYSTEM_USER_USERNAME" "Aborded! (SYSTEM_USERNAME_EMPTY)"
###############################
# 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))
export DOMAIN
export DOMAIN_ALIASES
export DOCUMENT_ROOT
export SYSTEM_USER_USERNAME
export SYSTEM_USER_GROUP
export PHP_ENABLED
export PHP_FPM_PORT
export WEB_HTTPS_FORCE
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
template php-fpm > "$PHP_FPM_FILE"
fi
for SERVICE in $SERVICES_TO_RELOAD; do
service $SERVICE reload
done
printf "┌──────────────────────────────────────────┐\n"
printf "│ Nom d'utilisateur : %-20s │\n" "$SYSTEM_USER_USERNAME"
printf "│ Mot de passe : %-25s │\n" "$USER_PASSWORD"
printf "└──────────────────────────────────────────┘\n"