Last active
February 6, 2022 01:21
-
-
Save r8928/773c7e648934094bcef3f5a1bf10a042 to your computer and use it in GitHub Desktop.
apache virtual host creation script
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 | |
if [ $# -eq 0 ] | |
then | |
echo "No arguments supplied" | |
exit; | |
fi | |
if [ -z "$2" ] | |
then | |
echo "Please enter both arguments. DOMAIN_NAME and DIRECTORY" | |
exit; | |
fi | |
DOMAIN_NAME=$1 | |
WEB_ROOT_DIR=$2 | |
sitesEnable='/etc/apache2/sites-enabled/' | |
sitesAvailable='/etc/apache2/sites-available/' | |
sitesAvailabledomain=$sitesAvailable$DOMAIN_NAME.conf | |
echo "Creating a vhost for $sitesAvailabledomain with a webroot $WEB_ROOT_DIR" | |
### create virtual host rules file | |
echo " | |
<VirtualHost *:80> | |
ServerName $DOMAIN_NAME | |
DocumentRoot $WEB_ROOT_DIR | |
<Directory $WEB_ROOT_DIR/> | |
AllowOverride All | |
allow from all | |
Options +Indexes | |
</Directory> | |
</VirtualHost>" > $sitesAvailabledomain | |
echo -e $"\nNew Virtual Host Created\n" | |
sed -i "1s/^/127.0.0.1 $DOMAIN_NAME\n/" /etc/hosts | |
a2ensite $DOMAIN_NAME | |
sudo systemctl reload apache2 | |
sudo /etc/init.d/webmin restart; | |
echo "Done, please browse to http://$DOMAIN_NAME to check!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment