Last active
February 7, 2022 17:45
-
-
Save mtahle/44b1c29483b42c8cb39b1cc250aabf88 to your computer and use it in GitHub Desktop.
A shell script to create apache2 virtual host
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 | |
# created by Mujahed Al-Tahleh [email protected] | |
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