Skip to content

Instantly share code, notes, and snippets.

@faejr
Last active December 29, 2016 15:10
Show Gist options
  • Save faejr/a4fe7ea124c543c6bc46fdd568f2d3dd to your computer and use it in GitHub Desktop.
Save faejr/a4fe7ea124c543c6bc46fdd568f2d3dd to your computer and use it in GitHub Desktop.
Virtual host manager (Intended for CloudLinux)
#!/bin/bash
#
# Intended for use with CloudLinux
#
action=$1
domain=$2
siteConfigs='/etc/httpd/conf.d/'
sitesAvailableDomain=$siteConfigs$domain.conf
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
if [ "$action" != '--create' ] && [ "$action" != '--delete' ] && [ "$action" != '--list' ] && [ "$action" != '--set-php' ] && [ "$action" != '--set-php' ] && [ "$action" != '--enable-extension' ]
then
echo "Invalid command usage"
echo "VHostMgr usage:"
echo
echo "--create <domain> -- Creates new virtual host"
echo "--delete <domain> -- Deletes virtual host (inc. user and files if requested)"
echo "--list -- List all active domains"
echo "--set-php <domain> <version> -- Set PHP version to use for virtual host"
echo "--enable-extension <domain> <version> -- Enable extension"
exit 1;
fi
if [ "$action" == '--create' ] || [ "$action" == '--delete' ] || [ "$action" == '--enable-extension' ] || [ "$action" == '--set-php' ]
then
while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g. home.dev, test.dev etc (without www)"
read domain
done
fi
# user variable
user="`echo "$domain"|cut -d. -f1`"
if [ "$action" == '--create' ]
then
### check if domain already exists
if [ -e $sitesAvailableDomain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
ret=false
getent passwd $user >/dev/null 2>&1 && ret=true
if $ret; then
echo -e $"User '$user' already exists"
else
echo -e $"Creating user '$user'..."
adduser $user
chmod -R 755 /home/$user
echo -e $"User created"
password=$(date +%s | sha256sum | base64 | head -c 32 ; echo)
echo "$user:$password" | chpasswd
echo -e $"Username: $user"
echo -e $"Password: $password"
fi
mkdir /home/$user/$domain
mkdir /home/$user/$domain/public_html
chown -R $user:$user /home/$user/$domain
chmod -R 755 /home/$user/$domain
echo "<?php phpinfo();" > /home/$user/$domain/public_html/index.php
if ! echo "Use VHost $domain info@$domain $user" > $sitesAvailableDomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created"
fi
systemctl reload httpd
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at /home/$user/$domain"
exit;
elif [ "$action" == '--list' ]
then
for f in $siteConfigs*.conf
do
f=${f##*/}
f=${f%.conf}
if [[ $f == *"."* ]]
then
echo "$f"
fi
done
exit;
elif [ "$action" == '--delete' ]
then
if [ -e $sitesAvailableDomain ]; then
echo -e $"Selecting $domain"
else
echo -e $"Domain doesn't exist"
exit 1
fi
read -p "Are you sure that you want to delete $domain? " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
rm -f $sitesAvailableDomain
read -p "Do you want to delete the associated user? " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Nn]$ ]]
then
userdel $user
echo -e $"$user has been deleted"
fi
read -p "Do you want to delete the associated home directory? " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Nn]$ ]]
then
rm -rf /home/$user
echo -e $"/home/$user has been deleted"
fi
systemctl reload httpd
echo -e $"Successfully deleted virtual host $domain"
elif [ "$action" == '--set-php' ]
then
version=$3
selectorctl --set-user-current=$version --user=$user
echo -e $"PHP version has been set to $version for $domain\nUnless selectorctl failed..." # SelectorCTL always has exit code 0
elif [ "$action" == '--enable-extension' ]
then
ext=$3
cl-selector --user $user --interpreter php --enable $ext
echo -e $"PHP Extension $ext enabled for $domain"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment