Skip to content

Instantly share code, notes, and snippets.

@GoZOo
Last active December 15, 2015 16:49
Show Gist options
  • Save GoZOo/5292159 to your computer and use it in GitHub Desktop.
Save GoZOo/5292159 to your computer and use it in GitHub Desktop.
Script bash pour créer un utilisateur, son virtualhost (avec SSL) et sa base de données. Nécessite que le fichier à partir duquel la configuration VirtualHost existe déjà (/root/apache2-default et /root/apache2-default-ssl) Idem pour le .bashrc : /etc/root/.bashrc-default
#! /bin/bash
######################
## Script de création de virtualhost + utilisateur + base de données
######################
## /!\ Penser a modifier [ROOTPASSWORD] pour mysql
######################
## USER
# Prompt user informations
read -p "Username : " username
read -p "FTP password : " password
read -p "domaine name : " hostname
read -p "MySQL password : " mysqlpassword
if [ "${username}" = "" ]; then
exit
fi
if [ "${password}" = "" ]; then
exit
fi
if [ "${hostname}" = "" ]; then
exit
fi
serverroot=/home/${hostname}
# Create user
useradd ${username} -g www-data -d ${serverroot} -m -p ${password} -s /bin/bash
# change password to crypted password
echo ${username}:${password} | chpasswd
mkdir ${serverroot}/logs
mkdir ${serverroot}/htdocs
cp /etc/root/.bashrc-default ${serverroot}/.bashrc
chown $username:www-data -R ${serverroot}/
## APACHE
# Copy default conf file to apache
cp /root/apache2-default /etc/apache2/sites-available/${hostname}
cp /root/apache2-default-ssl /etc/apache2/sites-available/${hostname}-ssl
# Replace tokens by real values
sed -i -e 's/%HOSTNAME%/'"${hostname}"'/' -e 's,%WWWROOT%,'"${serverroot}"',' /etc/apache2/sites-available/${hostname}
sed -i -e 's/%HOSTNAME%/'"${hostname}"'/' -e 's,%WWWROOT%,'"${serverroot}"',' /etc/apache2/sites-available/${hostname}-ssl
echo "## Apache configuré avec ${hostname}"
a2ensite ${hostname}
a2ensite ${hostname}-ssl
## MYSQL
# Create MYSQL Database
mysql -u root -p[ROOTPASSWORD] -e "CREATE DATABASE db${username}; GRANT ALL ON db${username}.* TO ${username}@localhost IDENTIFIED BY '${mysqlpassword}'"
echo "## La base db${username} a été créée."
# Test APACHE config
echo "##########################"
echo "# Test de la configuration de Apache :"
apachectl configtest
echo ""
echo "##########################"
echo "## Relancer apache pour prendre en compte les modifications si aucun problème n'a été détecté sur sa configuration : /etc/init.d/apache2 reload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment