-
-
Save pixeline/9e5ea7d28e52138f0b31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# script automatisant la création d'un compte hébergé: utilisateur + dossier + virtualhost | |
# Enter dummy password if you wish to attach domain to an existing user. | |
: ' | |
HOW-TO | |
$ bash /home/pixeline/bin/create-hosting-user username password domain.tld | |
Arguments: | |
- username (if not created, create it, otherwise use current) SEE https://gist.github.com/pixeline/9e5ea7d28e52138f0b31 | |
- user password (for existing user, enter anything, it will not be used) | |
- domain name (will also be used to name the folder containing the website) | |
TODO: | |
- check against invalid arguments | |
- add users credentials first, attach it to sftponly group, and use them for the virtualhost | |
' | |
ROOT_UID=0 | |
SUCCESS=0 | |
E_USEREXISTS=70 | |
# Run as root, of course. (this might not be necessary, because we have to run the script somehow with root anyway) | |
if [ "$UID" -ne "$ROOT_UID" ] | |
then | |
echo "Must be root to run this script." | |
exit $E_NOTROOT | |
fi | |
#test if all arguments are there | |
if [ $# -eq 3 ]; then | |
username=$1 | |
pass=$2 | |
domain=$3 | |
email="webmaster@$domain" | |
webroot="/home/$username/www/$domain/public" | |
# Check if user already exists. | |
grep -q "$username" /etc/passwd | |
if [ ! $? -eq $SUCCESS ]; then | |
echo "User $username does not exist. I'll create it now.." | |
# Create the User | |
useradd -p `mkpasswd "$pass"` -d /home/"$username" -m -g users -s /bin/false "$username" | |
usermod -aG $username $username | |
usermod -aG sftponly $username | |
usermod -aG www-data $username | |
echo "The $username account is now setup." | |
fi | |
echo "Creating the website folder and virtualhost ..." | |
# Check if git is installed | |
if ! hash git 2>/dev/null; then | |
echo -e "Git is not installed! You will need it at some point anyways..." | |
echo -e "Exiting, install git first." | |
exit 0 | |
fi | |
sudo -v | |
# Check if the webroot exists | |
if [ ! -d "$webroot" ]; then | |
echo "Creating $webroot directory" | |
mkdir -p $webroot | |
chown root:root /home/$username | |
chown -R $username:sftponly /home/$username/* | |
# grant apache permissions | |
chown -R www-data:www-data $webroot | |
chmod -R 770 $webroot | |
fi | |
# Virtualhost | |
echo "Checking for the virtual host template file..." | |
if [ ! -f /etc/apache2/sites-available/template.conf ]; then | |
echo "Downloading template file with Git..." | |
sudo git clone https://gist.github.com/922f6c541907ff3c1700.git /etc/apache2/sites-available/temp | |
sudo mv /etc/apache2/sites-available/temp/template /etc/apache2/sites-available/template.conf | |
sudo rm -Rf /etc/apache2/sites-available/temp | |
fi | |
echo "Creating the new $domain virtual host file that has a webroot of: $webroot" | |
if [ -f /etc/apache2/sites-available/template.conf ]; then | |
sudo cp /etc/apache2/sites-available/template.conf /etc/apache2/sites-available/$domain.conf | |
sudo sed -i 's/template.email/'$email'/g' /etc/apache2/sites-available/$domain.conf | |
sudo sed -i 's/template.url/'$domain'/g' /etc/apache2/sites-available/$domain.conf | |
sudo sed -i 's#template.webroot#'$webroot'#g' /etc/apache2/sites-available/$domain.conf | |
echo "Adding $domain to the /etc/hosts file..." | |
sudo sed -i '1s/^/127.0.0.1 '$domain'\n/' /etc/hosts | |
sudo a2ensite $domain | |
sudo service apache2 reload | |
echo "Virtual host $domain created with a webroot at $webroot reachable from http://$domain" | |
else | |
echo "Could not find the template.conf file" | |
exit 0 | |
fi | |
else | |
echo " this programm needs 3 arguments you have given $# " | |
echo " you have to call the script $0 username and the pass " | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment