Skip to content

Instantly share code, notes, and snippets.

@mtahle
Last active February 7, 2022 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtahle/44b1c29483b42c8cb39b1cc250aabf88 to your computer and use it in GitHub Desktop.
Save mtahle/44b1c29483b42c8cb39b1cc250aabf88 to your computer and use it in GitHub Desktop.
A shell script to create apache2 virtual host
#!/bin/bash
# created by Mujahed Al-Tahleh mujahed@ieee.org
E_NO_ARGS=65
if [ $# -eq 0 ]; then
echo "No arguments provided"
echo "Required arguments: 1- web dir name under (/var/www/) 2- linux user 3- website domain name"
exit $E_NO_ARGS
fi
#create webdir
WEBDIR=$1
USER=$2
set -e
mkdir /var/www/$WEBDIR
echo "$WEBDIR dir is created "
chown -R $USER:$USER /var/www/$WEBDIR
chmod -R 755 /var/www/$WEBDIR
echo "Permission granted for user: $USER on $WEBDIR dir"
# Create apache conf
DOMAIN=$3
echo "Create apache conf file for ${DOMAIN}"
cat > /etc/apache2/sites-available/${DOMAIN}.conf <<EOL
<VirtualHost *:80>
ServerAdmin webmaster@${DOMAIN}
ServerName ${DOMAIN}
ServerAlias www.${DOMAIN}
DocumentRoot /var/www/${WEBDIR}
ErrorLog ${APACHE_LOG_DIR}/error-${DOMAIN}.log
CustomLog ${APACHE_LOG_DIR}/access-${DOMAIN}.log combined
</VirtualHost>
EOL
a2ensite ${DOMAIN}.conf
echo "Check Apache config for errors "
apache2ctl configtest
echo "restarting apache2"
systemctl restart apache2
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment