Skip to content

Instantly share code, notes, and snippets.

@daminebenq
Created May 26, 2021 22:40
Show Gist options
  • Save daminebenq/fd5c1066160a668b64c3bcbbc69bc853 to your computer and use it in GitHub Desktop.
Save daminebenq/fd5c1066160a668b64c3bcbbc69bc853 to your computer and use it in GitHub Desktop.
add-domain.sh - apache2
#!/bin/bash
read -p "Enter your desired Domain Name: " DOMAIN
read -p "Enter your desired WEB_ROOT_DIR: " WEBROOT
read -p "Enter your ServerAdmin email address: " EMAIL
name=$DOMAIN
WEB_ROOT_DIR=$WEBROOT
email=${EMAIL-'webmaster@localhost'}
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
sitesAvailabledomain=$sitesAvailable$DOMAIN.conf
mkdir $WEB_ROOT_DIR;
echo "Creating a vhost for $sitesAvailabledomain with a webroot $WEB_ROOT_DIR"
### create virtual host rules file
echo "
<VirtualHost *:80>
ServerAdmin $email
ServerName $DOMAIN
ServerAlias $DOMAIN www.$DOMAIN
DirectoryIndex index.php index.html index.htm
DocumentRoot $WEB_ROOT_DIR
<Directory $WEB_ROOT_DIR>
Options Indexes FollowSymLinks
AllowOverride all
</Directory>
RewriteCond %{HTTP_HOST} ^www.$DOMAIN$
RewriteRule ^(.*)$ http://$DOMAIN/$1 [R=301]
</VirtualHost>" > $sitesAvailabledomain
echo -e $"\nNew Virtual Host Created\n"
sed -i "1s/^/127.0.0.1 $DOMAIN\n/" /etc/hosts
a2ensite $DOMAIN
service apache2 reload
echo "Done, please browse to http://$DOMAIN to check!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment