Last active
March 23, 2022 17:20
-
-
Save pixeline/d36926207a25e98e2124 to your computer and use it in GitHub Desktop.
Create Hosting User script, for easy Apache web hosting administration.
This file contains 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 useful to create hosting user accounts for Apache. | |
# It creates the system user, its home folder and the virtualhost | |
# Enter a dummy password if you wish to attach domain to an existing user. | |
: ' | |
USAGE | |
$ 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) | |
' | |
ROOT_UID=0 | |
SUCCESS=0 | |
E_USEREXISTS=70 | |
VIRTUALHOST_TEMPLATE='https://gist.github.com/30b7a9423b248541e6b8.git' | |
# 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 0775 $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 "$VIRTUALHOST_TEMPLATE" /etc/apache2/sites-available/tmp | |
sudo mv /etc/apache2/sites-available/tmp/virtualhost_le_template /etc/apache2/sites-available/template.conf | |
sudo rm -Rf /etc/apache2/sites-available/tmp | |
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(s)://$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
Do you have a ubuntu version?